在下面的类中,我有一个名为ProcessMessage的公共方法。此方法负责处理传入的消息。处理消息涉及不同的阶段。我想以这样的方式装饰这个类,我可以从消息处理的每个阶段发布性能计数器值。
我知道我可以覆盖ProcessMessage方法并使用发布性能计数器值再次重写逻辑。但是有没有更好的方法/模式我可以应用,所以我不必在装饰类中再次复制逻辑。
public class MessageProcessor
{
public void ProcessMessage()
{
ConvertReceivedMessage();
SendToThirdParty();
ReceiveResponse();
ConvertResponseMessage();
SendResponseToClient();
}
private void ConvertReceivedMessage()
{
//here I want to publish the performance counter value from the decorated class
}
private void SendToThirdParty()
{
//here I want to publish the performance counter value from the decorated class
}
private void ReceiveResponse()
{
//here I want to publish the performance counter value from the decorated class
}
private void ConvertResponseMessage()
{
//here I want to publish the performance counter value from the decorated class
}
private void SendResponseToClient()
{
//here I want to publish the performance counter value from the decorated class
}
}
感谢。
答案 0 :(得分:0)
使用IProcessor对象列表而不是一堆方法。通过这种方式,您可以添加/跳过/更改呼叫顺序。在您的IProcessor声明Process(PerformanceContext context)方法并实现PerformanceContext类来交换一些值,如StartTime,NumberOfCalls等。
祝你好运!