我要创建在code进行Structuremap命名实例,没有配置文件
I want to create a Structuremap named instance in code, without config file
我希望能够创建实例是这样的:
I want to be able to create the instance like this:
var namedInjector = ObjectFactory.GetNamedInstance<IInjectable>("Other");
我不能定义code这样的类型。我发现this样品 但它使用的previous版本旧的语法和命名实例定义为:
I cant define such a type in code. I have found this sample but it uses the old syntax of a previous version and defines the named instance as:
.ForRequestedType<MementoType>()
.AddConcreteType<ConcreteType>(instanceName)
在最新structuremap版本没有.AddConcreteType(实例名)方法,它需要一个实例的名称。
In the latest structuremap version there is no .AddConcreteType(instanceName) method which takes a instance name.
我相信你需要这样的:
class MyRegistry : Registry {
public MyRegistry() {
this.ForRequestedType<IFoo>()
.TheDefaultIsConcreteType<Bar>()
.AddInstances( x => {
x.OfConcreteType<Blap>().WithName("abc");
});
}
}
...
ObjectFactory.Configure(x=>x.AddRegistry<MyRegistry>());
IFoo test1 = ObjectFactory.GetInstance<IFoo>(); // Bar
IFoo test2 = ObjectFactory.GetNamedInstance<IFoo>("abc"); // Blap
...
interface IFoo {}
public class Bar : IFoo {}
public class Blap : IFoo {}