我需要使用状态模式进行数据审批流程吗?
我们系统的用户可以提交未经验证的联系数据。例如:
- 名字:null
- 姓氏:231
- 电话号码:不确定
- 等
此数据存储在 PendingContacts 表中。
我有另一个表 - ApprovedContacts。该表具有多种约束来提高一致性和完整性。该表不应包含任何脏数据或不完整数据。
我需要一个将数据从一个表移动到另一个表的过程。两个表的结构几乎相同,但是,一个表具有约束,而另一个表则没有。
我有两种状态:待处理和已批准,直觉告诉我应该使用状态模式详细信息。理论上,这应该允许我将联系人的状态从“待处理”更改为“已批准”,具体取决于联系人是否已成功验证。问题是我不明白这是如何运作的。
我的方向是正确的还是应该考虑一些完全不同的东西?
表示层位于 MVC 3 中,因此我有待处理联系人和已批准联系人的视图模型,以及待处理联系人和已批准联系人的域模型。我的视图模型通常是具有一些验证例程的 DTO,但现在我的视图模型也表示状态。这似乎不对。
例如,所有联系人都必须有一个状态,并且可以保存和删除,因此我需要一个接口:
public interface IContactViewModelState
{
void Save(ContactViewModel item);
}
然后我添加一个实现,用于将挂起的联系人保存到 PendingContacts 表中:
public class PendingContactViewModelState: IContactViewModelState
{
public void Save(ContactViewModel item)
{
// Save to the pending contacts table
// I don't like this as my view model now references data access layer
}
}
Users of our system are able to submit un-validated contact data. For example:
- Forename: null
- Surname: 231
- TelephoneNumber: not sure
- etc
This data is stored in a PendingContacts table.
I have another table - ApprovedContacts. This table has a variety of constraints to improve consistency and integrity. This table shouldn't contain any dirty or incomplete data.
I need a process to move data from one table to another. Structure of both tables is nearly identical, however, one table has the constraints, when another doesn't.
I have two states: Pending and Approved, gut feeling tells me that I should use a state pattern details here. In theory this should allow me to change contact's state from Pending to Approved, depending on whether the contact has been successfully validated. Problem is that I don't see how is this going to work.
Am I going in a right direction or should I be looking at something completely different?
Presentation layer is in MVC 3, so I have view models for pending contacts and approved contacts, as well as domain models for pending contacts and approved contacts. My view models are generally DTOs with some validation routines, but now my view models represent state too. This doesn't seem right.
For example, all contacts must have a state and they can be saved and removed , so I need an interface for that:
public interface IContactViewModelState
{
void Save(ContactViewModel item);
}
I then add an implementation for saving pending contacts into the PendingContacts table:
public class PendingContactViewModelState: IContactViewModelState
{
public void Save(ContactViewModel item)
{
// Save to the pending contacts table
// I don't like this as my view model now references data access layer
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的回答:不,因为你只有两种状态。您可以使用状态模式来帮助处理具有许多状态和规则的复杂情况。您可能想要采用基于状态模式的成熟实现的唯一原因是,如果您很有可能出现这种情况。
如果成功转换到已批准的结果是记录最终出现在已批准的表中,那么您实际上只需要决定要在何处强制实施约束。该决定将/可以基于许多因素,包括可能的更改频率(针对约束)以及其他逻辑所在的位置。
许多模式(但不是全部)倾向于处理如何构建应用程序,但在这里我认为这只是决定在何处以及如何实现某些逻辑的情况。换句话说 - 你可能只是不小心过度分析了问题 - 这很容易做到:)
Short answer: no, because you only have two states. You'd use a state pattern to help deal with complex situations with many states and rules. The only reason you might want to go with a full-blown state pattern based implementation is if you there's a very high chance such a situation is imminent.
If the result of a success transition to Approved is the record ending up in the approved table then you really just need to decide where you want to enforce the constraints. This decision will/can be based on many factors including the likely frequency of change (to the constraints) and where other logic resides.
A lot of patterns (but not all) tend to deal with how to structure an application, but here I think it's just a case of deciding where and how implementing some logic. In other words - you might just be accidentally over-analyzing the problem - it's easily done :)