如何告诉Ninject绑定到一个实现它不具有一个参考 [英] How to tell Ninject to bind to an implementation it doesn't have a reference to

查看:94
本文介绍了如何告诉Ninject绑定到一个实现它不具有一个参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 NinjectMVC3 在我的ASP.NET MVC3项目。

I'm using NinjectMVC3 in my ASP.NET MVC3 project.

我有3层


  • Foo.Web

  • Foo.Services

  • Foo.Data

Foo.Web引用Foo.Services但不Foo.Data。我的一个服务看起来像这样

Foo.Web references Foo.Services but not Foo.Data. One of my services looks like this

public class FooService : IFooService
{
    private readonly IFooRepository _fooRepository;

    public FooService(IFooRepository fooRepository)
    {
        _fooRepository = fooRepository;
    }

    // ...
}

NinjectMVC3在Foo.Web启动执行该引导方法

NinjectMVC3 executes this bootstrapping method in the Foo.Web startup

private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IFooService>().To<FooService>();
    kernel.Bind<IFooRepository>().To<FooRepository>();
    // Foo.Web doesn't know what FooRepository is
}        

如何注入FooServices依赖从Foo.Web?

How can I inject FooServices dependencies from Foo.Web?

推荐答案

为了让你在正确的方向,我建议你看一看的的洋葱架构。

To get you pointed in the right direction, I'd suggest you take a look at the onion architecture.

这是基本的premise是,任何code可以依赖于层的更多的中央。在您的情况(和它的使用Repository模式为MVC3应用常见的一种),你的UI应该有服务层的引用,这是可以接受的数据层提供参考。

It's basic premise is that any code can depend on layers more central. In your scenario (and it's a common one for MVC3 apps using the Repository pattern) your UI should have a reference to the services layer and it's acceptable to have a reference to the data layer.

如果你愿意接受(这是一个困难的药片吞下,如果你从一个经典的N层设置来了,我知道),那么你的情况就变得简单多了。

If you're willing to accept that (it's a hard pill to swallow if you're coming from a classic N-tier setup, I know), then your situation becomes much simpler.

使用Ninject你现在要做的事情如下:

With Ninject you now do something as follows:

在您NinjectMVC3.cs提交您CreateKernel变得

In your NinjectMVC3.cs file your CreateKernel becomes

    /// <summary>
    /// Creates the kernel that will manage your application.
    /// </summary>
    /// <returns>The created kernel.</returns>
    private static IKernel CreateKernel()
    {
        var modules = new INinjectModule[]
                          {
                              new ServiceDIModule(),
                              new RepositoryDIModule()
                          };

        var kernel = new StandardKernel(modules);

        //RegisterServices(kernel); <-- Only if you have some custom binding 
        //                              that are UI specific
        return kernel;
    }

现在,在你的服务层,您添加到Ninject(通过的NuGet只是普通Ninject,而不是通过的NuGet的MVC3 DROPIN)的引用,并添加什么我打电话,看起来像这样的ServiceDIModule以上:

Now, in your services layer, you add a reference to Ninject (just plain Ninject via NuGet, not the MVC3 dropin via NuGet) and add what I called above the ServiceDIModule which looks like this:

using Ninject.Modules;

namespace MyServiceLayer
{
    public class ServiceDIModule : NinjectModule
    {
        public override void Load()
        {
            //Bind Services
            Bind<IPracticeService>().To<PracticeService>().InRequestScope();
        }
    }
}

和你重复相同的过程数据层打针你可能有(的UnitOfWork,DatabaseFactory,IFooRepository等)

and you repeat the same process for the Data Tier injections you may have (UnitofWork, DatabaseFactory, IFooRepository, etc.)

namespace MyDataLayer
{
    public class RepositoryDIModule : NinjectModule
    {
        public override void Load()
        {
            //Bind Repos
            Bind<IFooRepository>().To<FooRepository>().InRequestScope();
        }
    }
}

现在,您可以访问所有绑定的你需要的方式放在首位。所以,你对我的问题其实可以归结为思维的转变。如果你能接受(勉强或其他方式)的洋葱概念,您的情况自行解决干净。

Now, you have access to all of the bindings you need way upfront. So, your question to me really boils down to a shift in thinking. If you can accept (begrudgingly or otherwise) the onion concept, your scenario resolves itself cleanly.

您还可以检查出项目丝绸并的 codeCampServer 。他们都实现了洋葱的概念在一定程度上(丝绸是肯定的,我现在在看的解决方案和用户界面的Web项目包含一个裁判的数据凸出包含了所有回购)

You can also check out Project Silk and CodeCampServer. They both implement the "onion" concept to some degree (Silk for sure, I'm looking at the solution right now and the UI web project contains a ref to the Data proj which contains all the Repos)

让我知道你在想什么。

这篇关于如何告诉Ninject绑定到一个实现它不具有一个参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆