Sunday 25 August 2013

Ninject Generic Container Interface Implementation

An implementation of the IIoCGenericContainer interface for the Ninject container.

using System;
using Ninject;
using System.Collections.Generic;

namespace IoC
{
public class NinjectGenericContainer : IIoCGenericContainer
{
IKernel kernel;

public NinjectGenericContainer(IKernel kernel)
{
this.kernel = kernel;
}



public object Resolve(Type t)
{
return this.kernel.Get(t);
}

public T Resolve<T>()
{
return this.kernel.Get<T>();
}

public T TryResolve<T>()
{
return this.kernel.TryGet<T>();
}

public IEnumerable<object> ResolveAll(Type t)
{
return this.kernel.GetAll(t);
}

public IEnumerable<T> ResolveAll<T>()
{
return this.kernel.GetAll<T>();
}

public void RegisterType<T>()
{
this.kernel.Bind<T>().ToSelf();
}

public void RegisterType<T>(bool singleton)
{
if (singleton)
{
this.kernel.Bind<T>().ToSelf().InSingletonScope();
}
else
{
this.RegisterType<T>();
}
}

public void RegisterType<TFrom, TTo>() where TTo : TFrom
{
this.kernel.Bind<TFrom>().To<TTo>();
}

public void RegisterType<TFrom, TTo>(bool singleton) where TTo : TFrom
{
if (singleton)
{
this.kernel.Bind<TFrom>().To<TTo>().InSingletonScope();
}
else
{
RegisterType<TFrom, TTo>();
}
}

public void RegisterInstance(object instance)
{
this.kernel.Inject(instance);
}

public void RegisterInstance<TInterface>(TInterface instance)
{
this.kernel.Inject(instance);
}

public void RegisterInstance<TInterface>(TInterface instance, bool singleton)
{
throw new NotImplementedException();
}
}
}

No comments:

Post a Comment