使用Windsor进行常规存储库生命周期配置 [英] Generic repository lifetime configuration with Windsor

查看:73
本文介绍了使用Windsor进行常规存储库生命周期配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何配置适用于Windows应用程序中存储库的Windsor容器。
我有通用的存储库实现存储库,其中T是实体类型,它具有依赖项IDatacontextProvider,它为它提供数据上下文:

I'm out of ideas how to configure right Windsor container for use with repositories in Windows app. I have generic repository implementation Repository, where T is entity type, it has a dependency IDatacontextProvider, which provides datacontext for it:

public class Repository<T> : IRepository<T> where T : class
{
    protected DataContext DataContext;
    public Repository(IDataContextProvider dataContextProvider) {
         DataContext = dataContextProvider.DataContext;
    }
    ....
}

为了简单起见使用以下配置,一切正常。

And for simple things everything works ok with following configuration:

 container.Register(                                
            Component.For<IDataContextProvider>()
                  .ImplementedBy<DataContextProvider>()
                  .Lifestyle.Transient,
            Component.For(typeof(IRepository<>))
                  .ImplementedBy(typeof(Repository<>))
                  .Lifestyle.Transient, .... 

当我尝试加入其他问题时出现问题只要每个存储库实例具有不同的数据上下文实例,这些实体就可以来自多个存储库。

例如,我有简单的服务:

Problems occur, when i try to join different entities from several repositories, as long as each repository instance has different data context instance.
For example i have simple service:

public class SimpleService : ISimpleService {
     public SimpleService(IRepository<Order>, IRepository<OrderLine>) {
         ....
     }
}

我可以将IDataContextProvider设置为Singleton,但我认为这会带来更大的问题。

我可以将IDataContextProvider传递给SimpleService,并尝试在那里解析存储库实例,但这将需要其他代码来使服务易于测试,并且还需要其他依赖项。
也许有人对如何解决这个问题有更好的主意?

I could make IDataContextProvider as Singleton, but i think that would bring even bigger problems.
I could pass IDataContextProvider to SimpleService, and try to resolve repository instances there, but that would require additional code to make service easy testable and would require additional dependencies. May be somebody has a better idea how to solve this?

更新:
根据建议,我创建了存储库工厂(这有点与答案中提出的建议不同,它与数据上下文没有直接的依赖关系,但是想法是完全相同的):

update: following advice, I've created repository factory (it's little bit different from proposed in answer, it does not have direct dependency to datacontext, but idea is very same):

 public interface IRepositoryFactory
{
    IRepository<T> GetRepository<T>() where T:class;
}

public class RepositoryFactory : IRepositoryFactory
{
    private readonly IDataContextProvider dataContextProvider;

    public RepositoryFactory(IDataContextProvider dataContextProvider)
    {
        this.dataContextProvider = dataContextProvider;
    }

    public IRepository<T> GetRepository<T>() where T : class
    {
        return new Repository<T>(dataContextProvider);
    }
}


推荐答案

关于它们之间的另一层,例如RepositoryFactory?那可能是一种短暂的生活方式。从工厂创建的所有存储库将共享相同的DataContext实例。您还需要更改您的存储库类,以便它们采用DataContext实例而不是DataContextProvider。

What about having another layer in between, such as a RepositoryFactory? That one could have a transient lifestyle. All repositories created from the factory would share the same DataContext instance. You would also need to change your repository classes so they take a DataContext instance instead of a DataContextProvider.

public class RepositoryFactory : IRepositoryFactory
{
   protected DataContext dataContext;
   public RepositoryFactory(IDataContextProvider provider)
   {
      dataContext = dataContextProvider.DataContext;
   }

   public IRepository<T> GetRepository<T>()
   {
      return new Repository<T>(dataContext);
   }
}

public class SimpleService : ISimpleService {
     public SimpleService(IRepositoryFactory factory) {
         ....
     }
}

这篇关于使用Windsor进行常规存储库生命周期配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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