using System;
using System.Linq;
using Ninject;
namespace Tapeti.Ninject
{
///
/// Dependency resolver and container implementation for Ninject.
///
public class NinjectDependencyResolver : IDependencyContainer
{
private readonly IKernel kernel;
///
///
public NinjectDependencyResolver(IKernel kernel)
{
this.kernel = kernel;
}
///
public T Resolve() where T : class
{
return kernel.Get();
}
///
public object Resolve(Type type)
{
return kernel.Get(type);
}
///
public void RegisterDefault() where TService : class where TImplementation : class, TService
{
if (kernel.GetBindings(typeof(TService)).Any())
return;
kernel.Bind().To();
}
///
public void RegisterDefault(Func factory) where TService : class
{
if (kernel.GetBindings(typeof(TService)).Any())
return;
kernel.Bind().ToMethod(_ => factory());
}
///
public void RegisterDefaultSingleton() where TService : class where TImplementation : class, TService
{
if (kernel.GetBindings(typeof(TService)).Any())
return;
kernel.Bind().To().InSingletonScope();
}
///
public void RegisterDefaultSingleton(TService instance) where TService : class
{
if (kernel.GetBindings(typeof(TService)).Any())
return;
kernel.Bind().ToConstant(instance);
}
///
public void RegisterDefaultSingleton(Func factory) where TService : class
{
if (kernel.GetBindings(typeof(TService)).Any())
return;
kernel.Bind().ToMethod(_ => factory()).InSingletonScope();
}
///
public void RegisterController(Type type)
{
kernel.Bind(type).ToSelf();
}
}
}