Skip to content
This repository has been archived by the owner on Feb 6, 2021. It is now read-only.

Resolution

Guy Antoine edited this page Oct 30, 2020 · 5 revisions

Basic Resolve

Resolving a Type or Interface is easy. There are only a handful of overloads:

//Basic Resolve
container.Resolve<IBrewEquipment>();
//or
container.Resolve(typeof(IBrewEquipment));

Conditional / Named Resolve

To resolve a named registration, just specify it's key.

//Resolve Named
container.Resolve<IBrewEquipment>("plunger");
//or
container.Resolve(typeof(IBrewEquipment), "plunger");

In order to specify that a dependency should resolve a named registration you have two options.

Using RegisterExplicit

Register the calling type using a linq expression that specifies the type to resolve. The input variable c => gives access to the container and all its methods.

container.RegisterExplicit<CoffeeRobot, CoffeeRobot>(c => new CoffeeRobot(
	c.Resolve<IBrewEquipment>("plunger"), //<--
	c.Resolve<Water>(), 
	new Grinder()));

Using the ResolveNamed Attribute

If you have access to the code, you can decorate the constructor parameter with a ResolveNamed(string key) attribute.

public class CoffeeRobot
{
  //Constructor
  public CoffeeRobot(
    [ResolveNamed("plunger")] IBrewEquipment equipment, //<--
    Water water, 
    Grinder grinder)
  {
    //...
  }

📝 This technique will result in slightly better performance

Smart Resolve

📝 Default behaviour can be modified by configuring the Container Options

When 'Resolve' is called, if SmartDi can't find the registration in the container, it will attempt to create an instance. This is possible as long as it can resolve the object's dependencies, it will successfully resolve the type. Once resolved, the type will be registered to cache it and improve performance. As such the performance is almost as-good as pre-registering. Smart-Resolve applies the following logic:

  • If the Resolved-Type is an interface or abstract class, it will be registered as a Singleton
  • Otherwise it will be registereed as a Transient and a new instance will be created each time the type is resolved

⚠️ This convenience enables rapid prototyping, but comes with its risks. Bugs may be harder to trace. For enterprise applications, its recommended to turn this feature off in the Container Options