4

我目前的应用程序结构工作模式的去耦单元是:净 - 多ORM的

  • 模型组装
  • 资料汇编
    • 定义由一个ORM实现仓库接口
  • ORM程序集
    • 实现存储库接口数据组装
    • 采用统一(IoC容器),以regsister Data.IRepository<>ORM.GenericRepository<>
  • 业务组件
    • 引用的数据和模型组件
    • 采用统一解决的类型IRepository<>
  • UI组件
    • Refe分配办法业务组件

该结构基本上已经解耦从ORM实施IRepository<T>业务层。

这个解耦结构的好处之一是我应该能够相对容易地替换ORM - 例如,从Entity Framework迁移到NHibernate或者只是升级现有的ORM。我目前首先使用EF 4.1代码,并为NHibernate构建另一个程序集。

我正在考虑实施工作单元模式。

我读过这个模式应该用在业务层(使用我的数据程序集中定义的接口,并在ORM程序集中实现,就像我使用存储库模式一样)。目前,所有实例化的存储库都有它们自己的DbContext/session,并且它的生命周期被设置为存储库的生命周期 - 这可能是不好的 - 我的问题是我不确定它是否可能实现工作单元模式,不同的ORM(呃,可能是,我只是没有达到速度)。

这是我想到的唯一的事情:

在具有功能我的数据组件创建IUnitOfWork:object GetCurrentSession();,然后有在ORM组件repositorys的构造函数的参数,并将其投射到适当的会议/ DbContext(如果NHibernate然后ISession,如果实体框架然后DbContext)

如果有人有一些洞察到这种情况,我将不胜感激。

回答

0

我可能已经找到了解决方案(还没有尝试过尚未):

在数据组件:

public interface IUnitOfWork : IDisposable 
{ 
    void Start(); 
    T Current<T>(); 
    // IDisposable stuff 
} 

在ORM组件:

public class GenericRepository<T> : IRepository<T> 
    where T : PersistentEntity 
{ 
    private ISession CurrentSession; 

    public GenericRepository(IUnitOfWork uow) 
    { 
     CurrentSession = uow.Current<ISession>(); 
    } 

    // other repository stuff here 
} 

public class NHhibernateUnitOfWork : IUnitOfWork 
{ 
    private ISession CurrentSession; 

    public void Start() 
    { 
     // instantiate CurrentSession 
    } 

    T Current<T>() 
    { 
     if(typeof(T) is ISession) 
     return (T)CurrentSession; 
     else 
     return new NotImplementedException(); 
    } 
} 

// in the prism module 
container.Resolve(typeof(IUnitOfWork), typeof(NHhibernateUnitOfWork)); 

在业务组装中:

IUnitOfWork uow = container.Resolve<IUnitOfWork>(); 
using(uow.Start()) 
{ 
    IRepository custRepo = container.Resolve<IRepository<Customer>>(uow); 
    // do stuff here with cust repo 
} 

这只是将IUnitOfWork从具体实现中解耦出来的概念证明。

+0

寻找想法:http://bit.ly/gHLubu。 – Steven 2011-04-12 07:41:23