如何在Asp.Net MVC中实现.Net RIA服务(单一应用逻辑)的概念? [英] How to implement concept of .Net RIA Service (Single App Logic) in Asp.Net MVC?

查看:58
本文介绍了如何在Asp.Net MVC中实现.Net RIA服务(单一应用逻辑)的概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,让我们看下面的图片,它解释 .Net RIA服务.

First, let's see the following picture that explain the concept of .Net RIA Service.


(来源: nikhilk.net )


(source: nikhilk.net)

如您所见,应用程序具有应用程序逻辑(业务规则),可以在服务器端(数据库+存储库+外部服务)和客户端(asp.net网页+ Silverlight + WCF)同时实现

As you see, the application has app logic (business rule) that can be implemented both server side (databases + Repositories + external services) and client side (asp.net web page + Silverlight + WCF)

接下来,我创建一些包含一些验证规则的数据类.

Next, I create some data class that contains some validation rule.

namespace [SolutionName].Models
{
    public interface IUser
    {
        Guid ID { get; set; }

        [Required]
        [StringLength(15)]
        [RegularExpression("^[a-zA-Z][a-zA-Z_]+$")]
        string LoginName { get; set; }

        [Required]
        [StringLength(255)]
        string HashedPassword { get; set; }

        DateTime CreateTime { get; set; }

        [StringLength(255)]
        string Description { get; set; }

        [Required]
        Role Role { get; set; }
    }
} 

此后,我创建了一些自定义Model Binder,用于在用户将数据发布到控制器时验证数据.因此,在保存之前,我可以确保每个模型都是有效的.

After that, I create some custom Model Binder for validating data when users post it to controllers. So, I can ensure that every Model is valid before I save it.

public ActionResult SaveData()
{
    if(ModelState.IsValid)
    {
        // logic for saving data
    }
    else
    {
        // logic for displaying error message
    }
}

但是,某些视图页面并不需要数据类型中的所有字段.它需要一些数据类型的字段.我不能将这种数据类型分为多个接口,这取决于查看页面所需的数据字段.因为某些数据字段是重复的.而且,它也将分离应用程序逻辑.

However, some view page doesn't require all of fields in data type. It needs some of field in data type. I can't separate this data type into multiple interfaces depend on what data field that view page requires. Because some of data fields are duplicate. Moreover, it will separate app logic too.

例如

  1. 登录视图仅使用2个字段, 包括LogOnName和 HashedPassword.
  2. ChangePassword视图仅使用2个字段,包括ID和 HashedPassword.
  3. UserProfile视图使用4个字段,包括ID,LogOnName, HashedPassword和说明.
  1. LogOn view uses only 2 fields, including LogOnName and HashedPassword.
  2. ChangePassword view use only 2 fields, including Id and HashedPassword.
  3. UserProfile view uses 4 field, including ID, LogOnName, HashedPassword and Description.


您有解决此问题的想法吗?我认为这很像AOP概念.


Do you have any idea for solving this problem? I think it much like AOP concept.

顺便说一句,我可以通过添加一个包含未使用字段的列表字段来解决此问题.但是,当我将它用于包含100多个字段的大型数据类型时,这个想法就很糟糕.

By the way, I can solve this by adding a list field that contains unused fields. But this idea is quite bad when I use it with a large data type that contains more than 100 fields.

namespace [SolutionName].Models
{
    public interface IUser
    {
        /*
            Some defined data type
        */

        // All fields that is contained in this list won't be validated by defined rules.
        List<string> unusedFields { get;set; }
    }
} 

谢谢

推荐答案

这里的基本问题似乎是以下问题之一:

The fundamental issue here seems to be one of the following:

  1. 上下文验证规则被表示为不变量
  2. 具有未满足的不变性的实体正在过早地创建/验证

我建议您不要尝试使用Model Binders实例化和验证无法从Http请求获得所有不变信息的类型.对于LogOn,唯一的信息就是用户名和密码.因此,LogOn不应期望用户类型,而应该期望用户名和密码,这些用户名和密码可能封装在 Credentials 类型中.

I would suggest that you don't attempt using Model Binders to instantiate and validate types for which all invariant information isn't obtainable from the Http request. In the case of LogOn, the only information you have is the user's name and password. Therefore, LogOn shouldn't expect a User type, but rather the username and password, perhaps encapsulated in a Credentials type.

这篇关于如何在Asp.Net MVC中实现.Net RIA服务(单一应用逻辑)的概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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