Skip to content
Suremaker edited this page Dec 11, 2012 · 1 revision

Spring.NET allows to specify scope for object definitions, which determines how object should be instantiated. The Spring.FluentContext offers methods to set two most commonly used scopes: singleton or prototype.

The singleton scope means that each request for object will result with return of the same instance, while for prototype, each time the new instance of object will be returned.

The following example shows how to define scopes for object definitions.

ctx.RegisterDefault<Cat>().AsSingleton();
ctx.RegisterDefault<Stick>().AsPrototype();

var cat = ctx.GetObject<Cat>();
var sameCat = ctx.GetObject<Cat>();
var stick = ctx.GetObject<Stick>();
var otherStick = ctx.GetObject<Stick>();

In example above, cat and sameCat are the same instances, while stick and otherStick are different ones.

Please note that all definitions have singleton scope set by default.

Continue reading: 5. Setter Injection

Clone this wiki locally