使用Ninject 2房产注射基本控制器 [英] Property Injection in Base Controller using Ninject 2

查看:91
本文介绍了使用Ninject 2房产注射基本控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code在我Global.aspx

I have the following code in my Global.aspx

protected override void OnApplicationStarted()
{
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
    RegisterAllControllersIn(Assembly.GetExecutingAssembly());
}

protected override IKernel CreateKernel()
{
    return new StandardKernel(new ServiceModule());
}

我也有以下Ninject模块:

I also have the following Ninject Module:

internal class ServiceModule : NinjectModule
{
    public override void Load()
    {
        Bind<IProductService>().To<ProductService>().InRequestScope();
    }
}

我也有一个基本的控制器:

I also have a base controller:

public class BaseController : Controller
{
    [Inject]
    public IProductService ProductService
    {
        get;
        set;
    }
}

这code工作。我遇到的问题是,我想从基本控制器取出注射属性,并指定这个在Ninject ServiceModule来代替。我换句话说,我怎么会去写的,告诉Ninject注入ProductService成?

This code works. The problem I am having is that I would like to remove the inject attribute from the base controller and specify this in the Ninject ServiceModule instead. I other words, how would I go about writing a binding rule in the ServiceModule that tells Ninject to inject ProductService into the property in the base controller?

如果我删除属性我会得到一个NullReferenceException。

If I remove the attribute I will get a NullReferenceException.

推荐答案

根据公约约束力的生命 http://github.com/ninject/ninject.extensions.conventions - 一个工具 IBindingGenerator 。这主要是关心发现接口和服务,但。

Convention-based binding lives in http://github.com/ninject/ninject.extensions.conventions -- one implements IBindingGenerator. This is largely concerned with discovering interfaces and services though.

在一般情况下,构造函数注入是一个很好的默认的态度。然而,在这ASP.NET MVC的工作方式,使这个更难做(因此FubuMVC等)。因此,物业注入是下一个最好的选择。

In general, constructor injection is a good default approach. However the manner in which ASP.NET MVC works makes this harder to do (Hence FubuMVC etc.). So property injection is the next best option.

您可能会发现,使用 OnActivati​​on 绑定可以让你做够了 - 如果可以的话,这是迄今为止最简单的。

You may find that using OnActivation in your Bind may allow you to do enough - and if you can, this is by far the simplest.

我想你的特点正在试图做基于约定激活的。这个问题的是:

I'd characterise what you're trying to do as convention-based activation. The problem's are:


  • 决定你要自动注射了什么。你要注入的一切公众未混凝土?一切你的内核知道?除非你能拿出你想要做什么干净的定义,注射过程可以成为联合国predictable和难以理解。你最终调试和解释给同事了不少。

  • deciding what you are going to auto-inject. Are you going to inject everything public that isn't concrete? Everything that your Kernel knows about? Unless you can come up with a clean definition of what you want to do, the injection process can become unpredictable and difficult to understand. You end up debugging and explaining to colleagues a lot.

使其高效。 Ninject动态生成幕后code键使一个实例有效(即激活,在行走的类寻找的时间[注入] 标记它生成code一旦做到这一点,然后,如果你写草书它得到即时编译)。

making it efficient. Ninject dynamically generates code behind the scenes to make the activation of an instance efficient (i.e., at the time of walking the class looking for [Inject] markers it generates code once to do that which then gets jitted as if you'd written it longhand).

展望在code,有没有简单的方法开箱即用。貌似添加自定义 IInjectionHeuristic 会做的伎俩。

Looking in the code, there's no easy way OOTB. Looks like adding a custom IInjectionHeuristic would do the trick.

不过,如果你要深入到这个容器,你需要

However if you're getting this deep into containers, you need to


  1. 暂停,看看你可以把它简单的通过不会沿着这条道路

  2. 进入ninject邮件列表和搜索类似的事情

  3. 如果你仍然想这样做,发送邮件那里。

这篇关于使用Ninject 2房产注射基本控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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