我正在创建一个三层体系结构的应用程序。
- UI(Web)
- BLL(ClassLibrary)
- DAL(ClassLibrary)
我的需求是像角色、城市、国家、州等一样绑定下拉列表。
我为所有实体创建了具有相同结构的属性。
public class City
{
public string Id { get; set; }
public string Name { get; set; }
}
考虑下面这个控制台应用程序的示例,它复制了我的需求。
假设这个调用是来自我的用户界面。
static void Main( string[] args )
{
List<City> cities = GetDataList<City>();
List<Role> roles = GetDataList<Role>();
}
这是我的业务逻辑层(BLL)
public static List<T> GetDataList<T>() where T:class ,new()
{
List<T> list = new List<T>();
(A) List<T> data = ** I am not getting the way how to call different methods of data acess here?
foreach (var temp in data.AsEnumerable())
{
var obj = new T();
( B) foreach (var prop in obj.GetType().GetProperties())
{
var propertyInfo = obj.GetType().GetProperty(prop.Name);
propertyInfo.SetValue(obj , Convert.ChangeType(temp. , propertyInfo.PropertyType) , null);
}
list.Add(obj);
}
return list;
}
这是我的数据访问层,我在这里使用EF
public static IList<City> FetchCityData()
{
List<City> dict = new List<City>();
dict.Add(new City{Id="1",Name="hello"});
dict.Add(new City{Id="2",Name="world"});
return dict;
}
public static List<Role> FetchStateData()
{
List<Role> dict = new List<Role>();
dict.Add(new Role { Id = "1" , Name = "hello" });
dict.Add(new Role { Id = "2" , Name = "world" });
return dict;
}
我有以下问题:
A) 如何从BLL访问不同的数据访问方法?
B) 我应该如何为我的类属性分配值?
A和B是代码中的标签。
FetchCityData
和FetchStateData
,无法概括它们。你唯一的输入是类型T
,从那里分支。 - HopelessCity
和Role
类的访问权限吗?如果可能的话,你可以创建一个基类或接口来暴露一个像FetchData
这样的方法。这意味着你将所有的FetchCityData
和FetchStateDate
方法替换为详细实现。 - Hopeless