[ Log On ]

Dependency Injection - Ninject

Posted by frennky on 5/18/2011 @ 10:12 PM in Web Development

Ninject je open source DI framework, besplatan za ličnu i komercijalnu upotrebu. U .Net zajednici možda i najpopularniji. Odlikuje ga jednostavna i simpatična sintaksa, kao i vrlo dobra dokumentacija koja do detalja objašnjava njegovu upotrebu.

Jednostavna implementacija IDependencyResolver interfejsa pomoću Ninject DI framework-a bi izgledala ovako:

public class NinjectDependencyResolver : IDependencyResolver
{
    private readonly IKernel kernel;

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

    public object GetService(Type serviceType)
    {
        try
        {
            return this.kernel.Get(serviceType);
        }
        catch
        {
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return this.kernel.GetAll(serviceType);
        }
        catch
        {
            return new List<object>();
        }
    }
}

Na kraju, potrebno je podesiti IoC kontejner unutar Global.asax.cs fajla:

protected void Application_Start()
{
    RegisterNinjectDependencyResolver();
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
}

private void RegisterNinjectDependencyResolver()
{
    var kernel = new StandardKernel();
    kernel.Bind<IRepository>().To<ConcreteRepository>();
    DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}