如何实现一个通用RepositoryFactory? [英] How to implement a generic RepositoryFactory?

查看:935
本文介绍了如何实现一个通用RepositoryFactory?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个通用信息库。这是我走到这一步......

I'm trying to implement a Generic Repository. This is what I've got so far ...

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

public class EntityFrameworkRepositoryFactory : IRepositoryFactory
{
    private readonly IWindsorContainer _container;

    public EntityFrameworkRepositoryFactory(IWindsorContainer container)
    {
        _container = container;
    }

    public IRepository<T> RepositoryOf<T>() where T : class
    {
        var repository = _container.Resolve<IRepository<T>>();
        return repository;
    }
}



该RepositoryFactory使用我的工作的实施单位

The RepositoryFactory is used by my unit of work implementation

public interface IUnitOfWork : IDisposable
{
    IRepository<T> RepositoryOf<T>() where T : class;
    void Commit();
}



不管怎样,我要问的问题是,是否具有RepositoryFactory实现依赖于IWindsorContainer是正确的?

Anyway, the question I want to ask is whether having the RepositoryFactory implementation depend on IWindsorContainer is correct?

我需要寻求任何类型的IRepository的一种方式,所以我的安装程序代码做这个...

I needed a way of asking for an IRepository of any type, so my installer code does this ...

// Windsor Container
container.Register(
    Component.For<IWindsorContainer>()
        .Named("Container")
        .Instance(container)
    );



这似乎只是违背国际奥委会的整体概念,但随后可能要求的整体思路一个仓库这是否反正。

Which just seems to go against the whole concept of IoC, but then maybe the whole idea of asking for a repository does that anyway.

修改(如回复 miensol 的答案)

Edit (As reply to miensol's answer)

我已经使用温莎在我的安装程序创建与下面的代码为我的仓库...

I am already using Windsor to create the repositories for me with the following code in my installer ...

// Generic Repository
container.Register(
    Component.For(typeof (IRepository<>))
        .ImplementedBy(typeof (EntityFrameworkRepository<>))
        .ServiceOverrides(
            ServiceOverride.ForKey("objectContext").Eq("ObjectContext"))
    );



我用的ServiceLocator过去以达到我想要的,但看过,这是一个有点反模式。所以,试图避免使用它。虽然我不得不承认,我不知道为什么,因为什么我也做似乎错了,因为我必须使用温莎城堡作为我的IoC / DI框架。服务定位,就是要架构无关。

I have used ServiceLocator in the past to achieve what I want, but have read that it's a bit of an anti-pattern. So was trying to avoid using it. Although I have to admit that I'm not sure why, as what I've done also seems wrong as I am bound to using Castle Windsor as my IoC/DI framework. Service Locator is meant to be framework agnostic.

所以,我有点糊涂了!

推荐答案

我不完全知道为什么你需要 IRepositoryFactory 当您使用IoC框架。但是有依赖散具体IoC容器的实现,虽然代码库通常不是一个好主意。大多数的时候,我真的不能找到一种方法,以使容器注入依赖于我的对象我用Service Locator模式,的这里你可以找到.NET常用的实现。那么你的工厂方法是这样的:

I'm not entirely sure why do you need IRepositoryFactory when you are using an IoC framework. However having dependencies to specific IoC container implementations scattered though the code base is generally not a good idea. Most of the time when I really can't find a way to make the container inject dependencies to my objects I use Service Locator Pattern, here you can find a commonly used implementation for .net. Then your factory method would look like this:

public IRepository<T> RepositoryOf<T>() where T : class
{
  return ServiceLocator.Current.GetInstance<IRepository<T>>();
}



不过好像你可以只让温莎创建通用信息库,你反正

Nevertheless it seems like you could just make Windsor create the generic repository for you anyways:

container.Register(
  Component.For(typeof(IRepository<>)).ImplementedBy(typeof(GenericRepositoryImplementation<>))
);

和让他们注入到你的对象,像这样:

and having them injected to your objects like so:

public class ClassThatRequiresSomeRepos
{
  IRepository<OneEntity> repoOne;
  IRepository<TwoEntity> repoTwo;

  public ClassThatRequiresSomeRepos(IRepository<OneEntity> oneEntityRepository, IRepository<TwoEntity> twoEntityRepository)
  {
    _repoOne = oneEntityRepository;
    _repoTwo = twoEntityRepository;
  }
} 

这篇关于如何实现一个通用RepositoryFactory?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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