【问题标题】:Intergrating (ian coopers) Brighter with StructureMap与 StructureMap 集成(ian coopers)Brighter
【发布时间】:2016-03-29 09:47:17
【问题描述】:

我只是开始掌握 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);
        }
    }

还有我的 Structuremap 依赖范围

   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;
    }

提前致谢,

马丁

【问题讨论】:

    标签: c# inversion-of-control ioc-container structuremap brighter


    【解决方案1】:

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

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

    应该适合你。

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

    【讨论】:

    • 完美的@pedro 正是我们所需要的,:D 只是在一个长周末后有点厚了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    相关资源
    最近更新 更多