我有以下代码:
public class Dispatcher : IDispatcher {
public void Send(Order order) {
var type = typeof(IOrderHandler<>).MakeGenericType(order.GetType());
var handler = ObjectFactory.GetInstance(type);
}
}
注意:我正在尝试获取 IOrderHandler,在这种情况下,订单为: public class CoffeOrder : Order { }
我的 StructureMap 配置如下:
ObjectFactory.Initialize(x => {
x.For<IOrderHandler>().Use<OrderHandler>();
});
调试时出现以下错误: {"StructureMap configuration failures:\r\nError: 104\r\nSource: Registry: StructureMap.Configuration.DSL.Registry, StructureMap, Version=2.6.4.0, ...
我尝试了其他选项,但总是以 104 错误或 202 错误结束:“StructureMap 异常代码:202\nNo Default Instance defined for PluginFamily Proj.Test.Tests+IOrderHandler`1[[Proj.Test.Tests+CoffeOrder,项目测试,...
注意: CoffeOrder 是我在测试中通过的订单类型。
我的 Handler 类和接口代码如下:
public class CoffeHandler : OrderHandler<CoffeOrder> {
public override void Handle(CoffeOrder order) {
// Some Code
} // CoffeHandler
}
public interface IOrderHandler {
void Handle(Order order);
} // IOrderHandler
public interface IOrderHandler<TOrder> : IOrderHandler where TOrder : Order {
void Handle(TOrder order);
} // IOrderHandler
public abstract class OrderHandler : IOrderHandler {
public abstract void Handle(Order order);
} // OrderHandler
public abstract class OrderHandler<TOrder> : OrderHandler, IOrderHandler<TOrder> where TOrder : Order {
public override void Handle(Order order) {
TOrder torder = (TOrder)order;
Handle(torder);
} // Handle
public abstract void Handle(TOrder order);
} // OrderHandler
有人可以帮我吗?
谢谢你,米格尔