Linq to SQL使用IoC,依赖注入,工作单元的存储库模式 [英] Repository pattern with Linq to SQL using IoC, Dependency Injection, Unit of Work

本文介绍了Linq to SQL使用IoC,依赖注入,工作单元的存储库模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于为Linq to SQL实现存储库模式,似乎有很多示例.其中大多数具有IRepository和DI功能;有些实施了工作单元,有些则没有.我试图读取在SO和Google上Linq到SQL存储库模式的搜索返回的大多数结果.不过,我还没有找到完整的解决方案.

There seems to be lots of examples on implementing Repository pattern for Linq to SQL. Most of them featuring IRepository and DI; Some have implemented Unit of Work and some not. I tried to read as most of the results returned by searches on SO and Google on Linq to SQL repository patterns. Nevertheless I've not come across a complete solution yet.

从我的阅读材料中,我实现了一个存储库模式,如下所示:

From my readings I've implemented a repository pattern as shown below:

我正在使用DI来注册存储库所依赖的接口:

I'm using DI to register interfaces on which the repositories are depended:

this.container.RegisterType<IConnectionStringFactory, ConnectionStringFactory>(new ContainerControlledLifetimeManager(),
    new InjectionConstructor(connectionString));
this.container.RegisterType<IDataContextFactory, DataContextFactory>();

存储库模式的实现

public interface IPrivilegeRepository : IRepository<PrivilegesEntity>
{
   IList<MenuModel> GetRootMenu();
   IList<MenuModel> GetChildMenu(int parentId);
}

public class PrivilegeRepository : Repository<PrivilegesEntity>, IPrivilegeRepository
{
    #region IPrivilegeRepository Members

    public IList<MenuModel> GetRootMenu()
    {
        return FindAll(menu => menu.ParentId == null)
            .OrderBy(menu => menu.SortOrder)
            .Select(c => EntityMapper.Privileges.ToBusinessObject(c))
            .ToList();
    }

    public IList<MenuModel> GetChildMenu(int parentId)
    {
        return FindAll(menu => menu.ParentId == parentId)
            .OrderBy(menu => menu.SortOrder)
            .Select(menu => EntityMapper.Privileges.ToBusinessObject(menu))
            .ToList();
    }

    #endregion

    public PrivilegeRepository(IDataContextFactory dataContextFactory)
        : base(dataContextFactory)
    {
    }
}

IRepository通用接口:

IRepository generic interface:

public interface IRepository<T> where T : class
{
    IEnumerable<T> All();
    IEnumerable<T> FindAll(Expression<Func<T, bool>> exp);
    T Single(Expression<Func<T, bool>> exp);
    T First(Expression<Func<T, bool>> exp);
}

Repository类通过IRepository的实现(未显示)通过以下方式实现,并且依赖于DI负责的IDataContextFactory:

Repository class is implemented as below with implementations of IRepository (not shown) and is having a dependency on IDataContextFactory which DI is taking care of:

public class Repository<T> : IRepository<T> where T : class
{
    public Repository(IDataContextFactory dataContextFactory)
    {
        this.dataContextFactory = dataContextFactory;
    }
}

使用IoC使用存储库:

Repositories are consumed using IoC:

PrivilegeRepository repository = container.Resolve<PrivilegeRepository>();

我将查询结果作为业务对象的集合返回,以避免在消耗存储库的应用程序层上依赖Linq依赖于SQL.上面的方案与我的使用MVVM模式的WPF应用程序配合良好.我有不依赖于Linq-SQL生成的类的ViewModel aks Presenter类.

I'm returning result of queries as a collection of Business Object in order to avoid dependency on Linq to SQL on application layers where I consume repository. The above scenario works fine with my WPF application which is using MVVM pattern. I have ViewModel aks Presenter classes which are not depended of classes generated by Linq-SQL.

如何扩展此模式,以便可以将数据保存到数据库.我想将业务对象传递回存储库并保存它们.是否有可能?在这种情况下,如何实现工作单元.

推荐答案

基本思想是通用存储库接口不能很好地工作,但是通用存储库实现可以很好地工作.它使用LINQ to SQL作为ORM示例,并且应该为您的问题提供一些见识.

The basic idea is that generic repository interfaces don't work so well, but generic repository implementations work great. It uses LINQ to SQL as the example ORM and should provide some insight to your question.

请务必仔细阅读保罗的回答,尤其是评论.

Be sure to read through Paul's answer as well, especially the comments.

这篇关于Linq to SQL使用IoC,依赖注入,工作单元的存储库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆