How to use Ninject inside a C # class library

I used Ninject in an MVC web application without any problems to input business logic classes. But I want to introduce data access classes in the business logic class constructor. The logical layer is a class library and does not have a 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 Level

So the Web layer knows nothing about my DAL layer. I have repository interfaces and specific classes in my DAL that are used in the BLL layer in business logic classes. The question is to separate DAL and BLL , how do I configure Ninject to implement my repository implementations at the BLL level?

+5
source share
2 answers

Install NInject with Nuget

 Install-Package Ninject 

You can create a method for registering objects. You need to register all the dependencies necessary 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 simply call the Get<> method, and you get a new view of this object. There are other methods.

 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, only the BLL sees the DAL , you can add a new project (class library) where you install NInject and create this Register method. You can reference this class library in a Web project and register objects.

The 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); } } 
+3
source

I just stumbled upon this post, and I had the same potential fallacy as the original poster, until I realized that:

Class libraries are just libraries. They do not control dependency injection. DI is configurable in any application that uses these libraries (whether it is a console, ASP.NET, WPF, WinForms or anything else), even if these injections are found exclusively in class libraries.

I ran into this problem when I moved my main models and entity structure (using a unit of work) to a separate class library for reuse. I spent a couple of hours trying to figure out how this class library sets up its DI, when in the end it was an MVC web application that references the class library that was supposed to handle it.

In other words, in the bootloader of my real application, I needed to bind the interfaces / specific classes that are used in my reference class libraries.

0
source

All Articles