在我的应用程序中,我注意到如果我将SM注册表中的类标记为Singleton类型,它将被处理掉,但是如果不指定任何Singleton它不会被处理掉。 这是为什么?为什么在StructureMap中处理Singleton类而瞬变不是
public class IoC
{
public static IContainer Init()
{
var container = new Container(x =>
{
x.Scan(s => {
s.TheCallingAssembly();
s.AssembliesFromApplicationBaseDirectory();
s.WithDefaultConventions();
});
// disposed is called on this class but not if .Singleton() is removed
x.For<IMyService>().Singleton();
});
return container;
}
}
class Program
{
static void Main(string[] args)
{
using (var container = IoC.Init())
{
var theStory1 = container.GetInstance<MyService>();
theStory1.TheMethod();
}
}
}
我认为这是有道理的,但我注意到嵌套容器**做**处置。所以我现在想知道为什么StructureMap可以跟踪嵌套容器中的瞬态对象而不是根容器? – user183872 2014-12-11 09:49:06
@ user183872 - 我忘了提及嵌套容器。 Jeremy D. Miller在这里解释了为什么嵌套容器是这样设计的以及如何使用它 - http://structuremap.github.io/the-container/nested-containers/ – LetMeCodeThis 2014-12-11 11:17:31