I would like to know what differentiates a Service class from a utility class or a helper class? A class only with underlying methods calls the dao's is a service? Doesn't the usage of Helper classes violates SRP?
|
|
The lines can be a little blurry, but I see it this way:
Services are not simply tightly coupled to DAOs, it's a broader term/usage pattern than persistence Helper classes do not violate SRP if coded in accordance with that principle. That is, each method should do one thing and one thing well, the class should perform one type of utility help, (e.g. Date conversion) and do that well. |
|||
|
|
|
Not a scientific definition, but my general take is a service class has some context within the application whereas helpers are more generic and don't care what app they are helping. |
|||
|
|
|
You mixed up two non related principals. Services and Helper-Classes are not connected. Especially the term "Service Class" is misleading - I think you are referring to a "Service" which is on a higher level of abstraction than classes. A service is characterized through
This definition slightly changes depending on your context. However, the critical point is that the term "service" is on an abstract level, the level of architecture and domain knowledge. The "Helper Class" is a design pattern (even though it's a anti-pattern as they tend to evolve to blob's or god classes) referring to a class that encapsulate generic operations (notice that this is on a lower level of abstraction and is connected to the application/solution knowledge). I'm aware of the fact that there exists nearly no software not containing any kind of helper class, but still, it's bad practice. |
|||
|
|
|
For me, I go by the Eric Evans definition of Generally, in well designed system, most classes have quite clear responsibility or function in that they deal with a specific entity or set of entities in the model. i.e.
When you have functionality that does not belong with any particular entity it can be difficult to find a correct place for it to sit. I.e something that encapsulates a process that involves both an So, that's where a I think of a |
||||
|
|
|
Service Class : Contain Business logic. |
|||
|
|
|
One thing to be wary of is the multiple definitions of 'service' in DDD: Application service: These sit in the application layer and communicate with the domain and data layer, they are the interface through which external systems/UI interact with the DDD system. Domain Service: This can be used by the domain or the application layer, and contain business logic that does not fit neatly into one particular entity. Infrastructure Service: These are used by the domain to communicate with external resources. Helper classes tend to contain pieces of code or algorithms that would be reused by multiple entities, so can't really go into entities without violating the DRY principle. They are probably closest to Domain Services, in that they sort fulfil the same purpose (externalising business logic from entities) but they do it for different reasons. |
|||
|
|
