How to implement a generic 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;
}
}
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 Winds开发者_JS百科or 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!
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;
}
}
精彩评论