Default Constructors
A Default called a constructor with no parameters. It could be explicitly defined on a Type or if Type has no defined constructors, C# will implicitly create one for you.
Invoking Default Constructor
To configure resolution of a Type with a default constructor you need to register that Type with Injection Constructor Member which takes no parameters.
Class Service
Consider the following Type:
public class Service : IService
{
public Service() => Ctor = 1;
public Service(object arg) => Ctor = 2;
public Service(IUnityContainer container) => Ctor = 3;
public int Ctor { get; } // Constructor called
}
Class Service
is a plain type with three accessible constructors. First constructor is a default constructor with no parameters, second and third constructors take one parameter each.
Registering Service
If you try to resolve this class with no registration, Unity will select one of the constructors with parameter. You can not guarantee which one it will select because both have the same number of parameters and each could be satisfied with dependencies.
Tip
If Diagnostic is enabled, it will throw an ambiguous constructor exception.
To prevent ambiguity, or if you need to execute default constructor, you can register this Type and instruct the container to invoke default constructor during resolution. In simplest form this registration will look like this:
Container.RegisterType<Service>(Invoke.Constructor());
Note
Of corse you could add other instructions like mapping, name, etc. to the registration but for simplicity it is omitted in this example.
Resolving Service
Once you register the Service
with the container, you can resolve it normally:
var instance = Container.Resolve<Service>();
// 1 == instance.Ctor
At first resolution the container will create a pipeline which invokes a default constructor to create the Service
and will be using it for all subsequent resolutions.
Default Generic Constructor
Unity can register and create Generic types. It allows to register Closed and Open Generics and can resolve constructed types based on these.
Class Service<T>
The principle for registering of default constructor is exactly the same as for plain types. Consider the following Type:
public class Service<T>
{
public Service() => Ctor = 1;
public Service(T arg) => Ctor = 2;
public int Ctor { get; } // Constructor called
}
Class Service<T>
is an open generic type with two constructors. First constructor is a default constructor with no parameters and second takes one parameter.
Registering Service<T>
Normally, Unity will create this Type by executing most complex constructor. To force Unity to use default constructor you need to register Service<T>
and instruct the container to invoke it during resolution. You can register constructed generic based on Service<T>
like this:
Container.RegisterType<Service<object>>(Invoke.Constructor());
Or you can register Open Generic Type:
Container.RegisterType(typeof(Service<>), Invoke.Constructor());
Resolving Service<T>
If you resolve Service<object>
:
var instance = Container.Resolve<Service<object>>();
// 1 == instance.Ctor
either registration will invoke the default constructor.