Lightweight Ioc / Dependency injection Framework

Inversion of Control, or IoC, is an abstract principle describing an aspect of some software architecture designs in which the flow of control of a system is inverted in comparison to procedural programming.

Inversion of Control as a design guideline serves the following purposes:
  • There is a decoupling of the execution of a certain task from implementation.
  • Every system can focus on what it is designed for.
  • The systems make no assumptions about what other systems do or should do.
  • Replacing systems will have no side effect on other systems.

There are already many popular framework available for .NET:

Castle Windsor based on the Castle MicroKernel.
* Licensed under Apache 2
* Well documented and used by many.

StructureMap has been around since June 2004
  • Licensed under Apache 2
  • Actively developed and approaching v2.5.
  • Written by Jeremy D. Miller

Spring.NET
  • Licensed under Apache 2
  • Currently 1.1, working on 2.0
  • Written by Mark Pollack

Autofac intends to be IoC with a C# 3.0 flavor, but also supports 2.0.
  • Licensed under MIT
  • Written by Nicholas Blumhardt and Rinat Abdullin

Unity
  • Licensed as MS-PL
  • Recently released
  • Written by Chris Tavares.
  • Build on the "kernel" of ObjectBuilder:
  • ObjectBuilder was formerly MSFT’s only public foray into DI/IoC

Puzzle.NFactory
  • Licensed under the Lesser GPL
  • Part of the larger Puzzle Framework
  • Written by Roger Alsing and Mats Helander

Ninject formerly "Titan"
  • Licensed under Apache 2
  • Was quiet, but has recently sprung back to life.
  • Written by Nate Kohari

S2Container.NET on version 1.3.7.
  • Port of Java’s Seasar2

PicoContainer.NET
  • Port of Java’s PicoContainer
  • Written by Aslak Hellesøy

LinFu
  • Licensed under Lesser GPL
  • Written by Philip Laureano

Munq
  • Lightweight basic Dependency Injection Container.
  • boasts to be the fastest DI.
  • Written by Matthew Dennis

If anyone want to use the lightest container and therefore Munq is the ultimate choice, but Munq is the basic container only & therefore it doesn’t supports constructor injection, auto wiring etc.

We reviewed the Munq source & researched the internal working of Munq Core. Matthew Dennis did a great job, he created a very lightweight container with simplicity. But I say, It is incomplete DI container without constructor injection, auto wiring & without some optimization, so what we want a new lightweight container with construction injection.

So We build the new container with construction injection, & auto wiring capabilities using Attributes.

Features like:
  1. InjectBind Attribute to binding classes for auto wiring support,
  2. Mark assemblies with IocContainerAssembly for faster auto wiring of assemblies.
  3. Mark Constructor with Inject Attribute just like nInject for constructor injection.
  4. Named Attribute to mark keyed instances on constructor parameters.
  5. ResolveAll Method to resolve types in specified assemblies using InjectBind Attribute.
  6. Specify Type Lifetime using InjectBind Attribute.
  7. Linq Compiled Expression for faster instance creation.
  8. Not Compared, but might be the fastest DI framework,

How to Use

Create a Interface.
public interface ICache
{
  object this[string key] { get; }
}


Implement Class and Define Attributes
[InjectBind(typeof(ICache), LifetimeType.Request)]
public class Cache : ICache
{ 
}


Add IocAttribute in AssemblyInfo.cs file
[assembly: IocContainerAssembly]


Use ContainerFactory class to resolve dependencies at runtime.
ICache cache = ContainerFactory.Container.Resolve<ICache>();

Last edited Oct 1, 2010 at 5:59 PM by isilverlabs, version 5

Comments

No comments yet.