Domain-Driven Design or Domain Oriented Design is an object-oriented software modeling standard that seeks to reinforce OO-related concepts and best practices.
This comes in contrast to the common use of Data-Driven Design or Data-Driven Project, which most developers use without even being aware of it.
Data-Driven Development
I have heard several times that data is the most important thing in a company, so modeling should always start thinking about the database.
It is nothing unusual for developers . Net, Java and C++ start a system establishing the guys that they will use and the relationship between them. These types are usually "dumb" objects, with getters and setters, representing nothing more, nothing less, than a database table.
The problem with this approach is that it does not make good use of the Object Orientation features. Many think that getters and setters are the pinnacle of encapsulation, but in practice these methods allow the user to recover and change all attributes. There’s no gain, except a lot of unnecessary code.
Anyway, many people think they are using OO, but the classes could easily be replaced by records or structures, according to the language used.
Domain-Driven Development
The initial idea of the DDD is to go back to a purer OO modeling, so to speak. We should forget how data is persisted and worry about how to better represent business needs in classes and behaviors (methods).
This means that in ddd one Cliente
may not have a Setter for their common attributes, but may have methods with business logic that in this business domain belong to the customer, such as void associarNovoCartao(Cartao)
or Conta recuperarInformacoesConta()
.
In short, the modeled classes and their methods should represent the business of the company, using even the same nomenclature. The persistence of the data is placed in the background, being only a complementary layer.
When not using DDD
Sometimes all it takes is a CRUD
DDD is not a solution for everything. Most systems have a large part composed of basic registers (CRUD) and it would not be appropriate to use DDD for this.
The DDD should help in the modeling of the most important and most central classes of the system of form and decrease the complexity and help in the maintenance of them, after all this is the objective of the principles of orientation to objects.
Sharing data with other systems
Integration routines that receive or make data available to other systems should not be "intelligent".
Many developers end up modeling their business classes trying to solve the internal issues of the system and at the same time thinking about how these classes will be exposed to other systems.
Standards as DTO (Data Transfer Object) who use "dumb" objects are more suitable for this.
Final considerations
DDD does not attempt to solve all problems of all layers of a system.
Its focus is on modeling the core business entities using the appropriate language of that domain to facilitate maintenance, extension and understanding.
Particularly, I would not follow the pattern to the letter, because there are numerous patterns and variations of OO modeling. Study the principles behind these patterns, as they are usually similar and see what works best for each project.
References
- DDD - Introduction to Domain Driven Design
- Coding for Domain-Driven Design: Tips for Data-Focused Devs
- Domain Driven Design Quickly (free e-book)
Note: I wrote terms like Domain-driven hyphenated, because when two or more words form a compound adjective in English they should usually be "linked". In this case, Domain-driven is an adjective of design. (Reference)
"Many think that getters and setters are the pinnacle of encapsulation, but in practice..." I’ve always hated getters and setters, but I didn’t really know why. With this explanation, it all makes sense now! I imagine that this must be one of the most poorly used resources of the OO...
– mgibsonbr
@mgibsonbr Sometimes the programmer is afraid to simply expose a data structure, so he writes
getters
andsetters
. Some say that an object does not even expose data, but only behaviors (which would be precisely thegetters
andsetters
): http://blog.8thlight.com/uncle-bob/2013/10/01/Dance-You-Imps.html. This one might not like languages like C#, wheregetters
andsetters
are transparent to the consumer of the object and present as simple exposed data (except for a differentiated icon in the IDE).– Caffé
"can have business logic methods that in this business domain belong to the customer", what does mastery mean in this context? Can you give me a synonym for that?
– HeyJoe
@Heyjoe Domain is what we usually call the concepts adopted by a particular business area. When we create a data model for a system, we usually base ourselves on the domain that that system belongs to. For example, in a bank system, the customer is associated with an account and a credit card, but in a virtual store the card is directly associated with the customer. Different areas with different rules.
– utluiz