When Controller1 is being created, I want that IService will be mapped to ConcreteService1 and IPageService to ConcretePageService1
And when Controller2 is created, I want that IService will be mapped to ConcreteService2 and IPageService to ConcretePageService2
How I can initialize ObjectFactory so that the above will work?
Mean while I initialezed ObjectFactory this way:
ObjectFactory.Initialize(x =>
{
x.For<IService>().Use<ConcreteService1>();
x.For<IPageService>().Use<ConcretePageService1>();
});
But this ALWAYS maps ConcreteService1 to IService and ConcretePageService1 to IPageService regardless of controller type
public class Controller1 : Controller
{
public Controller1(IService service, IPageService pageService)
{
}
}
public class Controller2 : Controller
{
public Controller2(IService service, IPageService pageService)
{
}
}
public interface IService
{
}
public class ConcreteService1:IService
{
}
public class ConcreteService2:IService
{
}
public interface IPageService
{
}
public class ConcretePageService1:IPageService
{
}
public class ConcretePageService2:IPageService
{
}
Look into the ConstructedBy() syntax for StructureMap.
It's on this page, but I'm not sure if these are the latest docs for SM:
http://structuremap.net/structuremap/InstanceExpression.htm#section18
I might be wrong about ConsructedBy--the docs aren't very good. Look at the following for a similar question:
StructureMap Conditional use
If it's just an isolated registration you can use named instances to map a specific instance to each controller.
If this is a pattern that's repeated in the application you should use a convention to map the types in order to avoid all this duplication.
Adding types named by type name is possible using a built in convention.