简体   繁体   English

如何设计Web应用程序的层体系结构?

[英]How to design layer architecture for web application?

I have design issue when implement 1 simple web application. 实现1个简单的Web应用程序时出现设计问题。 I use struts2 web controller, spring's IOC and Hibernate as persist layer. 我使用struts2 Web控制器,spring的IOC和Hibernate作为持久层。

Because this web application is very simple at begging. 因为此Web应用程序在乞讨时非常简单。 So I only have 2 layers: 1 DAO layer which used to access database. 所以我只有2层:1个用于访问数据库的DAO层。 Almost every table have related DAO. 几乎每个表都有相关的DAO。 2 Action layer. 2动作层。 User struts2. 用户struts2。 I am satisfy with this architecture because can quickly implement my web application. 我对这种体系结构感到满意,因为它可以快速实现我的Web应用程序。 As project become bigger, I found the action layer become big and complex, and very hard to re-use. 随着项目的扩大,我发现操作层变得又大又复杂,并且很难重用。 I try to create service layer, to solve complex business logic is good, but my application still have a lot of simply logic. 我尝试创建服务层,以解决复杂的业务逻辑很好,但是我的应用程序仍然有很多简单的逻辑。 Eg: Load 1 object, save 1 object, and get collection by some condition and display it to webpage. 例如:加载1个对象,保存1个对象,并在一定条件下进行收集并将其显示在网页上。 If give each simple DB access method have corresponding service method. 如果给每个简单的DB访问方法都有相应的服务方法。 Still cost a lot of effort. 仍然要付出很多努力。 How can solve this issue? 如何解决这个问题? And I think, if service layer existing, direct call DAO layer still not good design for my application. 而且我认为,如果存在服务层,则直接调用DAO层仍然不是针对我的应用程序的良好设计。 Is any good solution for this kind of small web application? 这种小型Web应用程序有什么好的解决方案吗?

When planing the different layers in a web application it is good practice to explicitly protect attributes and associations in your model from being manipulated without providing an identity context. 在计划Web应用程序中的不同层时,优良作法是在不提供身份上下文的情况下显式保护模型中的属性和关联免遭操纵。

This is something that should neither be done in the DAO layer nor in the Controller. 这既不应在DAO层中也不应该在控制器中进行。 You should wrap your DAO layer in a service layer and have the controller only talk to the services not the DAO directly. 您应该将DAO层包装在服务层中,并让控制器仅与服务对话,而不与DAO直接对话。

Protecting your model against unwanted manipulation means that you for instance adapt the amount of information passed in a data structure between Controller and Service to the actual operation that you want to perform. 保护模型免受不必要的操纵意味着,例如,您可以使在Controller和Service之间的数据结构中传递的信息量适应要执行的实际操作。

For instance: adding or removing an element from a collection is an explicit operation in the service, it does not happen implicitly by manipulating a collection as a member of a DAO object and passing that DAO back into the service. 例如:从集合中添加或删除元素是服务中的显式操作,通过将集合作为DAO对象的成员并将该DAO传递回服务中,不会隐式发生。

Instead your service may look like this: 相反,您的服务可能如下所示:

+ getAllCompanies(): CompanyType[*]
+ getAllEmployeesOfCompany(c: CompanyType) : EmployeeType[*]
+ addEmployeeToCompany(e: EmployeeType, c: CompanyType)
+ removeEmployeeFromCompany(e: EmployeeType, c: CompanyType)

The additional benefit of such an architecture is that the service layer serves as boundary for your transactions. 这种体系结构的另一个好处是,服务层可以充当事务的边界。 Using the methods of your controller as boundary for your transactions is in fact a very bad habbit. 实际上,使用控制器的方法作为事务的边界是一个非常糟糕的习惯。 You could even call it an anti-pattern. 您甚至可以称其为反模式。 Why? 为什么? Because for instance it would mean that when your client hangs up it would roll back your transaction. 因为例如,这意味着当您的客户挂断电话时,它将回滚您的交易。 That is clearly unwanted in 99% of the cases. 在99%的情况下,这显然是不希望的。

As @mwhs commented, Apache Isis provides plenty of guidance on layering your app. 正如@mwhs所评论的那样, Apache Isis提供了有关分层应用程序的大量指导。 To figure out whether it fits your requirements, you could run through this tutorial I presented at a recent conference. 为了弄清楚它是否符合您的要求,您可以遍历我在最近一次会议上介绍的本教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM