Files
Claudiu Farcas 99c758b38e command notes
2021-04-26 08:53:33 +03:00

100 lines
2.9 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Windows.Input;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace design_patterns.behavioral.command
{
/// <summary>
/// Command is a behavioral design pattern that turns a request
/// into a stand-alone object that contains all information about the request.
/// This transformation lets you pass requests
/// as a method arguments, delay or queue a requests execution,
/// and support undoable operations.
///
/// Use when:
/// - you want to parametrize objects with operations.
/// - you want to queue operations, schedule their execution,
/// or execute them remotely. (serialize commands)
/// - you want to implement reversible operations.
/// </summary>
public class CommandSample
{
public static async Task Run()
{
Console.WriteLine("Behavioral - Command");
var order = new Order {
ID = Guid.NewGuid().ToString()
};
List<ICommand> operations = new List<ICommand>();
operations.Add(new AddOrderCommand(order));
operations.Add(new RemoveOrderCommand(order.ID));
// batch execution
foreach(var cmd in operations){
cmd.Execute();
}
// undo
operations.Reverse();
foreach(var cmd in operations){
cmd.Undo();
}
}
}
public interface ICommand
{
bool HasSucceded {get;set;}
void Execute();
void Undo();
}
public class AddOrderCommand : ICommand {
public AddOrderCommand(Order order) => this.Order = order;
public Order Order { get; private set; }
public bool HasSucceded { get; set; }
public void Execute()
{
Console.WriteLine($"Execute add order command for order {Order.ID}");
// this should be set by a command processor externally
HasSucceded = true;
}
public void Undo()
{
if(HasSucceded){
Console.WriteLine($"UNDO - add order for order {Order.ID}");
}
}
}
public class RemoveOrderCommand : ICommand {
public RemoveOrderCommand(string orderId) => this.OrderID = orderId;
public string OrderID { get; private set; }
public bool HasSucceded { get; set; }
public void Execute()
{
Console.WriteLine($"Execute remove order command for order {OrderID}");
// this should be set by a command processor externally
// FORCE failure
HasSucceded = false;
}
public void Undo()
{
if(HasSucceded){
Console.WriteLine($"UNDO - remove order for order {OrderID}");
}
}
}
public class Order {
public string ID { get; set; }
}
}