Skip to content

Resolvers

Peter Csajtai edited this page May 3, 2017 · 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 pre-defined Resolvers:

  • EnumerableResolver: It resolves every registered type of a service as a collection type.
  • LazyResolver: It can resolve services wrapped by a Lazy<>.
  • FuncResolver: It can resolve services wrapped into a Func<> delegate.
  • TupleResolver: It can resolve services wrapped into a Tuple<>.
  • DefaultValueResolver: It can resolve optional and default values.
  • UnknownTypeResolver: It can resolve services which are not registered into the container.
  • ParentContainerResolver: It can resolve services which are not registered into the container but they can be found in the parent.

You can also specify a special rule to resolve services and extend the functionality of Stashbox by registering a custom Resolver implementation into it:

class MagicalWeaponResolver : Resolver
{
    public override Expression GetExpression(IContainerContext containerContext, TypeInformation typeInfo, ResolutionInfo resolutionInfo)
    {
        //Resolution expression generation
    }

    public override bool SupportsMany => ...; //Should return true, if the resolver can be used for enumerable argument resolution e.g. the enumerable
    										  //argument is a wrapped generic type which can be resolved by this resolver, otherwise false.

    public override Expression[] GetExpressions(IContainerContext containerContext, TypeInformation typeInfo, ResolutionInfo resolutionInfo)
    {
    	//If the SupportsMany property is true, the EnumerableResolver will call this method to collect the expressions of the enumerable items.
    }
	
    public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo)
    {
	//Returns the predicate which indicates the resolver can be used to activate the requested service or not.
        return Gauntlgrym.CanProduce(typeInfo);
    }
}

Resolver registration

container.RegisterResolver(new MagicalWeaponResolver());

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

  1. EnumerableResolver
  2. LazyResolver
  3. FuncResolver
  4. DefaultValueResolver
  5. Custom, external resolvers
  6. ParentContainerResolver
  7. UnknownTypeResolver