1. Introduction
Before catel2.0, the IOC container used unity internally. However, this forces all users to use unity as the IOC container in their applications, so they also need to divide the library, since 2.0, a different technology has been used, which allows developers to use their own hanging IOC container technology.
1.1 Different components in IOC
There are many different components in catel and they are very important to IOC.
Servicelocator
This component is used to register all types of IOC containers.
Typefactory
This component is used to create components of the type. iservicelocator is used to obtain the type that requires vision.
Dependencyresolver
The lightweight Implementation of iservicelocator. This does not expose the registration method, but only allows the resolve type.
1.2 obtain components from any object
In each object, you can use the default attribute to obtain the instance of each component. This may cause problems when different scoping is used. You must determine the correct object for your work, use the following extension code.
using Catel.IoC; // Contains ObjectExtensions which allow use of below extension methods public class MyService{ public void SomeMethod() { // If you need to create a type with the current scope type factory var typeFactory = this.GetTypeFactory(); // If you need to register a type with the current scope service locator var serviceLocator = this.GetServiceLocator(); // If you need to resolve a type with the current scope and the type is not injected via dependency injection var dependencyResolver = this.GetDependencyResolver(); }}
2. IOC component description 2.1 servicelocator description
Servicelocator serves as a service container in catel.
The typefactory is used internally as the initialization of the service.
Catel uses it's ownServicelocatorImplementingIservicelocatorTo gather all services required by catel.
For example, ipleasewaitservice and iuivisualizerservice are default services. Normally, when the first view model is in the initial state, catel has registered all the default services that can be used immediately, this allows developers to register their own implemented services before the view model is initialized (for example, the application starts the service ).
Servicelocator can be instantiated, but catel will instantiate a process and share all objects in the same appdomain. servicelocator can be obtained through servicelocator.
Note: If you want to know how the type is initialized and the dependency is injected, you can view the description of typefactory.
2.2 register a type
Use the following code to register a specified type to servicelocator
ServiceLocator.Default.RegisterInstance<IPleaseWaitService>(pleaseWaitService);
2.3 Register a late binding type
ServiceLocator.Default.RegisterType<IPleaseWaitService>(x => new PleaseWaitService());
2.4 Register an instance of the type
When the object is resolve for the first time, catel uses typefactory or activator. createinstance to create an instance, but sometimes a service constructor requires parameters or a long time to construct. In this case, the recommender creates a type and registers an instance.
var pleaseWaitService = new PleaseWaitService();ServiceLocator.Default.RegisterInstance<IPleaseWaitService>(pleaseWaitService);
2.5 use the misstype event to register a type
When a type is not registered to servicelocato or any other container, servicelocator will give a user a final registration information. It is very useful to record records of events (developers can know from the IOC container what type of events is lost through the record), or they can decide that the service must be used later,
TheServicelocatorGives the end-developer a last-resort chance to register a type when it is not registered in the servicelocator or any of the external containers. this event is very useful for logging (then the developer in the log knows exactly what type is missing from the IOC container) or it can be used to determine at runtime in a very late stage what implementation of the service must be used. to register a type via the event, subscribe to the event and then use the following code:
private
void
OnMissingType(object sender, MissingTypeEventArgs e)
{
if
(e.InterfaceType == typeof(IPleaseWaitService))
{
// Register an instance
e.ImplementingInstance =
new
PleaseWaitService();
// Or a type
e.ImplementingType = typeof(PleaseWaitService);
}
}
If bothImplementinginstanceAndImplementingtypeAre filled,ImplementingintanceWill be used.
Resolving a type
To retrieve the implementation of a service, use the following code:
var pleaseWaitService = ServiceLocator.Default.ResolveType<IPleaseWaitService>();
Catel help manual-catel. Core: (5) servicelocator and typefactory)