In Onion Architecture, we have repository layer which conducts Crud Operations (simple insert, delete, update). (Applying Entity Framework with database)

Onion Architecture

Additionally , we have Service Layer which conducts complex business logic.


(1) Should DBContext leak into Service layer? What if we wanted to conduct a complex business logic query, which is not possible from simple repository, and then EF Savechanges in database?

(2) Should IQueryable be in Service layer? Again, what if there is a need for a complex query that is best by creating and then running expression tree in the service layer after its built?

  • 1
    Regardless of that EF Core's DbContext support different databases or even NoSql, it still will leak database related dependencies/knowledge if you will use it in service layer or if you use IQueryable as abstraction type. Data access abstraction can have domain specific methods and types, then implementation of data access implementation will be responsible to convert database types into domain types. If you need complex query, you simply will add a another method. But try to keep as much logic in the service layer. – Fabio Sep 18 '19 at 4:29
  • 1
    hi @Fabio feel free to write in answer, and I can send points, thanks- – GregDavis Sep 18 '19 at 4:32
  • The answer to most questions containing the word "should" is "which way works best for you?" – Robert Harvey Sep 18 '19 at 7:16
  • hi @RobertHarvey thought different architecture allow different things, however was curious how Onion architecture specifically talks about this issue, thanks – GregDavis Sep 18 '19 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.