简体   繁体   English

在webapi actionfilter中使用ninject的正确方法

[英]Correct way to use ninject within an webapi actionfilter

I have been given a webapi2 application to manage and I've started to write some unit tests for it as it didn't have any. 我已经得到了一个webapi2应用程序来管理,并且由于没有任何应用程序,我已经开始为其编写一些单元测试。

Testing controllers and services are both pretty easy to do as they are injecting their dependencies via constructor injection. 测试控制器和服务都非常容易实现,因为它们通过构造函数注入来注入其依赖项。

For the actionfilters I see things are done differently as its not possible to use constructor injection. 对于动作过滤器,我看到事情做了不同的事情,因为不可能使用构造函数注入。

Now I haven't really used ninject much before but this an example of how the filters have been setup. 现在,我之前并没有真正使用过ninject,但这只是一个如何设置过滤器的示例。

public class CustomFilterAttribute : ActionFilterAttribute
{
    public ILog Log { get; set; }

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        var id = int.Parse(actionContext.ActionArguments["id"].ToString());
        Log = actionContext.Request
            .GetDependencyScope()
            .GetService(typeof(ILog))
            as ILog;


     Log.WriteMessage(string.Format("Got id:{0}",id));

   }
}

My registration looks like 我的注册看起来像

kernel.Bind<ILog>().ToConstant(new Log());

Does this look ok? 这样看起来还好吗? I am not sure how to write a test for the filter, do I somehow need to mock the .GetDependencyScope to include my required ILog ? 我不确定如何为过滤器编写测试,是否需要模拟.GetDependencyScope以包括所需的ILog

The way Log is getting wired up within the filter is this right? 日志在过滤器中的连接方式是对的吗?

In order to access it via the DependencyResolver you need to register it with the http configuration. 为了通过DependencyResolver访问它,您需要使用http配置注册它。

public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver, System.Web.Mvc.IDependencyResolver {
   private readonly IKernel kernel;

   public NinjectDependencyResolver(IKernel kernel)
       : base(kernel) {
       this.kernel = kernel;
   }

   public IDependencyScope BeginScope() {
       return new NinjectDependencyScope(this.kernel.BeginBlock());
   }
}

And then register it during startup... 然后在启动过程中注册它...

// Use the kernal and the NinjectDependencyResolver as
// application's resolver
var resolver = new NinjectDependencyResolver(kernal);

//Register Resolver for Web Api
GlobalConfiguration.Configuration.DependencyResolver = resolver;

You can access the resolver via the ActionContext 您可以通过ActionContext访问解析器

public class CustomFilterAttribute : ActionFilterAttribute {
    public ILog Log { get; set; }

    public override void OnActionExecuting(HttpActionContext actionContext) {
        var id = int.Parse(actionContext.ActionArguments["id"].ToString());
        Log = actionContext
            .RequestContext
            .Configuration
            .DependencyResolver
            .GetService(typeof(ILog))
            as ILog;


       Log.WriteMessage(string.Format("Got id:{0}",id));
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM