简体   繁体   English

用于逐步智能对象更新的设计模式

[英]Which design pattern to use for Step wise object update

I have to write a logic that would update an Object through a series of methods and would detect what step has failed : 我必须编写一种逻辑,该逻辑将通过一系列方法来更新对象并检测失败的步骤:

EntryPointForLogic() {
    Object obj = CreateObject();

    UpdateObj1(obj);
    UpdateObj2(obj);
    ....
    UpdateObj_(obj);
}

Is there a preferred / standard design pattern for writing similar logic. 是否存在用于编写类似逻辑的首选/标准设计模式。 The different steps could fail and I want to detect which one of them failed. 不同的步骤可能会失败,我想检测其中哪一个失败。

This roughly maps to something like a chain of responsibility , you could use an abstract class along with step name information and logging to create an audit trail. 这大致映射为类似 的责任链 ,您可以将抽象类与步骤名称信息和日志记录一起使用,以创建审计跟踪。

Again, this is not exactly a chain of responsibility because the previous handler is not changing the steps in my example 再次强调, 这并不是责任链,因为先前的处理程序未更改我的示例中的步骤

Abstract class 抽象类

This is not perfect, you'll need to handle the implementation of stuff as you need it, and wouldn't recommend building your own logging library but still an OK example 这不是完美的,您需要根据需要处理东西的实现,并且不建议您构建自己的日志记录库,但仍然是一个不错的示例

public abstract class Updater<T> 
{
     private readonly ILog _logger;
     public Name {get; private set;}
     public Updater(string name, ILog logger)
     {
         _logger= logger;
         Name = name;
     }


     protected abstract UpdateImpl(T updating);

     public Update(T updating)
     {
         _logger.Log("Started updater " + Name);
         try
         {
             UpdateImpl(updating);
         }
         //DON'T DO THIS IN ACTUAL IMPLEMENTATION
         //DO NOT EAT ALL OF THE EXCEPTIONS
         catch ( Exception e)
         {
            _logger.Log(e);   
         }
         finally
         {
             _logger.Log("Finished Updater " + Name);
         }
     }
}

Example Implementation 示例实施

Then you just define the individual updaters, for example 然后,您只需定义各个更新程序,例如

public class AwesomeObjectNameUpdater : Updater<AwesomeObject>
{
      public AwesomeObjectNameUpdater(ILogger logger) :base("Awesome Name Updater", logger)
      {

      }

      protected override UpdateImpl(AwesomeObject updating)
      {
           //here your mission critical business logic will go
           updating.Name = "Mr.Fancy-Pants " + updating.Name;
      }
}

Actual Chain 实际链

You can just create an enumerable with all of the different implementations of your Updaters and apply them in order 您可以使用更新程序的所有不同实现创建一个枚举,然后按顺序应用它们

//assume it is assign in a constructor
private readonly IEnumerable<Updater<AwesomeObject>> _updaters;
public void ApplyUpdaters(AwesomeObject obj)
{
    foreach(var updater in _updaters)
    {
        updater.Update(obj);
    }
}

Further reading 进一步阅读

If you want to read more about the pattern you can see the links provided below 如果您想了解有关该模式的更多信息,请参见下面提供的链接

You can use List of Action and Iterate through them: 您可以使用“动作列表”并对其进行迭代:

EntryPointForLogic() {
    Object obj = CreateObject();
    List<Action<object>> actions = new List<Action<object>>()
    { 
         UpdateObj1,UpdateObj2,UpdateObj3
    };


    for(int i =0; i<actions.Count; i++)
    {
        try
        {
             actions[i](obj);
        }
        catch(Exception ex)
        {
            Console.WriteLine($"Error occured in update nr {i}");
        }
    }
}

To add name of each action you can use 要添加每个动作的名称,您可以使用

- inside method : List<Tuple<string,Action<object>>> -内部方法: List<Tuple<string,Action<object>>>
- in interface you should not expose Tuple so create your own class/struct with name and action. -在接口中,您不应公开Tuple,因此请使用名称和操作创建自己的类/结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM