问题介绍
我通过 Craig Larman 的 Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development 学习了 OO 分析和设计。 ,遵循 UP(统一流程)。
在使用它时,我们通常绘制一个领域模型
,然后从中绘制交互/关联图,我们得到类图
。然后,我们通常制作 Controllers
,它将成为我们的 Model
和“外部世界”(遵循 GRASP 模式)之间的“门”。执行任何类型逻辑的所有世界访问都必须通过 Controllers
。我将这些 Controllers
称为 Model 的 Controllers
,因此稍后当我们讨论 MVC 的 Controllers
时,我们可以区分它们。
这可以在下面最棒的图表中描述:
alt text http://dl.dropbox.com/u/6187267/pic1.jpg
在黑色中,我们有我们的模型
的对象和该类的关联。在红色部分,我们有 Model Controllers
,它使用来自 Model
的数据。
基本上,按照这种设计,你只能通过所谓的Model's Controllers
来处理Model
(我们通常只有一个Controller
每个用例
!)。
现在是问题本身
在学习MVC的时候,我一直有一个疑问,MVC的Controller
到底是不是Model的Controller
?它们是同一个概念吗?我认为它们是不同的概念,因为我们过去所做的 Model Controllers
除了我们的 Model
上的类之外什么都不知道,这似乎不是发生在 MVC 的 Controllers
中。
如果我说的是真的,下图应该是有意义的:
alt text http://dl.dropbox.com/u/6187267/mvc_.png
我说得对吗?
最佳答案
我想我明白你在说什么:)
你在叫什么Model Controllers
被称为存储库。这是一个定义您与底层模型的特定交互的接口(interface)。例如,如果您有一个代表 Employees 的类,并且您想要做三件事:
-
List<Employee> List()
-
Add(int employeeID)
-
Delete(int employeeID)
然后您将定义一个接口(interface),名称如下:
public interface IEmployeeRepository
{
List<Employee> List();
void Add(int employeeID);
void Delete(int employeeID);
}
您将确保所有代码都处理接口(interface),而不是直接处理对象——存储库模式。这就是你所说的 Model Controller
.参见 http://www.dev102.com/2008/12/08/working-with-aspnet-mvc-part-2-the-model-and-the-repository-pattern/了解更多详情,或搜索 Repository Pattern。
MVC 世界中的 Controller 实际上是指示程序流逻辑的东西。例如,假设您有一个员工 ListView 。当用户点击此 View 上的“删除”按钮时, Controller 将加载适当的存储库并对其调用 Delete 方法。
tl;dr: MVC Controller = 当用户执行此操作时,在程序中执行此操作(程序逻辑);存储库(模型 Controller )= 定义我的模型中的对象支持哪些交互
是的,你是对的:)
关于c# - GRASP的Controllers和MVC的Controllers问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3316001/