简体   繁体   English

我正在使用MVC和三层架构设计Web应用程序。 我有三层架构的设计问题

[英]I am designing a web application using MVC and Three tier architecture. I have got design issue with three tier architecture

In DAO layer currently I am making methods for each type of database query like addDevice, deleteDevice, searchDevice, ifAlreadyRegistered ..... etc and It makes my DAO layer object very heavy . 在DAO层中,我正在为每种类型的数据库查询制作方法,如addDevice,deleteDevice,searchDevice,ifAlreadyRegistered .....等,这使得我的DAO层对象非常繁重。

Is there no way to have generic insert method , delete method , update method in DAO so that DAO layer will not be so heavy? 难道没有办法在DAO中使用通用插入方法,删除方法,更新方法,以便DAO层不会那么重?

Are you using an ORM like Hibernate to interface with the database? 你是否正在使用像Hibernate这样的ORM来与数据库连接? If not, you might want to give ORM a try. 如果没有,您可能想尝试ORM。 It can make working with a database much easier since you express your logic in the "native" programming language (like Java in your case) instead of SQL, and the ORM figures out the SQL for you. 它可以使您更容易使用数据库,因为您使用“本机”编程语言(例如您的Java)而不是SQL来表达逻辑,并且ORM为您计算出SQL。 In Hibernate, for example, object updates look like this (from this article ): 例如,在Hibernate中,对象更新看起来像这样(来自本文 ):

transaction = session.beginTransaction();
Course course = (Course) session.get(Course.class, courseId);
course.setCourseName(courseName);
transaction.commit();

...instead of raw SQL. ...而不是原始SQL。 In cases where using ORM makes sense (which is not all cases), it can make your code a lot cleaner and more straightforward. 如果使用ORM是有意义的(并非所有情况都是如此),它可以使您的代码更清晰,更直接。

In broad strokes, a good example of a program where ORM does make sense is something like a CRM where you're just pushing objects around and doing simple reports; 从广义上讲,ORM 确实有意义的程序的一个很好的例子就像是CRM,你只需要推动对象并做简单的报告; a good example of a program where ORM probably does not make sense is something like an analysis tool where you'll use heavily customized and carefully optimized SQL queries, in which case you'd probably spend more time working around the ORM than actually using it. ORM可能没有意义的程序的一个很好的例子就像分析工具,你将使用大量定制和仔细优化的SQL查询,在这种情况下,你可能花费更多的时间在ORM上工作而不是实际使用它。

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

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