简体   繁体   English

关于MVC 3中的模式

[英]About patterns in MVC 3

It is common sense to keep business logic out of controllers. 将业务逻辑置于控制器之外是常识。 It is also common sense that database access logic should be on a repository, using a repository pattern, as described here: Repository Pattern 这也是常识数据库访问逻辑应该上的存储库,使用一个仓库模式,如下所述: Repository模式

However, the repository pattern specifies only very simple low level database operations: Insert, Delete, Update, Select. 但是,存储库模式仅指定非常简单的低级数据库操作:插入,删除,更新,选择。 People advise to keep validation logic out of it too. 人们建议不要将验证逻辑也包括在内。 That is not a problem, since most of the validation can be put inside the model object itself. 这不是问题,因为大多数验证都可以放在模型对象本身内部。 The problem comes when we need to make some kind of cross-validation, that is, a validation that needs to look on more than one instance of the same model object (for example, ensuring that a name is unique accross all instances of same object) or, even worse, when the validation logic needs to check two or more objects of different types. 当我们需要进行某种交叉验证时,问题就来了,也就是说,验证需要查看同一模型对象的多个实例(例如,确保名称在整个相同对象的所有实例中都是唯一的) ),或者更糟的是,当验证逻辑需要检查两个或多个不同类型的对象时。 In this case, we have a real big hole: the business logic cannot be in controller, cannot be in the repository, cannot be in the object model itself (since the logic is not bound only to the object's properties). 在这种情况下,我们有一个很大的漏洞:业务逻辑不能在控制器中,不能在存储库中,也不能在对象模型本身中(因为逻辑不仅限于对象的属性)。 Where should this logic be? 这种逻辑应该在哪里? What is the best design pattern for this kind of requirement? 满足这种要求的最佳设计模式是什么?

您可以按照以下说明创建服务层: http : //www.asp.net/mvc/tutorials/older-versions/models- (data)/ validating- with-a-service-layer- cs

The problem here is that your design requires the UI to do validation based on business concerns. 这里的问题是您的设计需要UI根据业务问题进行验证。

The way to accomplish this is to abstract the validation into the business layer. 完成此操作的方法是将验证抽象到业务层。 Your business layer may have methods like ValidateUserIsUnique() and then your ui calls into this layer and receives a result, which is then used for validation. 您的业​​务层可能具有ValidateUserIsUnique()类的方法,然后您的ui会调用此层并接收结果,然后将其用于验证。

In particular, for client-side validation MVC provides the RemoteValidationAttribute , but this will only do client-side validation. 特别是,对于客户端验证,MVC提供了RemoteValidationAttribute ,但这只会执行客户端验证。 You will also need to do a server-side validation that calls the same (or a similar) function on the server. 您还需要执行服务器端验证,以在服务器上调用相同(或相似)功能。

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

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