Loading unreferenced dll MVC Ninject

2019-09-13 13:30发布

问题:

I have an MVC4 project in which I am using Ninject as the DI Framework. The problem I am having is trying to split my DI bindings from my implementation as i do not want my main web project to reference all of my other projects.

I have tried to create a separate project called Bindings which contains ninject modules:

public class WebModuleBindings : NinjectModule
{
    public override void Load()
    {
        Bind<IStaticDataRepository>().To<StaticDataRepository>();
    }
}

I then load this DLL dynamically in my web project:

    kernel.Load(bindingDll);

This works fine when i run it locally but when i use msbuild and deploy my application the bindings project dll is not included as it is not referenced anywhere.

What is the best way to solve this problem?

回答1:

Your web project is a host project. As a host project it is web project's responsibility to ensure that all dlls are provided. It would make a deployment easier



回答2:

There is no circular dependency when you have a Bootstrapper. Your flow of dependency would look something like this:

Web Project
    |
    |_______________- Bootstrapper project. All of your Ninject Bindings, etc.
    |                 The Kernel is also created here and passed back to the
    |                 Web project.
    |                                   |
    |                                   |
    ▼                                   |
Business Layer --------------------------
    |                                   |
    |                                   |
    |                                   |
    ▼                                   |
Data Access Layer -----------------------

                       Possibly a dangling Entities/POCO project here

Essentially, your bootstrapper is your composition root. It can reference every single other assembly so that it has access to all of the interfaces/concrete implementations it requires for bindings, etc. Then, your web project references both the Bootstrapper and the next layer down. This keeps your dependencies flowing down and can help structure your code a bit.