Need a way to extend pattern

Task

I’m trying to handle input string that looks like /commnd param1 param2 ... . But if user missed some parameters or parameter was incorrect, I would like to provide option to re-input only those parameters, that seemed incorrect.

Research

I’ve made an interface which works similar to FSM. But it works more directly. I could use FSM but it’s idea slightly different. In State Machine pattern a state knows the condition, when it has change to a next state and it creates next state inside itself. In my case there is no necessity to change state by state on each itteration, because plan have to execute step by step as much as possible at once.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public interface IPlan<TReport>{
IObservable<TReport> Execute();
}
public interface IStep{
IObservable<bool> Make();
}
</code>
<code>public interface IPlan<TReport>{ IObservable<TReport> Execute(); } public interface IStep{ IObservable<bool> Make(); } </code>
public interface IPlan<TReport>{
 IObservable<TReport> Execute();
}

public interface IStep{
  IObservable<bool> Make();
}

The plan is executed as long as all steps are completed successfully. The method IPlan.Execute() returns the report with the details of execution. And if plan fails, user will be able to update input parameters and resume plan execution from the last breakpoint.

It would be nice if there was no necessity to input some additional data. But unfortunately in number of cases the plan executes with some TContext, it calculates some data during execution and stores them inside TBuffer. And if IPlan.Execute breaks on some Step, it provides TReport which allows code to send an information to user about what exactly goes wrong. And user would have option to provide only the missing parameter.

Solution

So I’ve implemented the ObservablePlan<TStep, TReport>: IPlan<TReport> without TContext and TBuffer. And right now I’m trying to make an implementation of ObservablePlan that allows included Steps to work with TContext that could be updated from outside and TBuffer that should be updated from inside of plan execution. I thought about decorators, but the nested steps couldn’t have access to the fields of the decorator

ObservablePlan:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public abstract class ObservablePlan<TStep, TReport>: IPlan<TReport> where TStep : IStep
{
public TStep? CurrentStep { get; private set; } = default;
public IEnumerable<TStep> Steps { get; }
IEnumerator<TStep>? enumerator = null;
public ObservablePlan(IEnumerable<TStep> steps)
{
if (steps == null || !steps.Any())
throw new ArgumentNullException("steps", "Steps couldn't be empty or equal `Null`");
Steps = steps;
}
protected virtual void Init()
{
enumerator = Steps.GetEnumerator();
}
public virtual void Restart()
{
Init();
}
public IObservable<TReport> Execute()
{
if (enumerator == null)
Init();
if (enumerator.Current == null && !enumerator.MoveNext())
{
throw new ArgumentException("No more steps to do");
}
return RecursiveCallMake(enumerator).Select(CreateReport);
}
public IObservable<bool> RecursiveCallMake(IEnumerator<TStep> enumerator)
{
CurrentStep = enumerator.Current;
return CurrentStep.Make()
.SelectMany(x =>
{
if (!x)
{
Console.WriteLine("Step didn't passed: " + x.ToString());
return Observable.Return(x);
}
else
Console.WriteLine("Step passed: " + x.ToString());
if (!enumerator.MoveNext())
{
Console.WriteLine("No more steps!");
return Observable.Return(x);
}
return RecursiveCallMake(enumerator);
});
}
protected abstract TReport CreateReport(bool success);
}
</code>
<code>public abstract class ObservablePlan<TStep, TReport>: IPlan<TReport> where TStep : IStep { public TStep? CurrentStep { get; private set; } = default; public IEnumerable<TStep> Steps { get; } IEnumerator<TStep>? enumerator = null; public ObservablePlan(IEnumerable<TStep> steps) { if (steps == null || !steps.Any()) throw new ArgumentNullException("steps", "Steps couldn't be empty or equal `Null`"); Steps = steps; } protected virtual void Init() { enumerator = Steps.GetEnumerator(); } public virtual void Restart() { Init(); } public IObservable<TReport> Execute() { if (enumerator == null) Init(); if (enumerator.Current == null && !enumerator.MoveNext()) { throw new ArgumentException("No more steps to do"); } return RecursiveCallMake(enumerator).Select(CreateReport); } public IObservable<bool> RecursiveCallMake(IEnumerator<TStep> enumerator) { CurrentStep = enumerator.Current; return CurrentStep.Make() .SelectMany(x => { if (!x) { Console.WriteLine("Step didn't passed: " + x.ToString()); return Observable.Return(x); } else Console.WriteLine("Step passed: " + x.ToString()); if (!enumerator.MoveNext()) { Console.WriteLine("No more steps!"); return Observable.Return(x); } return RecursiveCallMake(enumerator); }); } protected abstract TReport CreateReport(bool success); } </code>
public abstract class ObservablePlan<TStep, TReport>: IPlan<TReport> where TStep : IStep
{
  public TStep? CurrentStep { get; private set; } = default;
  public IEnumerable<TStep> Steps { get; }
  IEnumerator<TStep>? enumerator = null;
  public ObservablePlan(IEnumerable<TStep> steps)
  {
    if (steps == null || !steps.Any())
      throw new ArgumentNullException("steps", "Steps couldn't be empty or equal `Null`");
    Steps = steps;
  }
  protected virtual void Init()
  {

    enumerator = Steps.GetEnumerator();
  }
  public virtual void Restart()
  {
    Init();
  }
  public IObservable<TReport> Execute()
  {
    if (enumerator == null)
      Init();
    if (enumerator.Current == null && !enumerator.MoveNext())
    {
      throw new ArgumentException("No more steps to do");
    }
    return RecursiveCallMake(enumerator).Select(CreateReport);
  }

  public IObservable<bool> RecursiveCallMake(IEnumerator<TStep> enumerator)
  {
    CurrentStep = enumerator.Current;
    return CurrentStep.Make()
      .SelectMany(x =>
      {
        if (!x)
        {
          Console.WriteLine("Step didn't passed: " + x.ToString());
          return Observable.Return(x);
        }
        else
          Console.WriteLine("Step passed: " + x.ToString());

        if (!enumerator.MoveNext())
        {
          Console.WriteLine("No more steps!");
          return Observable.Return(x);
        }
        return RecursiveCallMake(enumerator);
      });
  }
  protected abstract TReport CreateReport(bool success);
}

And the Step implementation:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public abstract class Step<T, K> : IStep<T> where K : class
{
public K Buffer{get;}
public Step(K buffer){
Buffer = buffer;
}
public abstract IObservable<T> Make();
}
</code>
<code>public abstract class Step<T, K> : IStep<T> where K : class { public K Buffer{get;} public Step(K buffer){ Buffer = buffer; } public abstract IObservable<T> Make(); } </code>
public abstract class Step<T, K> : IStep<T> where K : class
{
  public K Buffer{get;}
  public Step(K buffer){
    Buffer = buffer;
  }
  public abstract IObservable<T> Make();
}

Issues

As you can see I can easily share Buffer without changing interface, because it represents inner data, and it’s data can’t be changed by user directly. But I have issue with the TContext. Because if I add it to IPlan.Execute(TContext context) as a parameter, then it will spoils the interface. Because some plan can be executed without context. And if I add the Context parameter to a constructor of an Plan implementation, than I’ll have to create some kind of update method to IPlan interface, which spoils interface as well. Furthermore, I have to update somehow TContext variable for Step as well in both cases. Please keep in the mind, that I’m going to store intstances of IPlan or ObservablePlan inside collection. There are different plans for different operations: create, delete, menu, etc… All of them will have the same type of context and the different type of buffers.

So I’m looking for a solution to use Context variable in such a way, that I can update the current plan with a new instance of Context without compromising the interface.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật