Skip to content

Object configuration steps

Wojtek edited this page Feb 7, 2019 · 13 revisions

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

  • if given step offers different options, it is possible to select only one of them,
  • it is possible to skip given step(s),
  • 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 creation steps are listed below ( please note however that Table of Contents describes functionality ordered from basic to advanced ones, while following steps are grouping it logically ):

  1. Definition registration *
  2. Scope definition
  3. Indirect dependencies definition
  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 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();

The following 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);

Continue reading: 3. Object definition registration

Clone this wiki locally