移动验证,HTML辅助,以服务于MVC层 [英] Moving Validation, Html Helpers to Service Layer in MVC

查看:98
本文介绍了移动验证,HTML辅助,以服务于MVC层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有两个WebApplication的一个MVC解决方案,一个用于台式机和其它移动版本。

该架构是这样的:


  • 型号Libraray(包括库和DB型)

  • 服务层(业务逻辑)

  • Web应用程序项目(视图,控制器的ViewModels桌面)

  • 移动网络项目(移动视图,控制器,视图模型)

我添加了一个服务层到这个项目 HTTP: //www.asp.net/mvc/tutorials/validating-with-a-service-layer-cs )。

只要这两个网络项目有不同的控制器,但最常见的逻辑,以下我可以移动到服务层。


  • HTML辅助类

  • 查看模型验证

  • 常用功能

先谢谢了。


解决方案

首先回答你在哪里立场的$ C $问题CS


  1. 查看模型验证结果
    如果使用服务层模型验证我宁愿建议你使用模型库中的数据的注释做模型验证没有特别的理由。所有您需要做的是一些属性添加到您的实体的属性和事情很快就会过去,包括验证消息(盒子如果使用添加验证元素 Html.ValidationMessageFor()或在您的客户端查看类似)。


  2. HTML辅助类结果
    HTML辅助类应该是共同的UI库的一部分,如果他们应该被共享。他们不以任何现有图层的属于,因为所有的人都是独立的presentation层(Web应用程序)。我想你的HTML佣工将presentation层,因此依赖独立的库就足够了。


  3. 常用功能结果
    取决于你需要使用常用功能,他们可能也有示范库或服务层的一部分。


如何构建我的web应用程序

写为层以下。他们在整个解决方案过程中独立的项目,使他们成为独立的组件。


  1. 对象层 - 包含任何层使用的所有普通类/枚举/接口,一般的功能和应用模型波苏斯;这就是为什么它被所有其他层引用


  2. 数据层 - 有数据访问对象(不妨从波苏斯继承的对象层)只此层内使用,并进行映射,并从应用层波苏斯;也有存放区在该层,因此该层/组件由下一层引用(服务)


  3. 服务层 - 包含业务逻辑和操作应用模型类;从presentation层获取数据,并使用数据层来操纵后备存储的数据(无论它可能是 - 这是数据层库的一部分,当然​​与存储通信)


  4. presentation层 - 可能是一个Web应用程序或其他任何东西;引用对象和服务层,它能够进行沟通;这是只有在这里也创造了presentation层的任何对象(通过意见,但不是按服务需要特殊的视图模型);


所有这些意味着,我的对象层通过提供用于各层之间交换数据的通用类提供层之间的通信手段。

在那里有某些其他提供商我需要实现它们可以是一个单独的层的一部分,但上层分层作品的所有应用程序的90%的病例。一个很好的例子将是被在不同应用中重复使用一些常见的库。根据是什么库提供它得到由这些层/组件需要它的功能被引用。

在你的情况下,你有两个Web应用程序,我将创建一个名为一层特殊的 presentation 的,所有常用的HTML功能会,然后在这两个网络应用程序引用它。

We have two WebApplication in one MVC solution, one for Desktop and the other for Mobile version.

The architecture looks like this:

  • Model Libraray ( including repository and DB Model )
  • Service Layer ( Business Logic )
  • Web application Project ( views , controller, viewModels for desktop )
  • Mobile web project ( Mobile views, controller, viewModel)

I added a service layer into this project http://www.asp.net/mvc/tutorials/validating-with-a-service-layer-cs).

as long as these two web projects have different controller but most common logic , which of following I can move to service layer.

  • HTML Helper classes
  • View Model validations
  • Common functions

Thanks in advance.

解决方案

First to answer your question of where to position those codes

  1. View Model validations
    If there is no particular reason to use the service layer model validation I'd rather advise you to do model validation using data annotations within Model library. All you have to do is to add some attributes to your entities' properties and things will work out of the box including validation messages (if you add validation elements using Html.ValidationMessageFor() or similar) in your client side view.

  2. HTML Helper classes
    HTML Helper classes should be part of Common UI library if they're supposed to be shared. They don't belong in any of the existing layers, because all of them are presentation layer (web apps) independent. I suppose your HTML helpers will be presentation layer dependent hence a separate library would suffice.

  3. Common functions
    Depending on where you need to use common functions, they may as well be part of Model library or Service layer.

How I structure my web apps

The following are written as layers. They're of course separate projects in the whole solution so they become separate assemblies.

  1. Objects layer - contains all common classes/enums/interfaces, general functionality and application model POCOs used by any layer; that's why it's referenced by all other layers

  2. Data layer - has data access objects (that may as well be inherited from POCOs in the Objects layer) that are only used within this layer and perform mapping to and from application layer POCOs; There are also repositories in this layer hence this layer/assembly is referenced by the next layer (service)

  3. Services layer - contain business logic and manipulate application model classes; gets data from presentation layer and uses Data layer to manipulate data in the backing store (wherever it may be - this is of course part of Data layer repository to communicate with the store)

  4. Presentation layer - may be a web application or anything else; references Objects and Services layer so it's able to communicate; any objects that are presentation layer only are also created here (special view models required by views but not by services);

All these mean that my Objects layer provides means of communication between layers by providing common classes that are used to exchange data between layers.

In cases where there are certain other providers I need to implement they may be part of a separate layer, but upper layering works for the 90% of all applications. A good example would be some common library that gets reused across different applications. Depending on what that library provides it gets referenced by those layers/assemblies that need its functionality.

In you case where you have two web applications, I would create a special layer named Presentation where all common HTML functionality would be and then reference it in both web applications.

这篇关于移动验证,HTML辅助,以服务于MVC层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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