3

I am reading Eric Evan's Domain Driven Design, and I encountered this concept on p108. I am having a hard time grasping the concept, in spite of the explanations mentioned on the pages 107 and 108.

Here is an excerpt of the topic from the book:

Medium-grained, stateless SERVICES can be easier to reuse in large systems because they encapsulate significant functionality behind a simple interface. Also, fine-grained objects can lead to inefficient messaging in a distributed system.

As previously discussed, fine grained domain objects can contribute to knowledge leaks from the domain into the application layer, where the domain object's behavior is coordinated.

Can somebody explain to me what is granularity so I can understand more what is being described in the excerpt above?

2
  • 2
    This is more a question about the English language itself. When we speak of something being granular when we are not talking about grains of sand, sugar, salt, etc, then we are talking about how finely detailed something is, or how specific it is. Lower granularity means fewer details or being less specific. Higher granularity means more details or more specific. The word "animal" is not as granular as "horse," which is not as granular as "thoroughbred" (a breed of horse). Commented Jan 11, 2019 at 14:24
  • I see. I thought it is something adherent to software design that I have never encountered. Eric uses this term very often in that section, so I thought it is a way for categorizing services and domain objects. Thank you for clearing this up. Commented Jan 11, 2019 at 17:10

3 Answers 3

2

Coarser and finer grained means implementing more or less functionality respectively. It is somewhat related to the size too.

So a "fine grained" service is something that does very little. Like a service that just multiples two numbers. A "coarse grained" service is something that does something more complex, like booking a room in a hotel.

A "medium grained" service is usually something near the middle of this scale. Like a service that books only the money from your credit card but doesn't do anything else. It is very much subjective of course, and depends on the scales involved in your project.

What he is saying there, if you use finer grained services/objects than obviously you need to communicate more because you need to speak to more services/objects. This also means that you are exposed to a lot more knowledge, because you have to understand some data and the choreography between calls.

2

Think sugar

  • There is raw sugar, it has a big grain.
  • Refined or white sugar, has a smaller grain.
  • Icing sugar, has a very small grain, it looks like dust.

For a computer

Granularity is talking about how much is in a thing, when you look at it from a given perspective.

  • An application has a coarser grain than an expression.
  • An application has a finer grain in comparison to a cloud.

From this perspective you could call an application medium grained.

0
0

Please check "The Discipline of Organizing", Section 10.3.2.3 Granularity and Abstraction.

Granularity refers to the level of detail or precision for a specific information resource property. For in stance, the postal address of a particular location might be represented as several different data items, including the number, street name, city, state, country and postal code (a high-granularity model). It might also be represented in one single line including all of the information above (a low-granularity model). While it is easy to create the complete address by aggregating the different in formation components from the high-granularity model, it is not as easy to de compose the low-granularity model into more specific information components.

This does not mean, however, that a high-granularity model is always the best choice, especially if the context of use does not require it, as there are corresponding tradeoffs in terms of efficiency and speed in assembling and processing the resource information.

Fundamentals of Software Architecture, Page 110

Finding the proper granularity for components is one of an architect’s most difficult tasks. Too fine-grained a component design leads to too much communication between components to achieve results. Too coarse-grained components encourage high internal coupling, which leads to difficulties in deployability and testability, as well as modularity-related negative side effects.

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