Replacing the default components

By default, Catel provides very fast and functional implementations of the component interfaces. It is possible though that one needs to use a different container than the specified ones.

Replacing default 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.

To replace any component, first create a custom implementation of the specific component, for example the IServiceLocator. Then update the factory and call UpdateDefaultComponents:

Catel.IoC.IoCFactory.CreateServiceLocatorFunc = () => new MyCustomServiceLocator();
Catel.IoC.IoCFactory.CreateTypeFactoryFunc = () => new MyCustomTypeFactory();
 
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:

var serviceLocator = IoCFactory.CreateServiceLocator();

Catel will automatically create the right IDependencyResolver and ITypeFactory and register them in the newly created IServiceLocator.


Contributions

We would like to thank the following contributors:

Want to contribute to the documentation? We have a guide for that!


Questions

Have a question about Catel? Use StackOverflow with the Catel tag!


Discussion