Re: Should the "command" method do the validation?

"Thomas Presthus [email protected] [domaindrivendesign]" <[email protected]> Tue, 03 Nov 2015 08:48:32 +0100
Newsgroups gmane.comp.programming.domain-driven-design
Organization Erus Encodia AS
Message-ID <[email protected]>
Hi,


You are actually facing two different aspects here: Valiation and 
business rule enforcement.


Validation could be handled by a read-model, and the rule enforced by 
your domain.


This leads to your variation number 3, where you'd (before issuing a 
command) check with the read-model to see if it's at all possible to 
book the reservation. If it is possible at that moment, you issue the 
command. If the booking should fail, e.g. due to a lack of sufficient 
free seats, you can handle this afterwards by e.g. sending the customer 
an email about it.


Thomas.


On 02.11.2015 23:42, [email protected] [domaindrivendesign] wrote:
> Should validation in a domain Entity be implemented as separate 
> method
> or as part of the Entity state changing method?
>
> For example if I have event booking application where each event has
> limited number of places for participants. I want to check if user 
> can
> make the booking or not.
>
> Variation 1:
>
> class Event {
>
>  Result add(Booking booking) {
>
>  if (fits(booking) {
>
>  this.bookings.add(booking)
>
>  return Result.success()
>
>  }
>
>  return Result.fail(NOT_ENOUGHT_PLACES)
>
>  }
>
> }
>
> Variation 2:
>
> class Event {
>
>  boolean fits(Booking booking) {
>
>  ...
>
>  }
>
>  void add(Booking booking) {
>
>  bookings.add(booking)
>
>  }
>
> }
>
> Variation 3:
>
> class Event {
>
>  boolean fits(Booking booking) {
>
>  ...
>
>  }
>
>  void add(Booking booking) {
>
>  if (!fits(booking) {
>
>  throw ConstraintViloationException("Cannot fit booking")
>
>  }
>
>  bookings.add(booking)
>
>  }
>
> }
>
> Variation 1 seems nice but violates the Command and Query separation
> principle. Variation 2 is fragile because what if client forgets to
> call the fits() method. Variation 3 seems like duplicating the same
> check.


------------------------------------
Posted by: Thomas Presthus <[email protected]>
------------------------------------


------------------------------------

Yahoo Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/domaindrivendesign/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/domaindrivendesign/join
    (Yahoo! ID required)

<*> To change settings via email:
    [email protected] 
    [email protected]

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo Groups is subject to:
    https://info.yahoo.com/legal/us/yahoo/utos/terms/