Object-oriented – Object To Be Used By Service Layer

code-layoutn-tierobject-orientedormrepository

My project has a UI layer, a Service Layer and a Repository layer. The latter has Entity objects as part of the ORM (.net Entity Framework).

The service later returns a Dto to the UI layer.

My question is what object should the service layer work with? Should I return the Entities to my service layer methods for performing business logic calculations.

Best Answer

Take a look into "Clean Architecture" by Bob Martin: https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html

Just to summarize: your application's business rules must be testable without the database, the user interface, or any other external factor. Therefore, if your Entities expose stuff related to database, then perhaps your design could be improved.

My question is what object should the service layer work with? Should I return the Entities to my service layer methods for performing business logic calculations.

If your Entities are part of Repository layer, then they shouldn't be returned by your application (which is represented by your Services layer). Your application must work as one unit, and be testable too. So, define appropriate objects to be declared within Services layer and return them to whatever client might be using your app.

Related Topic