Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a few questions about how to implement state pattern in the MVC, and actually save the state of the object in the database.

Object (background) has a reference to the interface State
The specific State object is set by the controller object by assigning a specific class implementing the interface of the State


I have a few states, but suppose we have two possible states:
- Active
- Inactive


Assign a status of Active to the object, but how to save the information about state of object in the database (EntityFramework 4), that when we get data from the database was known state of object? Make an Enumeration field of possible states to the Property, and save the current state, and when specific class that implements the state will need to set the state auoub this information?

My question concerns a Strategy pattern - How to save information about the selected strategy for a given object?
Posted

Hi arturw82,

An object at its current state can be stored by serializing it. You may serialize an object into binary or xml. For framework above 3.0 you may have use DataContract Serialization. For your context object have a member field as myStateXML, a string field probably. When need to store you can have method to serialize the state object to an xml string and store in the string field and move the context to persistence. When retrieving back at the constructor check for that string, if available deserialize to the state object and initialize the state object field. If you want to use binary serialization then you may have a field as byte array.

Is this way work for you?

I am not sure as the state object has no parent child relationship with the context object how entity going to treat it. So I am thinking about the way I described. You could expect better answers from other experts as well. Good luck.
 
Share this answer
 
This post might help you.


HTH
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900