业务逻辑层可以继承n层体系结构中的数据访问层吗? [英] Can Business Logic Layer inherit Data Access Layer in n-tier architecture?

查看:107
本文介绍了业务逻辑层可以继承n层体系结构中的数据访问层吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是继承业务逻辑层(BLL)中的数据访问层(DAL)而不是以任何面向对象的语言在BLL中创建DAL对象的更好方法吗?但是,DAL仅使用受保护的方法和属性,因此它对其他层不可见".

Is that a better way to inherit the Data Access Layer(DAL) in Business Logic Layer(BLL) instead of creating an object of DAL in BLL in any Object oriented language? However the DAL uses only protected methods & properties so that it is not ''visible'' to other layers. Is this breaking n-tier architecture standard?

推荐答案

不,最好不要这样做.继承在应用程序逻辑和数据访问逻辑之间创建了过于强烈的依赖性​​.只需估算一下迁移到另一个数据库时必须重写数据访问类的工作量即可.相反,DAO应该公开一个由应用程序逻辑使用的接口.然后,您要做的就是编写实现DAO接口的新类,而应用程序逻辑不在乎那些对象来自何处以及它们如何实现其接口.

此外,您始终只能从一个基类继承.如果应用程序逻辑中的某些类需要使用两个或多个DAO,该怎么办?例如,您可以使用一个DAO来获取用户的数据,而使用第二个DAO来获取用户角色的列表,以便应用程序逻辑能够检查用户的权限.显然,应用程序逻辑的类并不总是与一个实体或其DAO相对应.
No, it''s better not to do that. Inheritance creates a far too strong dependence between the application logic and the data access logic. Just try to estimate the work involved when you have to rewrite the data access classes when migrating to another database. Instead, the DAOs should expose an interface which is used by the application logic. Then all you have to do is to write new classes that implement the interfaces of the DAOs and the application logic does not care where those objects come from and how they implement their interfaces.

In addition, you can always inherit only from one baseclass. What if some class in the application logic needs to use two or more DAOs? For example you could use one DAO to get a user''s data and a second one to get the list of the user''s roles, so that the application logic is able to check the user''s permissions. Obviously the classes of the application logic don''t always correspond to one single entity or its DAO.


这篇关于业务逻辑层可以继承n层体系结构中的数据访问层吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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