0

Classes are structured as below. MVC asp.net page

Controller calls Application Service to get data and post data to database.

Domain Entity class which is used by unit of work to insert, update and read data from db.

My question is where do I call sql function

Unitofwork.sqlquery<decimal>("select fn_testfunction").FirstOrDefault

From application service do I need to have separate domain Service to perform all these sql call? Or Can I call sql function directly from application service and expose the function name there? I have read while Google that we need to use CQRS to call procs and function. I don't understand how to achieve that. Please suggest

Marco
  • 22,856
  • 9
  • 75
  • 124
  • Do you encounter any specific errors or have just not tried it yet? – Marco Mar 15 '19 at 09:38
  • I don't have any error in calling the SQL function. I want to know how to structure the class and while calling this function from controller, where to encapsulate this unit of work SQL function call – Hello World Mar 15 '19 at 10:28
  • It has nothing to do with CQRS or DDD. These calls should be encapsulated under a repository class (check the repository pattern) so you can switch your persistence technology (e.g. your database) without impacting the rest of the application. – Maxime Gélinas Mar 25 '19 at 19:36

1 Answers1

1

If you are following DDD, the preferred structure is :

    UI to=> Controllers(in case of MVC) to=> Services to=>Repository(for CURD operations on Aggregates and Entities).

This is because as Services are for Business Logic, and if you add your SQL Logic as well First, it will break SRP(single responsibility principle) Secondly, you will not be able to Unit Test your Data access layer and Business layer in isolation.
Repository Pattern is an Important consideration of DDD so, it is preferable to handle all SQL Logic and CURD operation in Repositories separately, using repository pattern and Unit of work pattern together.

Googi
  • 426
  • 4
  • 16
Meow
  • 42
  • 8