StructureMap单例
这两个相等吗?
1) var store = new DocumentStore();
For<IDocumentStore>().Use(store);
2) var store = new DocumentStore();
For<IDocumentStore>().Singleton().Use(store);
或者
For< IDocumentStore>().AlwaysUnique().Use(store);
这两个都会返回没有重复实例的文档存储的单例实例?
Are these two equivalent?
1) var store = new DocumentStore();
For<IDocumentStore>().Use(store);
2) var store = new DocumentStore();
For<IDocumentStore>().Singleton().Use(store);
or
For< IDocumentStore>().AlwaysUnique().Use(store);
Will both of these return singleton instance of documentstore with no duplicate instances?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AlwaysUnique() 做相反的事情,总是创建一个唯一的(新的)实例,与 singleton 有点相反。
请参阅 这篇 stackoverflow 帖子 了解如何在两个之间共享单例接口。
Singelton() 创建单例。在您的示例 IDocumentStore 中,它是此接口的容器的 Singelton。
(编辑):它实际上是为此容器瞬时创建的对象的单例。请用谷歌搜索该术语和结构图。一般来说,这些是自动创建并注入到类中的对象,但我还没有看到它的确切定义。
AlwaysUnique() does the opposite and always creates a unique(new) instance, kind of opposite to singelton.
See this stackoverflow post to see how to share singeltons between two interfaces.
Singelton() creates the singelton. It is a Singelton for this container for this interface, in your example IDocumentStore.
(edit): it is actually a singelton for transiently created objects for this container. Please google that term together with structure map . Generally these are the objects that are created automatically and injected into classes, but I have not seen an exact definition of this.
当您提供实例而不只是类型时,您将始终获得单例行为。
You will always get singleton behavior when you provide an instance instead of just a type.