【问题标题】:Setting Context in LINQ to SQL Repository在 LINQ to SQL 存储库中设置上下文
【发布时间】:2012-06-20 06:25:14
【问题描述】:

我正在尝试使用 LINQ to SQL 数据上下文来实现通用存储库模式。上下文以 NULL 形式出现。需要进行哪些更改才能获取上下文并使其正常工作?

我有一个 dbml 文件,其中包含以下内容:

public partial class LibraryManagementClassesDataContext : System.Data.Linq.DataContext

它有帐户实体

代码

static void Main(string[] args)
{

        RepositoryLayer.Repository<RepositoryLayer.Account> selectedRepository = new RepositoryLayer.Repository<RepositoryLayer.Account>();
        AccountBusiness accountBl = new AccountBusiness(selectedRepository);
        List<RepositoryLayer.Account> accountList =   accountBl.GetAllAccounts();

}


namespace RepositoryLayer
{
public interface IRepository<T> where T : class
{
    System.Linq.IQueryable<T> GetAll();
}

public class Repository<T> : IRepository<T> where T : class
{
    public System.Data.Linq.DataContext Context
    {
        get;
        set;
    }

    public virtual System.Linq.IQueryable<T> GetAll()
    {
        if (Context == null)
        {
            throw new Exception("Context is null");
        }
        return Context.GetTable<T>();
    }

}
}

public class AccountBusiness
{
    //IRepository<T>
    RepositoryLayer.IRepository<RepositoryLayer.Account> accountRepository;
    public AccountBusiness(RepositoryLayer.IRepository<RepositoryLayer.Account> repo)
    {
        accountRepository = repo;
    }

    public List<RepositoryLayer.Account> GetAllAccounts()
    {
        IQueryable<RepositoryLayer.Account> acc = accountRepository.GetAll();
        return acc.ToList();
    }

}

阅读:

  1. LinqToSql declare and instantiate DataContext best practice?

【问题讨论】:

    标签: c# .net linq linq-to-sql repository-pattern


    【解决方案1】:

    当然它是空的:你永远不会为你的Context 属性赋值。去做吧:

    using(var context = new LibraryManagementClassesDataContext())
    {
        RepositoryLayer.Repository<RepositoryLayer.Account> selectedRepository = new RepositoryLayer.Repository<RepositoryLayer.Account>();
        selectedRepository.Context = context;
        AccountBusiness accountBl = new AccountBusiness(selectedRepository);
        List<RepositoryLayer.Account> accountList =   accountBl.GetAllAccounts();
    }
    

    我还建议Repository.DataContextLibraryManagementClassesDataContext 类型。

    【讨论】:

    • 谢谢。但是我遇到了两个错误 - 1) using 语句中使用的类型必须隐式转换为 'System.IDisposable' .... 2) 无法将类型 'LINQTest1.DataContext' 隐式转换为 'System.Data.Linq.DataContext '。注意:我有一个 dbml 文件,其中包含以下内容: public partial class LibraryManagementClassesDataContext : System.Data.Linq.DataContext
    【解决方案2】:

    由于上下文尚未实例化,您可以在类构造函数中创建它。像这样的...

    public class Repository<T> : IRepository<T> where T : class
    {
        public Repository()
        {
            this.Context = new System.Data.Linq.DataContext()
        }
    }
    

    您还可以让您的上下文保持短暂的生命,并尽早将其处理掉。在此处阅读有关尽早处理它的更多信息:In LINQ-SQL, wrap the DataContext is an using statement - pros cons

    public virtual System.Linq.IQueryable<T> GetAll()
    {
        using (var context == new System.Data.Linq.DataContext())
        {
            return Context.GetTable<T>();
        }
    }
    

    【讨论】:

    • 你忘了释放数据上下文
    • 是的,添加了另一个示例,展示了如何正确处理它以及指向另一个关于处理数据上下文的 SO 问题的链接。
    • 如果您这样做,您将结束检索未连接到任何数据上下文的表(因为它已被释放),从而导致异常。此外,为每个查询使用一个数据上下文是一种不好的做法。
    猜你喜欢
    • 1970-01-01
    • 2011-01-09
    • 2019-12-12
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多