How to implement the concept of .Net RIA Service (the only application logic) in Asp.Net MVC?

First, let's look at the following picture, which explains the concept of .Net RIA Service .

alt text
(source: nikhilk.net )

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

Then 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; } } } 

After that, I create some custom Model Binder to validate the data when users publish it to the controllers. Thus, I can make sure that each Model is valid before saving it.

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

However, some view pages do not require all the fields in the data type. You need several fields in the data type. I cannot split this data type into multiple interfaces, depending on which data field the browse page requires. Because some data fields are duplicated. Moreover, it will also separate the application logic.

for example

  1. The LogOn view uses only 2 fields, including LogOnName and HashedPassword.
  2. The ChangePassword view uses only 2 fields, including Id and HashedPassword.
  3. The UserProfile view uses 4 fields, including ID, LogOnName, HashedPassword, and Description.

Do you have any ideas for solving this problem? I think this is very similar to the concept of AOP.

By the way, I can solve this problem by adding a list box that contains unused fields. But this idea is pretty 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; } } } 

Thanks,

+1
oop asp.net-mvc
source share
1 answer

The main problem here seems to be one of the following:

  • Context validation rules are expressed as invariants
  • Entities with unsatisfactory invariants are prematurely created / verified

I would suggest that you are not trying to use model bindings to create and check types for which all invariant information is not available from an Http request. In the case of LogOn, the only information you have is a username and password. Therefore, LogOn should not expect a user type, but rather a username and password, possibly encapsulated in the credential type.

0
source share

All Articles