【问题标题】:Linq 2 SQL - A Better way of doing thisLinq 2 SQL - 一种更好的方法
【发布时间】:2010-10-12 21:24:06
【问题描述】:

我有一个存储库基类,它从我的 mvc 应用程序中的控制器调用

每个控制器都有自己的存储库

喜欢这个

public class ServiceRepository : Bluegrass.Mvc.RepositoryBase<Models.Service>
{
    public ServiceRepository()
    {
        this._db = new iSppms.Models.iSppmsDataContext();
    }
}

然后像被使用一样

internal DataAccess.ServiceRepository repository =
        new iSppms.DataAccess.ServiceRepository();
    public ActionResult Index()
    {
        var viewData = new ViewData.ServiceIndexViewData(
            repository.GetItems<Models.Service>());
        return View(viewData);
    }

我想做的不是传递模型,因为 RepositoryBase 已经是 Models.Service 类型

我正在寻找一种更好的方式来构建它。

public class RepositoryBase<T> where T : class
{
    public DataContext _db;

    public List<T> GetItems<T>() where T : class, Interfaces.IModel
    {
        var table = _db.GetTable<T>();
        var data = table.Where(t => !t.Deleted);

        return data.ToList();
    }

【问题讨论】:

    标签: c# generics


    【解决方案1】:

    也许这篇Repository Base Class 文章有帮助?

    【讨论】:

      【解决方案2】:

      您能否将IModel 位添加到RepositoryBase&lt;T&gt;,并使GetItems&lt;T&gt; 非泛型?注意:重复使用通用令牌 T 已经是一个坏主意(在 GetItems不同):

      public class RepositoryBase<T> where T : class, Interfaces.IModel
      {
          public DataContext _db;
      
          public List<T> GetItems()
          {
              var table = _db.GetTable<T>();
              var data = table.Where(t => !t.Deleted);
      
              return data.ToList();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-27
        相关资源
        最近更新 更多