Click here to Skip to main content
15,892,674 members
Articles / Programming Languages / C#

MetaContainer - an abstraction over the dependency injection infrastructure

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
7 Mar 2009Ms-PL3 min read 20.5K   82   10  
Description of an abstraction layer over the dependency injection infrastructure
using System;
using System.Collections;
using Castle.Core;
using Castle.MicroKernel;

namespace MetaContainer.Castle
{
   /// <summary>
   /// Provides common method used throughout the assembly.
   /// </summary>
   internal static class Common
   {
      /// <summary>
      /// Registers factory method mapping in Castle kernel.
      /// </summary>
      /// <param name="kernel">Kernel in which method should be registered.</param>
      /// <param name="from">Service interface type.</param>
      /// <param name="factoryMethod">A method producing service implementations.</param>
      /// <param name="name">Name under which mapping should be registred.</param>
      /// <param name="scope">Scope/lifestyle of registration.</param>
      internal static void RegisterInKernel(IKernel kernel, Type from, Func<string, object> factoryMethod, string name, Scope scope)
      {
         IDictionary properties = new Hashtable();
         properties["factoryMethod"] = factoryMethod;

         name = name ?? Guid.NewGuid().ToString();

         ComponentModel serviceModel = kernel.ComponentModelBuilder.BuildModel(
            name, from, from, properties);

         serviceModel.LifestyleType = scope == Scope.Transient ? LifestyleType.Transient : LifestyleType.Singleton;

         serviceModel.CustomComponentActivator = typeof(FactoryMethodActivator);

         kernel.AddCustomComponent(serviceModel);
      }
      /// <summary>
      /// Maps MetaContainer scope representation (<see cref="Scope"/>) to Castle's lifestyle (<see cref="LifestyleType"/>).
      /// </summary>
      /// <param name="scope">Lifestyle/scope in MetaContainer's terms.</param>
      /// <returns>Lifestyle/scope in Castle's terms.</returns>
      internal static LifestyleType MapLifestyle(Scope scope)
      {
         return scope == Scope.Transient ? LifestyleType.Transient : LifestyleType.Singleton;
      }
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) VSoft
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions