I'm trying to make simple 3-tier architecture application with ASP.NET and C#. I walked to an problem with circular dependency. I have Student Class at Business tier. I have an interface to presentation tier with those methods:
void SaveStudent(Student student);
Student[] GetStudents();
This looks like ok.
But I have also interface from Data Access tier to Business with those methods:
void InsertStudent(Student student);
Student[] ReadAllStudents();
The problem is with Student class. Because my Business tier depends on DAL, I cannot put reference to Business tier from my data access layer. I know that DAL should not depend from Business tier. But don't know how to solve that problem.
How should I pass the data then?
If I place Student class to DAL then my presentation tier would be forced to depends on data access layer which isn't good.
I never tried to make 3 tier architecture before.
How to solve this problem? Be free to change my interface methods if it is needed.
This problem is generally solved by placing your Student class in a separate Models assembly (call it what you like) which will be shared across all layers. In large scale n-tier architectures these classes will generally contain nothing more than data and are usually referred to as Data Transformation Objects (DTO). Any business logic which acts upon these objects will be held in the Business Layer (BL) which is usually exposed as a service (i.e WCF). The service is just an intermediary, you can design in this way even when all code is sitting on the same machine and even in the same process. As a minimum you want to separate your concerns (UI, Business Layer, DTO, DAL)at the assembly level.
In your case the GetStudents method would be exposed on the Business Layer Service and would return the Student DTO. The Business Layer would hold a reference to the Data Access Layer (DAL) upon which it would call InsertStudent. Again as I've said both the DAL and and BL have a reference to the Models assembly but most importantly the DAL doesn't have a dependency on the BL.
Client --> Business Layer Service --> Data Access Layer
<------------------ Student (DTO) ------------------>
The key concept in n-tier architecture is that the n-th tier knows only of the n+1-th tier. So your user-interface will call the business logic and the business logic will call the data access layer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With