Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working on an N-Layered design application using WinForms. My application has 4 layers identified as follows:

Presentation Layer
Application Layer
Domain Layer
Infrastructure Layer (Persistence with repositories)

What are some recommended approaches to performing validations in the Domain Layer for Domain Entity construction when data from a repository is needed?

For example, consider the following validation rule required to pass before an domain entity is created:

Rule 1: An employee requesting for a cell phone must have worked for ABC Company for 6 months or more

The information available to the Entity in the Domain Layer which originated from a UI and later passed in from an Order Service in the Application Layer does not have enough information to enforce the above sample rule. A query is needed from a repository to return the hire date of the employee to calculate if they have worked for 6 months or more.

The Question

The question is what layer or service should contact a repository at this point to get the employee hire date needed to validate Rule 1? The Domain Entity is not considered valid unless Rule 1 is passes and the entity's other data values pass as well.


I've thought of adding a Domain Server to make a repository call then let the Domain Layer use a Factory call to instantiate the Domain Entity with the missing information so it can be validated at the same time. However, I've seen a number negative articles saying the Domain Layer should not know about the repository and it should avoid Domain Services.

The other thought I had was maybe to have the Order Service in the Application Service get the data from the repository and pass it in to the Domain Layer where the Domain Layer can use it for validation. This option seems the easiest way but I'm still not sure.

Thanks in advance

Thanks in advance.

What I have tried:

I can get the information I need to complete the validation either from the Application Service in the Application or Domain Layer. It would work for either one. The problem is in trying to use a Domain Driven Design solution so that I don't end up with some problem later as a result of the wrong choice. My guess at this point is the wrong choice may impact system testing but I'm not sure. At this point I'd like some comments from others who have come across this situation and know what they did that not only worked but did not cause problems later as a result of the choice.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900