Search code examples
ooparchitecturesolid-principles

Do you define each business logic function of an application in separate classes in order to not break SRP?


If you had a Service class for every business related module such as UserService. You might have AddProductToBasket() and PlaceOrder().

These methods would rely on lower level modules defined in an IOC container. The PlaceOrder() would not make calls directly to the database or payment service but it would glue all the logic together.

Would this not break the single responsibility principle though. Because even though the Service class is not making direct calls it still carries out many different business functions, and if two business functions change in the same class, then the class has multiple reasons to change therefore.

Would it make sense to have a class for each business function?


Solution

  • You're missing a very important point here: Domain Models and Aggregates. In your example, the AddProductToBasket method should reside on a Basket class.

    Multiple business functions might not make sense in that context. Make sure you're separating them properly in the right Bounded Context. For example, if you're using microservices, you might want to have one microservice per Bounded Context.