1
我有這種類型與一個IFoo
注入:StructureMap中的優先級 - 對於<T>或AddRegistry <T>?
class MainThing
{
public MainThing(IFoo foo)
{
Console.WriteLine(foo.Name);
}
}
...和這個接口:
interface IFoo
{
string Name { get; }
}
...和IFoo
這兩種實現方式:
class Foo1 : IFoo
{
public string Name { get { return @"Foo1"; } }
}
class Foo2 : IFoo
{
public string Name { get { return @"Foo2"; } }
}
...我有這個StructureMap註冊:
var c = new Container(e =>
{
e.AddRegistry<Registry1>();
e.AddRegistry<Registry2>();
e.For<IFoo>().Use<Foo1>();
});
class Registry1 : Registry
{
public Registry1() { For<IFoo>().Use<Foo1>(); }
}
class Registry2 : Registry
{
public Registry2() { For<IFoo>().Use<Foo2>(); }
}
當我打電話c.GetInstance<MainThing>();
,我期待MainThing
給予一個Foo1
例如,但它給一個Foo2
實例。
該文檔說明後續的AddRegistry
調用將覆蓋在先前的註冊表中聲明的默認實例。我認爲,因爲我在最後附加了一個.Use<Foo1>
,它勝過了之前的一切,但事實並非如此。
這裏的WhatDoIHave()
:
===========================================================================================================
Configuration Sources:
0) Registry: StructureMap.ConfigurationExpression, StructureMap, Version=2.6.4.0, Culture=neutral, PublicKeyToken=e60ad81abae3c223
1) Registry: ConsoleApplication13.Registry1, ConsoleApplication13, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
2) Registry: ConsoleApplication13.Registry2, ConsoleApplication13, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
3) Registry: StructureMap.Configuration.DSL.Registry, StructureMap, Version=2.6.4.0, Culture=neutral, PublicKeyToken=e60ad81abae3c223
===============================================================================================================================================================================================================
PluginType Name Description
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Func`1<TResult> (Func`1<TResult>)
Scoped as: Transient
478e77e8-ff84-47e2-bdc1-b90bac2e7b61
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IFoo (ConsoleApplication13.IFoo) f10f7750-f009-4842-ae35-7af47826ac2b Configured Instance of ConsoleApplication13.Foo2, ConsoleApplication13, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Scoped as: Transient
9bd57a90-9c1e-4674-b249-7e6e96b40945 Configured Instance of ConsoleApplication13.Foo1, ConsoleApplication13, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
3e14df0c-da47-40e2-96ed-9bc6027a5da8 Configured Instance of ConsoleApplication13.Foo1, ConsoleApplication13, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
f10f7750-f009-4842-ae35-7af47826ac2b Configured Instance of ConsoleApplication13.Foo2, ConsoleApplication13, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IContainer (StructureMap.IContainer) cb17947d-5814-4c9b-a219-f359333d6fdb Object: StructureMap.Container
Scoped as: Transient
cb17947d-5814-4c9b-a219-f359333d6fdb Object: StructureMap.Container
===============================================================================================================================================================================================================
Foo2
我可以看到有中IFoo
3個實例和Foo2
是默認的,但我不知道爲什麼。
StructureMap的AddRegistry
優先於For
嗎?