如何以干净的方式解析处理程序
How to resolve handler in a clean way
我试图找到一种简单的方法来解决我的处理程序。解决方案是"handling different types of messages"。但是我找到的每个解决方案,例如 this idea,我都坚持解析处理程序,因为我的处理程序有一个构造函数,它对其他接口有一些依赖性。
例如,如果我遵循上述想法,我的 OrderMessageHandler
是
public class OrderMessageHandler
{
private readonly IInterface _interface;
public OrderMessageHandler(IInterface interface) {_inteface=interface}
bool HandleMessage( IMessage msg ) {
if ( !(msg is OrderMessage)) return false;
return true;
}
}
然而 IInterface
有其他依赖关系,我不能简单地解决它。
我想知道是否有人有比下面这个更好的主意 class?
public class MessageProcessor {
private List<IMessageHandler> handlers;
public MessageProcessor() {
handlers = new List<IMessageHandler>();
handlers.add(new SomeOtherMessageHandler());
handlers.add(new OrderMessageHandler());
}
public void ProcessMessage( IMessage msg ) {
bool messageWasHandled
foreach( IMessageHandler handler in handlers ) {
if ( handler.HandleMessage(msg) ) {
messageWasHandled = true;
break;
}
}
if ( !messageWasHandled ) {
// Do some default processing, throw error, whatever.
}
}
}
遵循Explicit Dependencies Principle,您可以确保所有必要的依赖项都注入其中。
public class MessageProcessor {
private List<IMessageHandler> handlers;
public MessageProcessor(IEnumerable<IMessageHandler> handlers) {
this.handlers = new List<IMessageHandler>(handlers);
//removed
//handlers.add(new SomeOtherMessageHandler());
//handlers.add(new OrderMessageHandler());
}
public void ProcessMessage( IMessage msg ) {
bool messageWasHandled
foreach( IMessageHandler handler in handlers ) {
if ( handler.HandleMessage(msg) ) {
messageWasHandled = true;
break;
}
}
if ( !messageWasHandled ) {
// Do some default processing, throw error, whatever.
}
}
}
如果使用 DI 容器,请确保注册所有相关方
伪代码:
container.Register<IInterface, Implementation>();
container.Register<IMessageHandler, SomeOtherMessageHandler>();
container.Register<IMessageHandler, OrderMessageHandler>();
container.Register<MessageProcessor>();
这样容器就知道如何在解析所需类型时构建对象图
MessageProcessor processor = container.Resolve<MessageProcessor>();
processor.ProcessMessage(...);
//...
如果使用 Pure DI 这仍然允许在需要的地方正确创建和注入所有涉及的类型。
IInterface dependency = new Implementation();
List<IMessageHandler> handlers = new List<IMessageHandler>();
handlers.Add(new SomeOtherMessageHandler());
handlers.Add(new OrderMessageHandler(dependency));
MessageProcessor processor = new MessageProcessor(handlers);
processor.ProcessMessage(...);
//...
我试图找到一种简单的方法来解决我的处理程序。解决方案是"handling different types of messages"。但是我找到的每个解决方案,例如 this idea,我都坚持解析处理程序,因为我的处理程序有一个构造函数,它对其他接口有一些依赖性。
例如,如果我遵循上述想法,我的 OrderMessageHandler
是
public class OrderMessageHandler
{
private readonly IInterface _interface;
public OrderMessageHandler(IInterface interface) {_inteface=interface}
bool HandleMessage( IMessage msg ) {
if ( !(msg is OrderMessage)) return false;
return true;
}
}
然而 IInterface
有其他依赖关系,我不能简单地解决它。
我想知道是否有人有比下面这个更好的主意 class?
public class MessageProcessor {
private List<IMessageHandler> handlers;
public MessageProcessor() {
handlers = new List<IMessageHandler>();
handlers.add(new SomeOtherMessageHandler());
handlers.add(new OrderMessageHandler());
}
public void ProcessMessage( IMessage msg ) {
bool messageWasHandled
foreach( IMessageHandler handler in handlers ) {
if ( handler.HandleMessage(msg) ) {
messageWasHandled = true;
break;
}
}
if ( !messageWasHandled ) {
// Do some default processing, throw error, whatever.
}
}
}
遵循Explicit Dependencies Principle,您可以确保所有必要的依赖项都注入其中。
public class MessageProcessor {
private List<IMessageHandler> handlers;
public MessageProcessor(IEnumerable<IMessageHandler> handlers) {
this.handlers = new List<IMessageHandler>(handlers);
//removed
//handlers.add(new SomeOtherMessageHandler());
//handlers.add(new OrderMessageHandler());
}
public void ProcessMessage( IMessage msg ) {
bool messageWasHandled
foreach( IMessageHandler handler in handlers ) {
if ( handler.HandleMessage(msg) ) {
messageWasHandled = true;
break;
}
}
if ( !messageWasHandled ) {
// Do some default processing, throw error, whatever.
}
}
}
如果使用 DI 容器,请确保注册所有相关方
伪代码:
container.Register<IInterface, Implementation>();
container.Register<IMessageHandler, SomeOtherMessageHandler>();
container.Register<IMessageHandler, OrderMessageHandler>();
container.Register<MessageProcessor>();
这样容器就知道如何在解析所需类型时构建对象图
MessageProcessor processor = container.Resolve<MessageProcessor>();
processor.ProcessMessage(...);
//...
如果使用 Pure DI 这仍然允许在需要的地方正确创建和注入所有涉及的类型。
IInterface dependency = new Implementation();
List<IMessageHandler> handlers = new List<IMessageHandler>();
handlers.Add(new SomeOtherMessageHandler());
handlers.Add(new OrderMessageHandler(dependency));
MessageProcessor processor = new MessageProcessor(handlers);
processor.ProcessMessage(...);
//...