-
Notifications
You must be signed in to change notification settings - Fork 2
Object instantiation
Object defined with FluentApplicationContext can be instantiated in four different ways.
Most obvious and default way to instantiate object is to call class constructor. This instantiation mode is used if UseConstructor() or BindConstructorArg() methods are used, or if no other instantiation mode is specified. Both constructor injection methods are described in 6. Constructor Injection section.
The UseStaticFactoryMethod() method allows to instantiate object by calling specified static method. The following example shows usage of this method:
class ButtonFactory
{
public static ButtonFactory CreateInstance() { /*...*/ }
/*...*/
}
// Configuration
ctx.RegisterDefault<ButtonFactory>().UseStaticFactoryMethod(ButtonFactory.CreateInstance);
// Instantiation
ButtonFactory factory = ctx.GetObject<ButtonFactory>();
The UseFactoryMethod() method allows to instantiate object by calling specified method of other registered object:
ctx.RegisterDefault<ButtonFactory>().UseStaticFactoryMethod(ButtonFactory.CreateInstance);
ctx.RegisterDefault<Button>()
.UseFactoryMethod<ButtonFactory>(f => f.CreateButton()).OfRegisteredDefault();
Button button = ctx.GetObject<Button>();
The example above shows usage of UseFactoryMethod(). It specifies type of class and its method used to instantiate defined object. The factory object is specified by OfRegisteredDefault() method, which refers to ButtonFactory registered by RegisterDefault() method. It is possible also to specify factory object by using OfRegistered() method that accepts object id or reference.
Both of described methods have overloads that allows to use factory method accepting up to 3 parameters, which can be configured in the same way as constructor arguments presented in 6. Constructor Injection section.
Here is an example of using factory methods with parameters:
enum ButtonType { Rectangular, Round }
class ButtonFactory
{
public static ButtonFactory CreateInstance(ButtonType type) { /*...*/ }
public Button CreateButton(string text, bool isDefault) { /*...*/ }
}
// Configuration
ctx.RegisterDefault<ButtonFactory>()
.UseStaticFactoryMethod((ButtonType type) => ButtonFactory.CreateInstance(type))
.BindMethodArg().ToValue(ButtonType.Round);
var closeBt = ctx.RegisterUniquelyNamed<Button>()
.UseFactoryMethod<ButtonFactory, string, bool>((f, text, isDefault) => f.CreateButton(text, isDefault))
.OfRegisteredDefault()
.BindMethodArg().ToValue("Close")
.BindMethodArg().ToValue(false)
.GetReference();
var saveBt = ctx.RegisterUniquelyNamed<Button>()
.UseFactoryMethod<ButtonFactory, string, bool>((f, text, isDefault) => f.CreateButton(text, isDefault))
.OfRegisteredDefault()
.BindMethodArg().ToValue("Save")
.BindMethodArg().ToValue(true)
.GetReference();
Last method of object instantiation refers to RegisterXXXSingleton() methods (described in 3. Object definition registration section). In this mode, object is already created and initialized before its registration in context, so each request of this object ends with returing of registered instance.
Please note that except last instantiation method, it is possible to configure additional object dependencies using property injection or autowiring. Please note also that only instantiation by constructor supports lookup methods injection
Continue reading: 9. Indirect dependencies