I have an interface IRepository and an implementation EFRepository.
I use structuremap injection in order to get the repository implementation.
Right now the EFRepository has constructor with no parameter so structuremap knows to retrieve instances on EFRepository easily.
Now I need to change the repository implementation so that it will recieve in the constructor parameter that holds the unit of work.
My question in such case, how I use structuremap in order to return an instance that initialized with the unit of work?
EXAMPLE
Until today I used:
using(IUnitOfWork uow=UnitOfWork.current) {
IRepository rep = ObjectFactory.GetInstance<IRepository<T>>();
//repository operations that uses UnitOfWork.current that initialized above
}// here dispose of UnitOfWork.current
Now I want to use:
using(IUnitOfWork uow=new UnitOfWork()) {
//Not sure is this is how I tell sructure map to use contractor that
//get IUnitOfWork)
IRepository rep = ObjectFactory.GetInstance<IRepository<T>>(uow);
//repository operations that uses uow that initialized above
}// here dispose of UnitOfWork
I’m assuming here that your unit of work is request-specific… So you have a service (WCF?) and each incoming request gets its own unit of work.
Then you can configure StructureMap to define a separate unit of work per HTTP Request. If you need unit testing without HTTP Requests, you can choose for a hybrid lifecycle: per HTTP Request OR per thread. StructureMap will figure out what to do at runtime.
Your class that needs the constructor injection simply states that it needs an IUnitOfWork:
This of course requires that MyClass is also managed/instantiated using StructureMap.