1

I have Web project which has got reference do BLL project. BLL project has got reference to DAL project. To dependency injection I use ninject in Web project.

In DAL project I have for example class:

public class UnitOfWork : IUnitOfWork
{
}

which is used in BLL project.

I can't make in Web project that injection:

Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();

Because Web project doesn't know anything about DAL project. What can I do?

user3691221
  • 109
  • 2
  • 13
  • If you already use DI, then your BL should not reference the DAL, the DAL should reference the BL, so it can implement the interfaces that the BL requires. Which you then wire together using DI. – Maarten Jun 20 '14 at 20:22

1 Answers1

0

What you can do is create an additional project called "DependencyResolver" for example.

This project contains references from all other projects (BLL, DAL and others). In this project you can build your Ninject modules and define your bindings etc. Your Web application can register the Modules in this project from Global.asax or any other startup code (of course your web application should reference the dependencyresolver project)... That way you worked around the issue that your web project needs a reference to your DAL.

So in your new DependencyResolver project:

namespace DependencyResolver
{
   public class MyModule : NinjectModule
   {
       public override void Load()
       {
           Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();
        }
}

And somewhere in your web project:

        var modules = new List<INinjectModule>
            {
                new DependencyResolver.MyModule(),
            };
        kernel.Load(modules);

Hope you get the point and can translate in so it fits your project.

Jeroen1984
  • 1,616
  • 1
  • 19
  • 32
  • Or maybe I should simply add reference to DAL from UI - what is better? – user3691221 Jun 20 '14 at 07:23
  • Do what feels good. Personally in a larger application where multiple developers work on, i would not reference my DAL in the UI layer. Main reason is that you expose your public method in your DAL to the UI, and that lets you (or other developers) do data-access in your user interface. In tradidional n-layered architecture, best practice is to let each layer only talk to the layer above. For example the UI layer should only be able to interact with the BLL. An extra project may look more difficult at first sight, but later it will definetly help you to seperate things out. – Jeroen1984 Jun 20 '14 at 07:53