MicroContainerFramework usage guide

After installing binary packages (either .msi or plain archive)

  • Create your project as normal .NET Micro Framework project.
  • Go to properties of Microsoft.SPOT.Native reference and change Copy local property to True because PostSharp will not be able to load it otherwise.
  • Add references to PostSharp.Public.dll (version shipped with MicroContainer, not ordinary PostSharp version) and MicroContainer.dll
  • Build your solution

After installing source package

  • Add your project to MicroContainer solution just the same way as ConsoleDemo project is added. After you set up your project you can remove ConsoleDemo.

After creating your project

To define a container you need to create a class. By convention it should be static and contain one method decorated with ContainerBuilderAttribute. Method should have one parameter of type IContainerBuilder and should return IMicroContainer. Method code should do nothing but configuring the container using passed IContainerBuilder instance. At the end, result of Build() method should be returned as method result.

Multiple mapping can be registered in the method. The only requirement is that mappings should not be ambigous which means that only one mapping from particular type can be registered with particular name. This rule is checked during compilation.

Currently, only constructor injection is supported. Is is not mandatory by default, so null values are injected when resolution of parameter fails. Resolution process can be customized using DependencyAttribute in which one can provide name of mapping to use when resolving dependency as well as set dependency as mandatory.

This sample code

public static class ContainerConfig
{
   [ContainerBuilder]
   public static IMicroContainer CreateContainer(IContainerBuilder builder)
   {
      return builder
         .RegisterType(typeof(IComponentA), typeof(ComponentA2), "SomeName")
         .RegisterType(typeof(IComponentA), typeof(ComponentA1), null)
         .RegisterType(typeof(IComponentB), typeof(ComponentB), null)
         .Build();
   }      
}

registers mapping from IComponentA to ComponentA2 with name "SomeName", an unnamed (default) mapping from IComponentA to ComponentA1 and an unnamed mapping from IComponentB to ComponentB. Given ComponentB like

public class ComponentB : IComponentB
{
   public ComponentB([Dependency(MappingName = "SomeName", Required = true)] IComponentA a)
   {
   }      
}

the resolution process for IComponentB will create ComponentB instance with injected ComponentA2. If it was not registered a compilation-time error would be reported.

Last edited Oct 19, 2008 at 10:15 AM by SzymonPobiega, version 3

Comments

No comments yet.