Skip to main content

Special resolution cases

Unknown type resolution

When this feature is enabled, the container will try to resolve unregistered types by registering them using a pre-defined configuration delegate.

Without a registration configuration, the container can resolve only non-interface and non-abstract unknown types. In the following example, the container creates an implicit registration for Dependency and injects its instance into Service.

class Dependency { }

class Service
{
public Service(Dependency dependency)
{ }
}

var container = new StashboxContainer(config => config
.WithUnknownTypeResolution());

container.Register<Service>();

var service = container.Resolve<Service>();

Default value injection

When this feature is enabled, the container will resolve unknown primitive dependencies with their default value.

class Person 
{
public Person(string name, int age) { }
}

var container = new StashboxContainer(config => config
.WithDefaultValueInjection());
// the name parameter will be null and the age will be 0.
var person = container.Resolve<Person>();
note

Unknown reference types are resolved to null only in properties and fields.

Optional value injection

Stashbox respects the optional value of each constructor and method argument.

class Person 
{
public Person(string name = null, int age = 54, IContact contact = null) { }
}

// the name will be null
// the age will be 54.
// the contact will be null.
var person = container.Resolve<Person>();