Skip to content

Resolvers

Peter Csajtai edited this page Feb 11, 2016 · 29 revisions

Stashbox internally maintains a resolution graph between the registered services with a Resolver instance assigned to each of them.

For faster resolution it also maintains an internal Expression tree through all the dependencies.

Stashbox contains three pre-defined Resolvers:

  • ContainerResolver: It mainly uses the container itself for resolving dependencies.
  • EnumerableResolver: It resolves every registered type of a service as an IEnumerable or array.
  • LazyResolver: It can resolve services registered into the container wrapped by a Lazy<T>.

You can also specify a special rule to resolve services and extend the functionality of Stashbox. To achieve this you can register a custom Resolver implementation into Stashbox:

class MagicalWeaponResolver : Resolver
{
	 public MagicalWeaponResolver(IContainerContext containerContext, TypeInformation typeInfo)
            : base(containerContext, typeInfo)
        {
		}
		
	public override object Resolve(ResolutionInfo resolutionInfo)
    {
        //..
    }

    public override Expression GetExpression(ResolutionInfo resolutionInfo, Expression resolutionInfoExpression)
    {
        //..
    }
}

The TypeInformation parameters holds information about the type which was actually requested from the container.

The ResolutionInfo parameter holds information about the current resolution context (factory parameters, overrides, etc.)

The ResolutionInfoExpression parameter represents a ParameterExpression which will be passed as a parameter to the lambda expression built by the resolution graph.

##Resolver registration

container.RegisterResolver((context, typeInfo) => new MagicalWeaponResolver(context, typeInfo),
	(context, typeInfo) => Gauntlgrym.CanProduce(typeInfo));

The first parameter is a factory delegate for creating a resolver instance. The second parameter is a predicate delegate which can decide the actual type can be resolved by the resolver or not.

Stashbox will choose from the resolvers to accomplish the current resolution request in the following order:

  1. ContainerResolver
  2. EnumerableResolver
  3. LazyResolver
  4. Custom, external resolvers
  5. Parent container resolvers