MVC3中的"remote“注释允许您调用一个”action“来为您执行属性数据验证。很漂亮!或…真的吗?
问题:下面的“远程”注释(参见注释)调用客户端中的代码!!我的角色类在模型中。我喜欢“远程”,因为我不需要编写自定义验证器。
我是否应该使用对象视图模型模式,并在其中复制属性“Role.Name”?这样就行了。然后还有另一个问题:我如何真正避免DRY原则(不要重复自己)?在对象视图中使用带有注释的属性,然后在模型中使用相同的属性,是否有效?我的意思是,对于分离关注点来说,这是不是太多的工作?
我只是在尝试设计正确的代码,并应用正确的设计原则,这样我就不会在这个网站的代码增长时被烧伤。
做这件事最好的方法是什么?
namespace StartWeb.Model.ObjectModel
{
public class Role //this class is in the Model (see namespace) and it needs to be "client agnostic”
{
//Then, this annotation is NOT client agnostic, it calls a controller:
[Remote("ValidateRoleName", "Role", AdditionalFields="InitialRoleName", ErrorMessage = "Role Name already exists")]
public string Name { get; set; }这是RoleController操作中的验证代码(在“client”中):
[HttpGet]
[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
public JsonResult ValidateRoleName(string name, string initialRoleName)
{
bool isValid = true;
if (name != initialRoleName) isValid = !(new SecurityFacade().IsRoleNameExist(name));
return Json(isValid, JsonRequestBehavior.AllowGet);
}发布于 2011-12-07 22:26:06
经过进一步的研究,我发现了这个开源工具,它可以为您从不同的层映射实体:
Automapper tool between domain model and view models
https://stackoverflow.com/questions/8402290
复制相似问题