-
Notifications
You must be signed in to change notification settings - Fork 10
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 aLazy<>
. -
FuncResolver
: It can resolve services wrapped into aFunc<>
delegate. -
TupleResolver
: It can resolve services wrapped into aTuple<>
. -
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);
}
}
container.RegisterResolver(new MagicalWeaponResolver());
Stashbox will choose from the resolvers to accomplish the current resolution request in the following order:
EnumerableResolver
LazyResolver
FuncResolver
DefaultValueResolver
- Custom, external resolvers
ParentContainerResolver
UnknownTypeResolver
- Service registration
- Factory registration
- Assembly registration
- Composition
- Fluent registration api
- Service resolution
- Resolution by attributes
- Conventional resolution
- Delegate resolution
- Conditional resolution
- Multi resolution
- Lifetimes
- Generics
- Generic wrappers
- Decorators
- Resolvers
- Scopes
- Container configuration
- Container diagnostics
- Exceptions