Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Ninject inside a c# class library

I have used Ninject in MVC Web application without any problem for injecting the business logic classes. But i want to inject the data access classes to the constructor of business logic classes. Logic layer is a class library and has no Program.cs class or entry point.

Data access class

 public class DataAccessClass
 {
   public void Insert(Product product)
    {
        new SqlObj().Insert(Product);
    }
 }

Data access class interface

 public Interface IDataAccessClass()
 {
   void Insert(Product product);
 }

Business logic class

 public class ProductLogic()
 {
     IDataAccessClass _dataAccessClass;

    //DataAccessClass should be injected here using Ninject
     public ProductLogic(IDataAccessClass dataAccessClass)
     {
        _dataAccessClass=dataAccessClass;
     }
     public InsertProduct(Product product)
     {
         _dataAccessClass.Insert(product);
     }
 }

This is what I need I have a 3 layer application and the layers are:

  • Web: Presentation Layer (ASP.NET MVC) --> only sees BLL
  • BLL: Business Logic Layer --> only sees DAL
  • DAL: Data Access Layer

So the Web layer doesn't know anything about my DAL layer. I have repository interfaces and concrete classes in my DAL, which are used in BLL layer in business logic classes. The question is, in order to decouple DAL and BLL, how do I setup Ninject to inject my repository implementations to the BLL layer?

like image 387
VIVEK P S Avatar asked Apr 18 '15 14:04

VIVEK P S


2 Answers

I've just come across this post and had the same potential misconception as the original poster until I understood that:

Class libraries are just that, libraries. They do not control the injection of dependencies. The DI is configured in whichever application uses those libraries (be it console, ASP.NET, WPF, WinForms or whatever) even when those injections are solely in the class libraries.

I came across this issue when moving my core models and entity framework (using unit of work) into a separate class library for re-use. I spend a couple of hours trying to work out how that class library configures its DI, when in the end it was the MVC web app that references the class library that needed to handle it.

In other words, in the bootstrapper of my actual app I needed to bind the Interfaces / Concrete Classes that are used in my referenced class libraries.

like image 171
JWhitley Avatar answered Sep 30 '22 09:09

JWhitley


Install NInject with Nuget

Install-Package Ninject

You can create a method to register the objects. You need to register all the dependencies required to create the object.

public static void Register(IKernel kernel)
{
    kernel.Bind<IDataAccessClass>().To<DataAccessClass>();
    kernel.Bind<ProductLogic>().ToSelf();
}

Create a new instance of StandardKernel and call Register to register the objects.

To get an instance of an object you simple call Get<> method and you will get a new insance of that object. There are other methods too.

static void Main(string[] args)
{
    var kernel = new StandardKernel();
    Register(kernel); // register the objects

    var productLogic = kernel.Get<ProductLogic>(); // create instance
}

To solve the problem that, only BLL sees DAL you can add a new project (class library) where you install NInject and create that Register method. You can reference this class library in Web project and register the entities.

Objects

public class Product
{
}

public class DataAccessClass : IDataAccessClass
{
    public void Insert(Product product)
    {

    }
}

public interface IDataAccessClass
{
    void Insert(Product product);
}


public class ProductLogic
{
    IDataAccessClass _dataAccessClass;

    //DataAccessClass should be injected here using Ninject
    public ProductLogic(IDataAccessClass dataAccessClass)
    {
        _dataAccessClass = dataAccessClass;
    }
    public void InsertProduct(Product product)
    {
        _dataAccessClass.Insert(product);
    }
}
like image 30
adricadar Avatar answered Sep 30 '22 07:09

adricadar