而不是在ObjectFactory的StructureMap

而不是在ObjectFactory的StructureMap

本文介绍了如何使用容器,而不是在ObjectFactory的StructureMap ServiceActivator?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在使用的WebAPI实现StructureMap DI,我们使用了 ServiceActivator

When implementing DI in WebAPI with StructureMap, we used the ServiceActivator found in




  • Configuring Dependency Injection with ASP.NET WebAPI 2.1
  • WebAPI + APIController with structureMap
public class ServiceActivator : IHttpControllerActivator
{
    public ServiceActivator(HttpConfiguration configuration) {}

    public IHttpController Create(HttpRequestMessage request,
           HttpControllerDescriptor controllerDescriptor, Type controllerType)
    {
        var controller = ObjectFactory.GetInstance(controllerType) as IHttpController;
        return controller;
    }
}

但是现在有了新的 StructureMap ,我ReSharper的建议:

But now with the new StructureMap, my ReSharper suggested:

班StructureMap.ObjectFactory已过时:ObjectFactory的将在未来的4.0版本StructureMap来去除。赞成容器类的未来工作的用法

集装箱智能感知给了我只有非常有限的信息。

The intellisense on Container gave me only very limited information.

我们怎么改写我们的 ServiceActivator 容器类?

How are we supposed to rewrite our ServiceActivator with the Container class?

推荐答案

东西是怎么回事走。如果你不使用某种类型的服务定位器,你将不得不实现自己的的ObjectFactory 为的引用:

The static stuff is going away. If your not using a Service Locator of some type you're going to have implement your own "ObjectFactory" as referenced here:

public static class ObjectFactory
{
    private static readonly Lazy<Container> _containerBuilder =
            new Lazy<Container>(defaultContainer, LazyThreadSafetyMode.ExecutionAndPublication);

    public static IContainer Container
    {
       get { return _containerBuilder.Value; }
    }

     private static Container defaultContainer()
     {
        return new Container(x =>
        {
               // default config
         });
     }
}

更新:我的previous答案是错的。感谢@JoeMighty的抬起头。

Update: My previous answer was wrong. Thanks @JoeMighty for the heads up.

这篇关于如何使用容器,而不是在ObjectFactory的StructureMap ServiceActivator?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:21