I am new to StructureMap and have the following issue which seems to be the reverse of issues other people are having. I have a generic UnitOfWorkClass
defined as follows:
public class UnitOfWork<TContext> : IUnitOfWork
where TContext : IContext, new()
{
....
}
However I'm baffled about how you'd register this to resolve if I've got several different possibilities for the concrete instance.
I've seen plenty of examples where people have a generic interface such as:
StructureMap Auto registration for generic types using Scan
So I guess the question could be asked if I need to re-look at my design too!
Thanks a lot!
# EDIT POSSIBLE SOLUTION?So I have a class which takes two parameters into the constructor:
public MyClass(IUnitOfWork firstConstructor,
IUnitOfWork secondConstructor)
{
...
}
Then as my object graph gets built I do the following:
ObjectFactory.Initialize(cfg =>
{
var firstContext = cfg.For<IContext>().Use<MyFirstContext>();
var secondContext = cfg.For<IContext>().Use<MySecondContext>();
var firstUnitOfWork =
cfg.For<IUnitOfWork>().Use<UnitOfWork<MyFirstContext>>()
.Ctor<IContext>().Is(firstContext);
var secondUnitOfWork =
cfg.For<IUnitOfWork>().Use<UnitOfWork<MySecondContext>>()
.Ctor<IContext>().Is(secondContext);
cfg.For<IMyClass>().Use<MySecondContext>()
.Ctor<IUnitOfWork>("firstConstructor").Is(firstUnitOfWork)
.Ctor<IUnitOfWork>("secondConstructor").Is(secondUnitOfWork);
});
So what I've done here is:
- Resolved my dependency for IContext and stored them in variables
- Injected the resolved IContext into my IUnitOfWork constructor
- I can then resolve the correct generic UnitOfWork constructor correctly
It doesn't feel perfect but I think this suits my needs...
TContext
type parameter? Either you must supply that type by registering a closed version of it explicitly, or you make the interface generic. The choice is up to you. – StevenIFirstUnitOfWork
,ISecondUnitOfWork
. – Steven