Validation in a ddd entity/value object

619 views
Skip to first unread message

Sean Farrow

unread,
Feb 11, 2015, 3:35:32 AM2/11/15
to ddd...@googlegroups.com

Hi all,

In a project I’m working on, I’ve gone down the route that says a ddd entity/value object should never be created in an invalid state. Given this wher should validation for things like arguments not being null be placed? In the constructor or in a factory, or in a validation object?

Given I am also using CQRS, it seems like I’m going to have the same validation in several places.

 

What are people’s thoughts on this?

Kind regards

Sean.

gaz_edge

unread,
Feb 11, 2015, 4:16:48 AM2/11/15
to ddd...@googlegroups.com
Id be a little careful using the word 'validation' as that infers some user input that requires validating before being passed into the system.

Your question just sounds like you want to create objects in a consistent state. To do this, the object needs to be in its consistent state as soon as it is created. Therefore, avoid writing any public setter methods, use the constructor to force a consistent state.

Matt Goodwin

unread,
Feb 11, 2015, 8:08:58 AM2/11/15
to ddd...@googlegroups.com
+1 in constructor with the use of Value Objects ;-)

Sean Farrow

unread,
Feb 11, 2015, 1:02:28 PM2/11/15
to ddd...@googlegroups.com

Hi,

 

That’s where I was going, how do you handle a situation where you might want to unit test this, say you have a FutureDate value object that only allows dates in the future.

If this was a class I would pass in an interface that gave access to the time.

I appreciate it’s not advisable to pass dependencies in to domain objects, would a factory be more appropriate, if so where should this live?

Cheers

Sean.

--
You received this message because you are subscribed to the Google Groups "DDD/CQRS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dddcqrs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Greg Young

unread,
Feb 11, 2015, 5:48:27 PM2/11/15
to ddd...@googlegroups.com
"I appreciate it’s not advisable to pass dependencies in to domain
objects, would a factory be more appropriate, if so where should this
live?"

??
--
Studying for the Turing test

Brian Mavity

unread,
Feb 11, 2015, 5:51:17 PM2/11/15
to ddd...@googlegroups.com
Hi Sean,

I think there is a bit of a misunderstanding here. Generally dependencies are not passed into domain objects through the constructor. But passing them in as arguments to a method is common.

Brian

Sean Farrow

unread,
Feb 11, 2015, 6:19:36 PM2/11/15
to ddd...@googlegroups.com

Hi Brian,

 

Thanks for the clarification.

Thinking about this further, I can get around this using value objects.

Cheers

Sean.

Reply all
Reply to author
Forward
0 new messages