Skip to main content

简单的MVC就够了吗?浅谈service Layer的引入



MVC是web开发中常见的程序结构。
简单的mvc结构如下:
view层:显示层。
control层:业务层,集合了各种action。
model层:模型层,一般和数据打交道。简单的sample:一个表对应一个model类。
其中control层调用model层的方法,实现对数据的访问。

采用这样的结构在一定程度上,可以做到代码清晰,较容易扩展,代码的管理复杂度较低。
但是如果是业务很多,逻辑又很复杂的网站,如果再加上开发人员的水平参差不齐,那必然会导致下面的情况:
1 action中的代码越来越长,逻辑越来越复杂,不同action之间看起来有很多可以重用的代码, 但是真要进行重构的话,又非常困难。
2 model层中包含的方法越来越多,有些方法也过于复杂。甚至在不少方法中还包含了业务逻辑。
3 代码的修改,还是牵一发而动全身。
4 代码难以进行自动化测试。

本来以为引入了mvc,程序的管理复杂度问题就高枕无忧了,但现在又面临了相同的问题了。

以我最近的所学看,在mvc中再引入service层,可以在很大程度上避免或者缓解上述问题。
原有的mvc结构改成如下:
1 view层:显示层。
2 control层:业务层,集合了各种action。
3 service层。
4 DAO层。
原来的model层不见了,增加了service层和DAO层。DAO,即Data Access Object,数据访问接口,数据访问:顾名思义就是与数据库打交道。

在这个结构中,control不直接和DAO联系,
需要操作数据的时候,通过service层访问DAO层来实现。
service层做的事情,不仅仅是调用DAO操作数据,还会包含了一定的业务逻辑。整个程序的设计,也变成了针对服务进行设计。

这样做的好处是:
1 control层中的action得以精简,因为action中的一些逻辑,被重构成一个个的服务。而不同的action也可以重用服务了。
2 只负责和数据打交道的DAO层,相比之前的model层,也得以精简(DAO层尽量只做最原子的数据操作,不同数据操作之间的联系,这边不考虑,那是service层的事情)。
3 service层可以实现很大程度上的代码复用,程序的功能封装更清晰了。
4 由于service层更加清晰的定义了应用程序的边界,那么对于各个service函数(对应某个服务/应用),要做到自动化测试就方便多了。WEB程序如何做到能方便的进行单元测试,这是一直困扰我的难题,这样的设计似乎真的可行了~
5 开发人员的工作分配,理论上真的可以按层次划分了。只是理论上~

同时,这样的设计模式也是存在一定的缺点的:
层次太多,刚接触的开发人员理解起来比简单的mvc结构费时;
service层的设计需要一定的功力,因为action中和model层的逻辑在很大程度上转移到这里了。

但整体上看,service Layer的引入,更加清晰的定义了应用程序的边界,提供了一系列可以重用的操作集合。这对于网站的可扩展性和可维护性是非常有帮助的。

from http://www.blogjava.net/wealupa/archive/2010/10/07/333904.html

Comments

Popular posts from this blog

Regression Testing

From  http://www.softwaretestinghelp.com/regression-testing-tools-and-methods/ The selective retesting of a  software  system that has been modified to ensure that any  bugs  have been fixed and that no other previously working functions have failed as a result of the reparations and that newly added features have not created problems with previous versions of the  software . Also referred to as  verification testing , regression testing is initiated after a  programmer  has attempted to fix a recognized problem or has added  source code  to a program that may have inadvertently introduced errors. It is a  quality  control measure to ensure that the newly modified code still complies with its specified requirements and that unmodified code has not been affected by the maintenance activity. What is Regression Software Testing? Regression means retesting the unchanged parts of the application. Test cases are re-execu...

ThreadPoolExecutor使用

From 洞玄的博客 http://dongxuan.iteye.com/blog/901689 jdk官方文档(javadoc)是学习的最好,最权威的参考。 文章分上中下。上篇中主要介绍ThreadPoolExecutor接受任务相关的两方面入参的意义和区别,池大小参数 corePoolSize和 maximumPoolSize,BlockingQueue选型( SynchronousQueue, LinkedBlockingQueue, ArrayBlockingQueue );中篇中主要聊聊与 keepAliveTime这个参数相关的话题;下片中介绍一下一些比较少用的该类的API,及他的近亲: ScheduledThreadPoolExecutor 。 如果理解错误,请直接指出。 查看JDK帮助文档,可以发现该类比较简单,继承自AbstractExecutorService,而AbstractExecutorService实现了ExecutorService接口。 ThreadPoolExecutor的完整构造方法的签名是: ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit  unit, BlockingQueue < Runnable > workQueue, ThreadFactory  threadFactory, RejectedExecutionHandler  handler)   先记着,后面慢慢解释。 ===============================神奇分割线================================== 其实对于ThreadPoolExecutor的构造函数网上有N多的解释的,大多讲得都很好,不过我想先换个方式, 从Executors这个类入手 。因为他的几个构造工厂构造方法名字取得令人很容易了解有什么特点。但是其实Executors类的底层实现便是ThreadPoolExecutor! ThreadPoolEx...

How to install JSVC on Linux WidenHome Log | WidenHome Log

How to install JSVC on Linux WidenHome Log | WidenHome Log In our team, we have a lot of Java standalone applications which should be run as daemon on Unix/Linux system, and we found JSVC is the best choice for us to wrap Java programs to daemons. This article records the steps on how to install JSVC executable on Linux, which is our stage/prod environment. Download JSVC source package First of all, we need to download JSVC source package from this URL: http://commons.apache.org/daemon/download_daemon.cgi , for example, I downloaded commons-daemon-1.0.5-src.tar.gz file. Or, download it via wget: wget -c http://apache.etoak.com/commons/daemon/source/commons-daemon-1.0.5-src.tar.gz Build JSVC executable Unzip the source package and build JSVC executable. chmod 755 commons-daemon-1.0.5-src.tar.gz tar zxvf commons-daemon-1.0.5-src.tar.gz cd commons-daemon-1.0.5-src/src/native/unix Before building the JSVC executable, please make sure you have set JAVA_HOME variable correctly. And make sur...