Skip to content

Object configuration steps

Suremaker edited this page Dec 10, 2012 · 13 revisions

The Spring.FluentContext allows to construct object definition with following steps:

  1. Definition registration *
  2. Scope definition
  3. [Indirect dependencies definition](Indirect object dependencies)
  4. Object Instantiation
  5. Lookup Method Injection **
  6. Autowiring
  7. Dependency Injection
  8. Initialization Behavior
  9. Finalization Behavior
  10. Dependency checking
  11. Definition referencing

* If RegisterXXXSingleton() method group is used, 2-10 construction steps would be skipped.

** This step is only accessible if object instantiation is not explicitly specified or is specified by using BindConstructorArg(), UseConstructor()

The API is constructed in this way, that:

  • if given step offers different options, it is possible to select only one of them,
  • it is possible to skip given step, if it is meaningless for given configuration case
  • it is not possible to go back to previous construction step.

Described functionality dramatically decreases chances of creating inconsistent configuration and allows to focus on putting only meaningful configuration.

The following example shows full configuration of object (which does not skip any steps):

var factoryRef = ctx.RegisterDefault<HouseFactory>()
    .AsSingleton()
    .DependingOnDefault<OtherFactory>()
    .UseConstructor((IConcreteProvider concreteProvider, int workersCount) => new HouseFactory(concreteProvider, workersCount))
        .BindConstructorArg().ToRegisteredDefault()
        .BindConstructorArg().ToValue(20)
    .BindLookupMethod(factory => factory.CreateWorkPlace())
        .ToRegisteredDefaultOf<SecuredWorkplace>()
    .Autowire(AutoWiringMode.ByType)
    .BindProperty(factory => factory.Name)
        .ToValue("My Factory")
    .CallOnInit(factory => factory.Initialize())
    .CallOnDestroy(factory => factory.Cleanup())
    .CheckDependencies()
    .GetReference();

This example shows configuration of the other objects, where only meaningful steps are being used:

ctx.RegisterDefault<ButtonFactory>().UseStaticFactoryMethod(ButtonFactory.CreateInstance);

var closeBt = ctx.RegisterUniquelyNamed<Button>()
	.UseFactoryMethod<ButtonFactory>(f => f.CreateButton()).OfRegisteredDefault()
	.BindProperty(b => b.Name).ToValue("Close")
	.GetReference();

var saveBt = ctx.RegisterUniquelyNamed<Button>()
	.UseFactoryMethod<ButtonFactory>(f => f.CreateButton()).OfRegisteredDefault()
	.BindProperty(b => b.Name).ToValue("Save")
	.GetReference();

ctx.RegisterDefault<Window>()
	.BindProperty(w => w.CloseButton).ToRegistered(closeBt)
	.BindProperty(w => w.SaveButton).ToRegistered(saveBt);
Clone this wiki locally