A Resolver should be created for any plugin type. Resolvers are the standard way to retrieve/create/register plugin types.
As an example, we'll create a resolver to resolve an application error logger:
All you need to do is inherit from Umbraco.Core.ObjectResolution.SingleObjectResolverBase<TResolver, TResolved>
and then add whatever constructors, properties and methods you would like to expose.
In the example above we have a constructor that accepts a default IErrorLogger
. Normally in Umbraco this resolver will be constructed in a IBootManager
with a default object. The we expose a method to allow developers to change to a custom IErrorLogger
at runtime called SetErrorLogger
. Then we create a property to expose the IErrorLogger
called ErrorLogger.
Example:
Creating a multiple object resolver is similar. As an example we'll create a LanguageConvertersResolver.
The naming convention for multiple objects resolvers are plural: We've named this LanguageConvertersResolver with a pluralized 'Converters' to denote that this resolver returns multiple objects
When creating a multiple object resolver you need to decide what lifetime scope the objects created and returned will have which is defined in the constructor created. The default constructor of the ManyObjectsResolverBase
specifies that the objects created will have an Application based lifetime scope which means the objects will be singletons only one instance of each one will exist for the lifetime of the application. There are 3 lifetime scopes that can be specified:
ObjectLifetimeScope.Application
One instance of each object will be created for the entire lifetime of the application (singleton)
ObjectLifetimeScope.Transient
A new instance of each object will be created each time the 'Values' collection is accessed
ObjectLifetimeScope.HttpRequest
One instance of each object will be created for the lifetime of the current http request