MVC和观察者模式 [英] MVC and Observer pattern

查看:201
本文介绍了MVC和观察者模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中执行Observer模式时遇到问题。
该项目必须作为C#中的MVC,像Windows应用程序。


在我的域模型中,我有例如Country类和Country库。我有一个国家控制人和观看所有国家(表单上的列表),添加新国家和编辑现有国家的意见。


我不知道有关与更改国家相关的更改有多少意见。事情是我必须使用观察者模式。而在网络上,当主题是国家和观察者是一个正在编辑国家的表单,所有的示例都在控制台应用程序中时,我只能找到考试。



我需要所有具有国家列表的表单都知道添加新的国家,而不仅仅是编辑现有的国家。我应该如何做这个,我应该做一个存储库一个主题?

解决方案

你可以创建一个ManageCountry任务,这可以让您编辑/添加国家/地区。
此任务可能有一个事件CountryChanged,其他视图可以订阅。



当您修改国家/地区或创建新的国家/地区时,您会提出事件,订阅者可以对其进行反应。
你只需要确保你提出的事件使用一些自定义的EventArgs,以便可以将已修改的Country对象传递给eventhandler。



你用什么来实现一个MVC应用程序?你使用某种框架吗?


I am having problems with implementing Observer pattern in my project. The project has to be made as MVC in C#, like a Windows application.
In my domain model I have for example Country class and Country repository. I have a Country controller and views for seeing all countries(a list on a form), adding new country, and editing existing country.
I don't know how many views will have to know about changes connected with changing Country. The thing is I have to use Observer pattern. And on the web I can find only examlpes when a Subject is Country and Observer is a form that is editing a country and all the examples are in console application.

I need that all my forms that have lists of countries know about adding new country, not just editing existing ones. How am I supposed to do that, should I make a repository a Subject?

解决方案

You could create a 'ManageCountry' task, which lets you edit / add countries. This task could have an event 'CountryChanged', where other views can subscribe to.

When you modify a country, or create a new country, you raise the event, and subscribers can react to it. You just have to make sure that the event you raise, uses some custom EventArgs so that you can pass the Country object that has modified to the eventhandler.

What are you using to implement an MVC app ? Are you using some kind of framework for it ?

这篇关于MVC和观察者模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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