Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Project-Embedded IoC Container

I am looking for a really simple and lightweight IoC Container whose C# source can be included in my own project (thus not making an external reference).

The reason for this is that I am writing an infrastructure and would like to provide a single .dll file, without any additional dependencies.

I also do not want to ILMerge my assembly with the IoC assembly..

I thought about MEF, some other suggestions?

like image 993
dux2 Avatar asked Jan 23 '12 08:01

dux2


People also ask

What is IoC container?

IoC container is a framework for implementing automated dependency injection. It contains object creation for the longer ways to use and injects dependencies within the class.

What are types of IoC containers?

There are basically two types of IOC Containers in Spring: BeanFactory: BeanFactory is like a factory class that contains a collection of beans. It instantiates the bean whenever asked for by clients. ApplicationContext: The ApplicationContext interface is built on top of the BeanFactory interface.

Is Spring container and IoC container same?

An IoC container is a common characteristic of frameworks that implement IoC. In the Spring framework, the interface ApplicationContext represents the IoC container. The Spring container is responsible for instantiating, configuring and assembling objects known as beans, as well as managing their life cycles.

Why do we use IoC container?

Spring IoC Container is the core of Spring Framework. It creates the objects, configures and assembles their dependencies, manages their entire life cycle. The Container uses Dependency Injection(DI) to manage the components that make up the application.


2 Answers

If you're using .NET 4.0 you have MEF included.

For .NET 2 I once wrote something similar using interfaces and Reflection. There are a lot of tutorials out there describing that process. Even if you can use MEF, its still worth attempting some reflection tutorials as this is how MEF works underneath.

Also check out this question which has some good answers. TinyIoC looks like its just a single source file.

like image 116
Tim Avatar answered Nov 13 '22 13:11

Tim


If you don't need anything fancy, a DI container can be really short:

public class Container
{
   private readonly Dictionary<Type,Func<Container,object>> factories;
   private readonly Dictionary<Type,object> cache;

   public Container()
   {
       this.factories = new Dictionary<Type,Func<Container,object>>();
       this.cache = new Dictionary<Type,object>();
   }

   public void Register<TContract>(Func<Container,TContract> factory)
   {
       // wrap in lambda which returns object instead of TContract
       factories[typeof(TContract)] = c => factory(c);
   }

   public TContract Get<TContract>()
   {
       var contract = typeof(TContract);
       if (!cache.ContainsKey(contract))
       {
           this.cache[contract] = this.factories[contract](this);
       }
       return (TContract)this.cache[contract];
   }
}

Which you would use like this:

var container = new Container();
container.Register<ICar>(c => new Car(
    c.Get<IEngine>(), c.Get<IWheel>()));
container.Register<IWheel>(c => new Wheel());
container.Register<IEngine>(c => new Engine());

var car = container.Get<ICar>();

Even more minimalistic would be to do dependency injection without a container:

IWheel wheel = new Wheel();
IEngine engine = new Engine();
ICar car = new Car(engine, wheel);

However, for complex object graphs it can quickly get complicated to maintain the correct construction order during refactorings. The container doesn't have this problem.

like image 36
Wim Coenen Avatar answered Nov 13 '22 13:11

Wim Coenen