MVC,状态模式,对象在数据库中的保存状态 [英] MVC,state pattern, saving state of object in the database

查看:120
本文介绍了MVC,状态模式,对象在数据库中的保存状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何在MVC中实现状态模式以及如何将对象的状态实际保存在数据库中,我有几个问题.

对象(背景)具有对接口状态
的引用 特定的 State 对象由控制器对象通过分配实现 State
接口的特定类来设置

我有几个状态,但是假设我们有两个可能的状态:
-活动
-无效


为对象分配活动状态,但是如何在数据库中保存有关对象状态的信息(EntityFramework 4),即当我们从数据库中获取数据时,该状态为对象的已知状态?对属性设置一个可能的状态的枚举字段,并保存当前状态,当实现该状态的特定类将需要设置状态时,是否需要此信息?

我的问题与策略模式有关-如何为给定对象保存有关所选策略的信息?

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?

推荐答案

您好arturw82,

可以通过序列化存储处于当前状态的对象.您可以将对象序列化为二进制或xml.对于高于3.0的框架,您可能使用DataContract Serialization.对于您的上下文对象,将一个成员字段作为myStateXML,可能是一个字符串字段.当需要存储时,可以有方法将状态对象序列化为xml字符串并存储在字符串字段中,并将上下文移至持久性.在构造函数中取回该字符串时,请检查该字符串(如果可用)反序列化到状态对象并初始化状态对象字段.如果要使用二进制序列化,则可能有一个字段作为字节数组.

这样为您工作吗?

我不确定状态对象与上下文对象之间没有父子关系,实体如何对待它.所以我在考虑我的描述方式.您也可以期望其他专家提供更好的答案.祝你好运.
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.


This post might help you.


HTH


这篇关于MVC,状态模式,对象在数据库中的保存状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆