4

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?

Chandu
  • 81,493
  • 19
  • 133
  • 134
bkhanal
  • 1,400
  • 3
  • 16
  • 24

2 Answers2

6

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.

Community
  • 1
  • 1
Gregor
  • 358
  • 2
  • 7
3

You will always get singleton behavior when you provide an instance instead of just a type.

Joshua Flanagan
  • 8,527
  • 2
  • 31
  • 40