How to implement a generic RepositoryFactory?
Asked Answered
M

1

8

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;
    }
}

The RepositoryFactory is used by my unit of work implementation

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

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

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.

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"))
    );

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.

So, I'm a bit confused!

Mcgean answered 21/12, 2010 at 15:46 Comment(4)
Added this is a reply, but it's more of a comment -- these might tangentially be useful (answered a similar question earlier today): [1]: #3675 [2]: mikehadlow.blogspot.com/2008/03/…Frogfish
Yes there is plenty of similar questions. I think i have answered about 3 of them..Sleigh
@Tim: Link #2 is broken.Pyroelectricity
Hmm .. will try again :) Thanks Daniel. mikehadlow.blogspot.com/2008/03/…Frogfish
L
5

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;
  }
} 
Loudspeaker answered 21/12, 2010 at 20:38 Comment(2)
This is the first thing that jumped into my head when I read this, good answer.Norine
please see the edit to my question. tbh, I am not sure if ServiceLocator is an anti-pattern or not. It seems fine to me as long as you limit the number of places it's called.Mcgean

© 2022 - 2024 — McMap. All rights reserved.