How to persist objects which implement the State pattern?

c#, design-patterns, software-design, state-machine, state-pattern

Solution

Instances of States don't have state themselves, so all you need to save is each State's identity. It's not a good idea to save the State class name in the database, because the database would have to change if you changed the State class name. Instead,

- give each State class a member with an Enum value that is unique to the state.

- When you persist the object that has the State, persist the Enum.

To get the state back when you load the object, either

- instantiate the object's State member immediately and assign it to the object's State member, or

- if it's expensive to instantiate a State, change the object to access the State member through a method and lazily instantiate the State in that method based on the value of the State identity Enum member.

Either way, you need to be able to go from an Enum value to a State. Do that by looping through all of the relevant State classes until you find the one whose identity value matches.

So, what are the relevant States? It depends on who's writing State classes.

In the simple case, where you control the entire program and all of the State classes in the program are potentially members of the State-having object, you can just loop over all the subclasses or implementers of the State superclass or interface, like this: Getting all types that implement an interface.

If for some reason there are State classes that you don't want to loop over, just define a list of the ones that you do want to loop over in a constant or (if you want to change it without changing the code) in configuration.

If making your list of State classes is slow, just do it once at program startup or first use. If you hardcode the list, don't do that in the State-having class (it should be independent of specific States) or in the State superclass (that would introduce a circular dependency); put the list higher up (dependency-wise) in your program or (as Farhad suggested) in its own class.

There are a lot of examples how how to persist objects with State out there; this one is relatively straightforward.

Problem

I am new to the State design pattern and I can't find a proper example of saving different states of an object to the database (SQL Server in my case). The scenario is quite similar [almost identical] to the example described in the following article, however I have not found an applicable solution for persisting the states to the database. Can you guys recommend a link or possiblly give an example? State Pattern Usage and Sample in C# In addition: How do you enumerate all different ConcreteState types at run time? For instance, if you have 10 different states, do you declare an EnumStates with 10 different members and give every single ConcreteState member an associated EnumStates member, or you do get all the distinct states by getting the subclasses of ConcreteState? For you information, I need to be able to search for entities based on their different states.

Original source

Related problems