-
Notifications
You must be signed in to change notification settings - Fork 10
Resolvers
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 anIEnumerable
or array. -
LazyResolver
: It can resolve services registered into the container wrapped by aLazy<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:
ContainerResolver
EnumerableResolver
LazyResolver
- Custom, external resolvers
- Parent container resolvers
- 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