One of the changes I made to NCommon in 1.1 was to provide an easy way to configure NCommon’s various services. The approach taken was inspired by MassTransit’s and Caliburn’s configuration API.
To configure NCommon you can use the static Configure class:
1 /// <summary>
2 /// Static configuration class that allows configuration of NCommon services.
3 /// </summary>
4 public static class Configure
5 {
6 /// <summary>
7 /// Entry point to NCommon configuration.
8 /// </summary>
9 /// <param name="containerAdapter">The <see cref="IContainerAdapter"/> instance to use
10 /// for component registration.</param>
11 /// <returns>An instance of <see cref="INCommonConfig"/> that can be used to configure
12 /// NCommon configuration.</returns>
13 public static INCommonConfig Using(IContainerAdapter containerAdapter)
14 {
15 Guard.Against<ArgumentNullException>(containerAdapter == null,
16 "Expected a non-null IContainerAdapter implementation.");
17 return new NCommonConfig(containerAdapter);
18 }
19 }
The Using method of the Configure class expects an instance of IContainerAdapter . An implementation of IContainerAdapter is basically a IoC container wrapper that exposes a standard contract that NCommon can use to register components with the IoC container of your choice.
Implementations of Castle Windsor, StructureMap, NInject, Unity are already provided. The interface is simple enough to implement for other containers. Below is an example of using Configure with castle windsor:
18 var container = new WindsorContainer();
19 var containerAdapter = new WindsorContainerAdapter(container);
20 NCommon.Configure.Using(containerAdapter)
21 .ConfigureState<DefaultStateConfiguration>()
22 .ConfigureData<NHConfiguration>(nhConfiguration => nhConfiguration
23 .WithSessionFactory(() => nhFactory))
24 .ConfigureUnitOfWork<DefaultUnitOfWorkConfiguration>(uowConfiguration => uowConfiguration
25 .AutoCompleteScope()
26 .WithDefaultIsolation(IsolationLevel.ReadCommitted));
In future posts I’ll go into details on the different configuration options of NCommon.