Structuremap - implementation of several interfaces

I am completely new to Structuremap and confused about how to connect an interface that has several implementations.

Suppose I have Controller1 and Controller2 . I have Interface1 , which is implemented by two separate classes, Class1ForInterface1 and Class2ForInterface1 . In Controller1 I want to enter Class1ForInterface1 and in Controller2 I want Class2ForInterface1 insert Class2ForInterface1 .

How to do this using structure? It seems that I can have only one interface mapping for a particular type?

Thanks!

+8
asp.net-mvc-3 structuremap
source share
1 answer

It is possible that several classes implement the same interface with the structure structure.

If you name your mappings, you can later load them with that name.

 For<MyInterface>().Add<Class1ForMyInterface>().Named("Class1"); For<MyInterface>().Add<Class2ForMyInterface>().Named("Class2"); 

If you want your Class1ForMyInterface you can call

 container.GetInstance<MyInterface>("Class1"); 

There are also several ways to display all this in your container, and

 For<IController>().Add<Controller1>().Ctor<MyInterface>().Is(i => i.GetInstance<MyInterface>("Class1")); 

Or, if you save the smartinsatance property, which is returned when the type is registered, you can use it in the mapping.

 var class1 = For<MyInterface>().Add<Class1ForMyInterface>().Named("Class1"); For<IController>().Add<Controller1>().Ctor<MyInterface>().Is(class1); 
+10
source share

All Articles