Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The  ServiceLocator in Catel can be set up from configuration file.

Importing IoC configuration section

...

It's possible add more than one service locator configuration to the configuration file but you must specify an unique name. If a name of a service locator configuration is not specified then the name default is assigned. By default such configuration supports dependency injection.

Code Block
languagehtml/xml
<configuration>
   <configSections>
       <sectionGroup name="catel">
           <section name="ioc" type="Catel.IoC.IoCConfigurationSection, Catel.Core" />
       </sectionGroup>
   </configSections>
   <catel>
       <ioc>
           <serviceLocatorConfigurations>
               <serviceLocatorConfiguration [name="default"] [supportDependencyInjection="true"]>
                   <register interfaceType="Catel.MVVM.Services.IUIVisualizerService" implementationType="Catel.MVVM.Services.UIVisualizerService" />
                   <register interfaceType="Catel.MVVM.Services.IProcessService" implementationType="Catel.MVVM.Services.ProcessService" />
               </serviceLocatorConfiguration>
        </serviceLocatorConfigurations>
       </ioc>
   </catel>
</configuration>

...

To configure a service locator from the default service locator configuration use the following code:

 

Code Block
languagecsharp
var serviceLocator = ServiceLocator.Default;

Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var ioc = configuration.GetSection<IoCConfigurationSection>("ioc", "catel");

ioc.DefaultServiceLocatorConfiguration.Configure(serviceLocator);

Configuring a service locator from a named configuration

The following configuration file is a full example on how write more than one service locator configuration:

Code Block
languagehtml/xml
<configuration>
   <configSections>
       <sectionGroup name="catel">
           <section name="ioc" type="Catel.IoC.IoCConfigurationSection, Catel.Core" />
       </sectionGroup>
   </configSections>
   <catel>
       <ioc>
           <serviceLocatorConfigurations>
               <serviceLocatorConfiguration>
                   <register interfaceType="Catel.MVVM.Services.IUIVisualizerService" implementationType="Catel.MVVM.Services.UIVisualizerService" />
                   <register interfaceType="Catel.MVVM.Services.IProcessService" implementationType="Catel.MVVM.Services.ProcessService" />
               </serviceLocatorConfiguration>
               <serviceLocatorConfiguration name="test" supportDependencyInjection="false">
                   <register interfaceType="Catel.MVVM.Services.IUIVisualizerService" implementationType="Catel.MVVM.Services.Test.UIVisualizerService" registrationType="Transient"/>
                   <register interfaceType="Catel.MVVM.Services.IProcessService" implementationType="Catel.MVVM.Services.Test.ProcessService" tag="test"/>
               </serviceLocatorConfiguration>
           </serviceLocatorConfigurations>
       </ioc>
   </catel>
</configuration>

To configure a service locator from a named configuration use the following code:

Code Block
languagecsharp
var serviceLocator = ServiceLocator.Default;

Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var ioc = configuration.GetSection<IoCConfigurationSection>("ioc", "catel");

ioc.GetServiceLocatorConfiguration("test").Configure(serviceLocator);

You should also note the options setup if the container in order to support dependency injection and the registration type (a.k.a instantiation style) and the tag for each registration.

 

...