我的数据库中有大量表,它们基本上是支持数据的。这些表格列出了国籍、性别、语言等,并且都基于相同的数据模型:
public class SupportDataModel
{
public int Id { get; set; }
public string Name { get; set; }
public bool Deleted { get; set; }
}
public class Gender : SupportDataModel
{
}
此数据主要显示在 DropDownList 控件中,因此我需要查询每个表以获取列表。因为我不想每次需要访问数据时都重写这个查询,所以我把它写成一个帮助类:
public class GendersHelper : IAlternateHelper<Gender>
{
public List<Gender> ListItems()
{
using (var db = new ApplicationDbContext())
{
return db.Genders.Where(x => !x.Deleted).ToList();
}
}
}
对于这些类中的每一个,这个函数都是相同的,除了它查询的表中。这就是为什么我想编写一个单独的类,它使用我传递给它的类型作为我要查询的表的决定因素,但我不知道该怎么做。
这是我到目前为止所得到的......
public abstract class SupportingDataHelper<T>
{
public List<T> ListItems()
{
// Logic to determine which table gets queried,
// as well as the query itself should go here.
}
}
如何让这个方法从传入的类型中确定要查询的表,然后返回这些项目的列表?