-->

Types the DAL and BLL should pass

2019-02-27 03:56发布

问题:

Just a quick question. In a tiered architecture environment with a Business Logic Layer (BLL) and a Data Access Layer (DAL), what types should the DAL pass to the BLL?

Currently our DAL classes produce DataTables and SQlDataReader objects (among others common data types). Should the conversion of the objects to our custom types be done in the DAL before passing to the BLL or should the DAL pass the original type and the BLL does the conversion?

I'm trying to think of a scenario where the DAL layer may be replaced for another technology in the future. In that scenario it sounds like the BLL should be expecting converted data type so that when a switch is made we only need to make sure our objects are being returned?

Any clarity on this or reading materials to clear things up would be helpful!

TIA

回答1:

Although there are guidelines, there is no any hard rule for this. Also, the guidelines may change per project based on architecture, size, requirements, priorities, ORM used etc. Refer this.

As I said, there is nothing definitive, following is what I do in my big project.

I am using the NHibernate ORM. My DAL returns Entity. My BLL receives entity and converts it to DTO. DTO is then passed to application layer. This is also not true for all the cases. Refer this post. I have mentioned details why I do so in that answer.

For my small projects, I use Dapper ORM. In this case, my DAL returns Entity. BLL receives Entity and pass it as is to application. As Dapper does not create proxies, I do not convert Entities to DTOs.

It looks you are not using any ORM. Second way (transferring Entity to BLL and same to application) may be better approach for you. But you should also take in consideration other aspects I mentioned above.

With any case mentioned above, DAL should return transformed object (like Entity/POCO) instead of ADO.NET object. This way, any technology switch in future will need changes only in DAL as long as your interfaces are not broken.

Transferring ADO.NET objects (DataTable, DataReader) binds your DAL and entire project to one technology. It will be hard to switch the technology. On other hand, if your DAL returns Entity (plain c# class), you can switch technology in DAL and your application remains unaffected. What to do in BLL is up to you based on other considerations.

Should the conversion of the objects to our custom types be done in the DAL before passing to the BLL or should the DAL pass the original type and the BLL does the conversion?

Conversion of objects should happen at DAL. Converted object should be passed to BLL.