The POCO class has behaviors in domain driven design such as Validate()
method, Is it true?

- 1,657
- 3
- 19
- 37

- 31
- 6
-
2Domain classes (POCOs) should certainly contain related behavior. Whether validation methods such as `Validate` belong in entity classes (POCOs) is arguable. Many times it is preferable to have entities be always valid. – eulerfx Nov 15 '12 at 22:50
-
1Does this approach cause POCO have dependency? – Raymound Nov 15 '12 at 23:10
-
2Dependency on what? This approach forces you to ensure that you have valid data before attempting to create an entity (POCO). Validation could be performed in the UI, for example. – eulerfx Nov 15 '12 at 23:40
-
You mean entity not POCO, right ? POCO is orthogonal to domain entity. You can have non-entity POCO's and non-POCO entities. – guillaume31 Nov 16 '12 at 13:45
-
With regards to DDD, non-POCO entities might be a little questionable, but I used POCO and entity interchangeably. More specifically, I meant that entities are represented as POCOs. – eulerfx Nov 16 '12 at 16:08
-
Was any of the answers helpful to you? If yes, please accept the answer to appreciate the help. – Dennis Traub Nov 22 '12 at 17:38
3 Answers
Yes - the "Entity" encapsulates the data and the behaviour of the object - so it isn't a plain old contract object any longer, it is a domain object.
One way to think of it is to imagine that none of your other code could see the properties of the object, so they can't do...
if (myDomainObject.Name != null) ...
They have to call
if (myDomainObject.IsValid()) ...
When you change the rules about what makes it valid, the change only needs to be done in the domain object as you have stopped the logic from leaking outside into the code that uses it.

- 241,084
- 71
- 387
- 401
Yes, the classes of the Domain Model in Domain-Driven Design should focus on behavior, if that is what you mean.

- 50,557
- 7
- 93
- 108
-
-
Classes inside the Domain Model should not have any dependencies on other modules of the application. – Dennis Traub Nov 16 '12 at 07:47
No. They do not have methods like Validate()
.
A DDD entity should always be in a valid state. That's why we use behaviors (methods) on the classes and not public property setters.
Does this approach cause POCO have dependency?
No. Typically everything depends on the DDD model and not vice versa.

- 99,844
- 45
- 235
- 372