2

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.

  • Use the object/data structure most convenient for the business rules. Even if that means things will need to be converted. – candied_orange Sep 23 '17 at 4:14
1

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.

1

Follow the "Functional Core, Imperative Shell" model. This is the essence of all modern architecture including Uncle Bob's "Clean", the Onion architecture as well as things like the Elm architecture.

According to this model your service layer, the core of your app, will have all the logic of your app but none of the side effects. This will make your service layer very easy to test, no stubs, mocks or test doubles needed.

If your Entity objects can be built independent of the ORM, then it's fine to use them in your service layer, otherwise you will have to create some "Plain Old Data" types to pass into the service layer.

Some resources:

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.