简体   繁体   English

在C#中管理对象依赖关系的设计模式

[英]Design pattern to manage object dependencies in C#

I m building a website in ASP.NET MVC 4 C#. 我正在ASP.NET MVC 4 C#中建立网站。 Requirement is as follows: 0. Build an order by collecting required details across several pages. 要求如下:0.通过跨多个页面收集所需的详细信息来构建订单。

  1. User should be able to navigate back and forth (to check the values entered) before submitting the order finally. 在最终提交订单之前,用户应该能够来回导航(以检查输入的值)。

  2. There may be a dependency wrt data collected in subsequent pages based on the current selection. 基于当前选择,在后续页面中收集的数据可能存在依赖性。 For example, if I change the country to which product needs to be shipped, custom duties & taxes applicable in the next page needs to be changed. 例如,如果我更改了产品需要运送到的国家/地区,则需要更改下一页中适用的关税和税款。 Hence data in subsequent pages need to be invalidated. 因此,后续页面中的数据需要无效。 If modification to a field does not impact any other data(change in quantities ordered), current selection needs to be persisted. 如果对字段的修改不影响任何其他数据(订购数量的变化),则需要保留当前选择。

For scenario 1, I m planning to use Memento pattern. 对于方案1,我打算使用Memento模式。 The object will be serialized and persisted in database. 该对象将被序列化并保存在数据库中。 However, I m not sure how to deal with scenario 2. I m sure there would be a design pattern that I can use here. 但是,我不确定如何处理场景2。我确定这里会有一个设计模式。 A code sample would be definitely helpful. 代码示例绝对有帮助。

Initially I thought of Observer pattern. 最初我想到了观察者模式。 However, I do not have any subscribers active to be acted on the change.(Values will be saved in DB and will be loaded of next/previous page). 但是,我没有任何活动的订阅者可以进行更改。(值将保存在DB中,并将加载到下一页/上一页)。 Also, we are mostly looking at a single entity of storage here (Field 1 and 2 will be populated on page 1, field 3,4,5 in page2......etc) 另外,我们主要在这里查看单个存储实体(字段1和2将填充在页面1上,字段3、4、5填充在页面2中……等等)

I would separate the responsibilities like this for your scenarios: 1. managing the page navigation and interactions 2. awareness of state of the object 对于您的方案,我将这样划分职责:1.管理页面导航和交互2.了解对象状态

Because you are going to have a complex page navigation based on your scenario, I would recommend you to use a Mediator to manage the interaction/communication between pages. 由于您将根据自己的情况进行复杂的页面导航,因此建议您使用介体来管理页面之间的交互/通信。 In the other hand you need to somehow manage the state of the object and based on that state you would need to invalidate and take action, so you also would need to have a State pattern. 另一方面,您需要以某种方式管理对象的状态,并且基于该状态,您将需要使对象无效并采取措施,因此,您还需要具有状态模式。

After this, there is one level upper that has the knowledge about what the scenario is that you can use something like usecase/application controller that manages your scenario using those implemented patterns (Mediator, State). 在这之后,有一个上层了解方案是什么,您可以使用诸如用例/应用程序控制器之类的东西来使用那些已实现的模式(介体,状态)来管理方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM