MicroContainer internals

Container configuration

As you can read in Usage guide section, MicroContainers are configured in code using simple fluent interface. You write your container configurator method like any other method. You operate on interfaces to register type mappings and does not have to worry about actual implementation.

Container wire-up process

But you can be surprised, that this method's code isn't executed as you would expect - at run-time. It is executed during PostSharp build process. The mappings you configured are recorded in provided IContainerBuilde implementation and are subject to code generation, which is conducted in during wire-up process.

First of all, a class inheriting from BaseContainer is created as inner class of one that contained your configuration method. Next, all required internal structures are created (cache for delegates of type-resolving methods, cache accessor which is required by BaseContainer contract).

Next step is generation of just mentioned type-resolving methods. For each mapping registered in IContainerBuilder one method is generated. It purpose is to create instance of type which mapping leads to and inject all the dependencies into this instance. As currently only constructor injection is supported, injection is simple process of invoking other type-resolving methods to obtain constructor arguments. Is no such method can be found one of following actions is conducted:
  • if mapping is mandarotry (marked as required in DependencyAttribute), a compile-time error is reported
  • otherwise, a null is passed as dependency value.

After that, a static constructor is generated. It initilizes the delegates cache by adding delegates for all the type-resolving methods.

The last step is replacing the configuration method body with new code which constructs generated container class instance and returns it. As this step is finished, the container is fully wired up and ready to use.

Run-time usage

At run time, the configuration method is called to obtain an instance of the container. You should pass null as argument of the method, because this argument is no longer used as body of the method was replaced.

Last edited Oct 19, 2008 at 12:01 PM by SzymonPobiega, version 1


No comments yet.