n-layered architecture - BLL, DAL and interfaces. What is best practice?

耗尽温柔 提交于 2019-12-04 16:33:20

Generally speaking, objects in the BLL will consume the interfaces - not implement them:

For example if I had a Person object in the BLL that implemented IPerson, and a PersonDataObject or whatever in the DLL that also implements IPerson

Taking a "Person" as an example: think about the different data operations associated with a person (Getting all the data for a single person, a collection of shallow data for many persons, CRUD operations, searching, etc) - then design interfaces along logical groupings (see the Interface Segeragtion Principle).

The interfaces might represent operations from a BL centered perspective - or a "service" based one.

Anyway, to answer your specific question...

I define my equivalent of DTO's in a Common assembly, and the Data Interface in a separate one as well - so I have 4 assemblies: BL, Data Access Inteface definition, the Interface Implementation and Common; all assemblies reference the common one.

I'm working in C#.Net, I define the DTOs as structs (but you could use classes); and all the properties of these are readonly - you feed data into them in the constructor - this way the DTO's are effectly 'dumb' envelopes of information.

While following the n-tier architecture, it is very common to share the data objects between BL and DAL. Sometimes, the same data object can be used in the UI layer as well.

Usually I have a Data Model (or data objects or domain model, whatever you name it) assembly that houses all model objects as interfaces. Taking your people example, I will create an IPeople interface in model assembly. DAL will return an instance of IPeople to BL. BL will consume this instance and if required pass this on to the UI layer after applying business logic.

Google for domain driven design and the repository pattern. It soumnds like you are headed in that direction with your architecture and depending that the scenario warrants it I would use this approach on more complicated code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!