...
Note |
---|
Note that when any component is replaced, it must be registered with the other instances that are already running. Catel cannot do this automatically because it is not aware how other (customized) components interact or require registration. |
Replacing
...
default
...
Code Block |
---|
var serviceLocator = new MyCustomServiceLocator();
var dependencyResolver = new CatelDependencyResolver(serviceLocator);
var typeFactory = new TypeFactory(dependencyResolver);
IoCConfiguration.DefaultServiceLocator = serviceLocator;
IoCConfiguration.DefaultDependencyResolver = dependencyResolver;
IoCConfiguration.DefaultTypeFactory = typeFactory; |
...
Note that Catel registers it's own services as soon as the assembly is loaded by the .NET runtime. If a custom ServiceLocator is used, these services have to be registered again. Catel provides helper classes for this by naming convention:
...
components
Starting with Catel 3.9, it is very easy to customize the components. This can be achieved by customizing the factory methods that are available on the IoCConfiguration class.
Note |
---|
Note that the customization of the IoCConfiguration is the firstthing that must be done at application start up |
To replace any component, first create a custom implementation of the specific component, for example the IServiceLocator. Then update the factory and call UpdateDefaultComponents:
Code Block |
---|
Catel.IoC.IoCFactory.CreateServiceLocatorFunc = () => new MyCustomServiceLocator();// Core elements CoreModule.RegisterServices(serviceLocator); MVVMModule.RegisterServices(serviceLocator Catel.IoC.IoCFactory.CreateTypeFactoryFunc = () => new MyCustomTypeFactory();// Extensions ExtensionsPrismModule.RegisterServices(serviceLocator); ExtensionsMementoModule.RegisterServices(serviceLocator); |
Replacing the default TypeFactory
...
Code Block |
---|
var typeFactory = new MyCustomTypeFactory(IoCConfiguration.DefaultDependencyResolver);
IoCConfiguration.DefaultTypeFactory = typeFactory; |
Replacing the default DependencyResolver
...
Catel.IoC.IoCConfiguration.UpdateDefaultComponents(); |
At this moment, Catel will fully replace the components (in this case the IServiceLocator and ITypeFactory), but will keep using the default implementation of the IDependencyResolver.
Creating IoC components in code
It is best to respect the customization of the IoC components in the code. Therefore it is wise to always use the IoCFactory to create a ServiceLocator when a new instance is needed:
Code Block |
---|
var dependencyResolverserviceLocator = new CatelDependencyResolver(IoCConfiguration.DefaultServiceLocator); var typeFactory = new TypeFactory(dependencyResolver); IoCConfiguration.DefaultDependencyResolver = dependencyResolver; IoCConfiguration.DefaultTypeFactory = typeFactory;IoCFactory.CreateServiceLocator(); |
Catel will automatically create the right IDependencyResolver and ITypeFactory and register them in the newly created IServiceLocator.