将 (ian coopers) Brighter 与 StructureMap 集成

Intergrating (ian coopers) Brighter with StructureMap

我刚开始掌握 StructureMap (IoC) 的基础知识,并正在尝试整合 Brighter 的演示,可以在此处找到

https://iancooper.github.io/Paramore/QuickStart.html

在 MVC 项目中使用 Structuremap。

我遇到的问题是确定在构建时将什么传递给 Structuremap 对象工厂,它正在返回

我目前有以下错误

An exception of type 'System.ArgumentOutOfRangeException' occurred in StructureMap.dll but was not handled in user code

Additional information: Specified argument was out of the range of valid values.

工厂处理程序

public class SimpleHandlerFactory : IAmAHandlerFactory
    {
        public IHandleRequests Create(Type handlerType)
        {
            return new GreetingCommandHandler();
        }

        public void Release(IHandleRequests handler)
        {

        }
    }

CommandHandler

  public class GreetingCommandHandler : RequestHandler<GreetingCommand>
    {
        public override GreetingCommand Handle(GreetingCommand command)
        {
            Debug.Print("This is the trace for the command :  {0}", command.Name);
            return base.Handle(command);
        }
    }

还有我的结构图依赖范围

   public IHandleRequests<T> GetExecutorFor<T>() where T : class, IRequest
        {
            return this.Container.GetAllInstances<IHandleRequests<T>>().FirstOrDefault(); // GetInstance() throws exception if more than one found
        }

然后在IoC.cs

    public static IContainer Initialize() {
        ObjectFactory.Initialize(x =>
                    {
                        x.Scan(scan =>
                                {
                                    scan.TheCallingAssembly();
                                    scan.WithDefaultConventions();
                                });
                   //  x.For<IHandleRequests<IRequest>>().Use<IHandleRequests<IRequest>>();
// So this is where I was going wrong I should of been doing 
 x.For<IHandleRequests<GreetingCommand>>().Use<GreetingCommandHandler>();
                    });
        return ObjectFactory.Container;
    }

提前致谢,

马丁

要解析 GreetingCommandHandler,您必须专门在 IContainer 中注册它,因为它不会使用 DefaultConventions 解析。

For<IHandleRequests<GreetingCommand>().Use<GreetingCommandHandler>();

应该适合你。

对于所有其他具体实现,您也必须这样做。