Featured post
c# - How to implement a generic RepositoryFactory? -
i'm trying implement generic repository. i've got far ...
public interface irepositoryfactory { irepository<t> repositoryof<t>() t : class; } public class entityframeworkrepositoryfactory : irepositoryfactory { private readonly iwindsorcontainer _container; public entityframeworkrepositoryfactory(iwindsorcontainer container) { _container = container; } public irepository<t> repositoryof<t>() t : class { var repository = _container.resolve<irepository<t>>(); return repository; } }
the repositoryfactory used unit of work implementation
public interface iunitofwork : idisposable { irepository<t> repositoryof<t>() t : class; void commit(); }
anyway, question want ask whether having repositoryfactory implementation depend on iwindsorcontainer correct?
i needed way of asking irepository of type, installer code ...
// windsor container container.register( component.for<iwindsorcontainer>() .named("container") .instance(container) );
which seems go against whole concept of ioc, maybe whole idea of asking repository anyway.
edit (as reply miensol's answer)
i using windsor create repositories me following code in installer ...
// generic repository container.register( component.for(typeof (irepository<>)) .implementedby(typeof (entityframeworkrepository<>)) .serviceoverrides( serviceoverride.forkey("objectcontext").eq("objectcontext")) );
i have used servicelocator in past achieve want, have read it's bit of anti-pattern. trying avoid using it. although have admit i'm not sure why, i've done seems wrong bound using castle windsor ioc/di framework. service locator meant framework agnostic.
so, i'm bit confused!
i'm not entirely sure why need irepositoryfactory
when using ioc framework. having dependencies specific ioc container implementations scattered though code base not idea. of time when really can't find way make container inject dependencies objects use service locator pattern, here can find commonly used implementation .net. factory method this:
public irepository<t> repositoryof<t>() t : class { return servicelocator.current.getinstance<irepository<t>>(); }
nevertheless seems make windsor create generic repository anyways:
container.register( component.for(typeof(irepository<>)).implementedby(typeof(genericrepositoryimplementation<>)) );
and having them injected objects so:
public class classthatrequiressomerepos { irepository<oneentity> repoone; irepository<twoentity> repotwo; public classthatrequiressomerepos(irepository<oneentity> oneentityrepository, irepository<twoentity> twoentityrepository) { _repoone = oneentityrepository; _repotwo = twoentityrepository; } }
- Get link
- X
- Other Apps
Comments
Post a Comment