lundi 6 août 2018

Best deisng pattern to handle action oriented requirement

I have an entity let’s say Order. Order has a Status property. Status will be Enum’s like New, Processed, Disable..etc. I have set of Actions like, DeleteOrder, DuplicateOrder..etc.

Actions will be available based on status. I need to get list of Actions when I pass Instance of Order to a method. How can I achieve this. I have following pattern.

public interface IAction
{
    void Process();
}

public interface IActionFactory
{
    IAction GetAction(OrderActionModel model);
    List<string> GetActions(Order order);
}

public class ActionFactory : IActionFactory
{
    private readonly IMailService _mailService;

    public ActionFactoryIMailService mailService)
    {
        _mailService = mailService;
    }

    public IAction GetAction((OrderActionModel model)
    {
        switch (model.Action)
        {
            case DeleteOrder:
                    return new CanDeleteOrder(model, _mailService);
            case DuplicateOrder:
                    return new EditWorkInstruction(model, _mailService);
            default:
                    return null;
        }
    }

    public List<string> GetActions(Order order)
    {
        //How to call isValidAction from here for each list of actions
    }
}

public class CanDeleteOrder : IAction
{
    private (OrderModel _model;
    private readonly IMailService _mailService;

    public CanDeleteOrder(OrderModel model, IMailService  mailService)
    {
         _model = model;
         _repository = repository;
    }

    public void Process()
    {
        throw new NotImplementedException();
    }

    //This is I dont know is valid or not
    public bool IsValidAction(Order order)
    {
         return order.Status == OrderStatus.New;
    }
}

public IHttpActionResult Action(OrderModel model)
{
    IAction action = null;

    switch (model.Action)
    {
        case DeleteOrder:
           action = _actionFactory.GetAction(model);
           break;

        case RequisitionerAction.UpdateStatus:
           action = _actionFactory.GetAction(model); 
           break;
   }

   action.Process();
   return Ok();
}

public IHttpActionResult GetActions(1)
{
    var order = repo.getOrder(1)
    //Some mthod to get Actions
    return Ok(actions);
}

Aucun commentaire:

Enregistrer un commentaire