locked
State Pattern Help RRS feed

  • Question

  • User1034446946 posted

    Hi

    I have been reading this article about the state pattern:

    http://dotnetcodr.com/2013/05/16/design-patterns-and-practices-in-net-the-state-pattern/

    And I want to use it (for the first time), however I can't figure out where the interface know which OrderState object to use out of the 3, I also not sure why there is a change propery in the object class.

    A few other questions.

    I have entity models and viewModels, I assume I put this int he viewModels because otherwise it will make my entities a mess and the entityframework won't be able to search by the status?

    If that where do I put the state objects? my service layer is free from viewModels, would it be better to add a service folder to my view models project to keep them there?

    Also I have only just figure out this pattern although used a few different ones in the past, is it common to build many a project, then have to convert it into patterns when you know them, or should I know them and then start programming?

    Any information would be appriciated.

    Friday, January 29, 2016 10:06 PM

Answers

  • User475983607 posted

    enendaveyboy

    And I want to use it (for the first time), however I can't figure out where the interface know which OrderState object to use out of the 3, I also not sure why there is a change propery in the object class.

    The interface does not determine which OrderState object to use.  The OrderState object is passed into Order object's constructor.  

    public Order(IOrderState orderState)
    {
       _orderState = orderState;
    }

    This means the initial state is known when the Order is created; otherwise you would not be able to apply the business rules.

    enendaveyboy

    I also not sure why there is a change propery in the object class.

    The Change method sets the private _orderState variable which in turn updates all the properties.  This method would be inside a condition like if(Order.CanShip) {// process shipping }

    void Change(IOrderState orderState)
    {
      _orderState = orderState;
    }

    enendaveyboy

    If that where do I put the state objects? my service layer is free from viewModels, would it be better to add a service folder to my view models project to keep them there?

    You would have a look up table that defines each state, an order state table.  You would have an Order table that contains orders and a table that joins (cross reference) the order table with the order state table.  

    Alternatively, you could store the state in the Order table if you don't care about state history.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 31, 2016 4:41 PM

All replies

  • User475983607 posted

    enendaveyboy

    And I want to use it (for the first time), however I can't figure out where the interface know which OrderState object to use out of the 3, I also not sure why there is a change propery in the object class.

    The interface does not determine which OrderState object to use.  The OrderState object is passed into Order object's constructor.  

    public Order(IOrderState orderState)
    {
       _orderState = orderState;
    }

    This means the initial state is known when the Order is created; otherwise you would not be able to apply the business rules.

    enendaveyboy

    I also not sure why there is a change propery in the object class.

    The Change method sets the private _orderState variable which in turn updates all the properties.  This method would be inside a condition like if(Order.CanShip) {// process shipping }

    void Change(IOrderState orderState)
    {
      _orderState = orderState;
    }

    enendaveyboy

    If that where do I put the state objects? my service layer is free from viewModels, would it be better to add a service folder to my view models project to keep them there?

    You would have a look up table that defines each state, an order state table.  You would have an Order table that contains orders and a table that joins (cross reference) the order table with the order state table.  

    Alternatively, you could store the state in the Order table if you don't care about state history.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 31, 2016 4:41 PM
  • User1034446946 posted

    Thanks for the info

    Wednesday, February 3, 2016 12:55 AM