对不起标题。
我有一个系统,它是面向服务架构的一部分。它接收消息并处理它们。该过程可以简单地归结为将数据从一个位置移动到另一个位置。系统做出的所有决定都可以通过检查系统始终可用的两个不同类来做出:
- 正在处理的消息
- 特定数据操作的配置信息(从哪里移动到哪里等)
以下是主要接口
public interface IComponent
{
bool CanHandle(Message theMessage, Configuration theConfiguration);
int Priority {get;}
}
public interface IComponentLocator<T>
where T : IComponent
{
public LocateComponent(Message theMessage, Configuration theConfiguration);
}
我使用 Castle Windsor 框架来解决依赖倒置问题,因此我实现的一个定位器接收通过数组解析器注入的所有适当组件。
这里是:
public class InjectedComponentsLocator<T> : IComponentLocator<T>
where T : IComponent
{
private readonly T[] components;
public InjectedComponentsLocator(T[] components)
{
this.components = components;
this.components.OrderBy((component) => component.Priority);
}
public T LocateComponent(Message theMessage, Configuration theConfiguration)
{
List<T> candidates = this.components.Where((h) => h.CanHandle(message, configuration)).ToList();
if (candidates.Count == 0)
{
throw new Exception(string.Format(Resources.UnableToLocateComponentException, typeof(T).Name));
}
else if (candidates.Count > 1 && candidates[0].Priority == candidates[1].Priority)
{
throw new Exception(string.Format(Resources.AmbiguousComponentException, candidates[0].GetType().Name, candidates[1].GetType().Name));
}
return candidates.First();
}
}
现在的问题。界面上的Priority
属性IComponent
..我不喜欢。现实是优先级应该可以由最具体的来确定IComponent
。
例如,假设我有两个组件。
public class HandlesOneRecord : IComponent
{
public bool CanHandle(Message theMessage, Configuration theConfiguration)
{
return theMessage.BatchSize == 1;
}
}
public class HandlesOneInsert : IComponent
{
public bool CanHandle(Message theMessage, Configuration theConfiguration)
{
return theMessage.BatchSize == 1 && theMessage.Action = "Insert";
}
}
我希望系统知道一条记录的插入消息需要选择第二条,因为它是最具体的一条。现在我需要设置不同的优先级,这感觉就像在创建新组件时会变得笨拙并产生错误。
添加以尝试澄清:
如果系统最终以我设想的方式工作,我将能够拥有两个组件,一个将处理任何“插入”类型的操作,还有一个将处理批量大小的“插入”的特定组件= 1. 任何编写代码的开发人员都不应该关心系统是否选择了正确的代码,它会的。
谢谢!