2011-07-21 91 views
3

这两个等价吗?StructureMap singleton

1)var store = new DocumentStore();

 For<IDocumentStore>().Use(store); 

2)var store = new DocumentStore();

 For<IDocumentStore>().Singleton().Use(store); 

 For< IDocumentStore>().AlwaysUnique().Use(store); 

都将这些返回documentstore的单一实例,没有重复的实例?

回答

3

当你提供一个实例而不仅仅是一个类型时,你总会得到单例行为。

5

AlwaysUnique()完全相反,并且始终创建一个与singelton相反的独特(新)实例。 请参阅this stackoverflow post以了解如何在两个界面之间共享singelton。

Singelton()创建singelton。它是这个接口的容器的Singelton,在你的示例IDocumentStore中。 (编辑):它实际上是用于此容器的临时创建对象的singelton。请结合结构图与Google一起使用该术语。通常这些是自动创建并注入到类中的对象,但我没有看到它的确切定义。