Hi,
I was wondering if anyone had anymore thoughts about validation.
I've been enhancing the domain model from POJOs in Action to do more sophisticated validation and return multiple errors. Instead of throwing an exception when the first error is detected the code now returns a collection of errors in a Notification.
For example, the business logic for processing the delivery information for an order has to verify two things:
- The delivery time is one hour in the future
- The delivery time and address is served by can at least one restaurant
Several things can be wrong:
· Delivery time is not far enough in the future
· No restaurants serve the delivery address
· No restaurants serve the delivery time
· No restaurants serve the combination of delivery time and address
The domain logic now accumulates these errors in a Notification.
One observation I would make is doing more elaborate validation makes the code more complicated – not surprising since its doing more. E.g. there is additional code to figure out whether it's the delivery time or delivery address are wrong. The Notification also has to be passed around between domain objects, e.g. from the PlaceOrderService, which ultimately returns it to the client, to the PendingOrder to the DeliveryInfo.
Much of the validation logic is currently in the DeliveryInfo class, which has a validate() method:
class DeliveryInfo {
private Address deliveryAddress;
private Date deliveryTime;
void validate(Notification, RestaurantRepository) {
….
}
…
}
The validate() method checks that the delivery time is in the future and calls RestaurantRepository to determine whether the deliveryInfo is served by any restaurants. I suppose that I could, instead, have a separate DeliveryInfoValidator class.
Any thoughts?
Chris
--
Enterprise POJO consulting - http://www.chrisrichardson.net
Author, POJOs in Action - http://www.manning.com/crichardson
Enterprise POJOs blog - http://chris-richardson.blog-city.com
I am working up an application's domain model right now and basically
have the "anemic" domain model built which is just a whole ton of
simple Beans that meet the data requirements of the application. I am
about embark on building in the business logic to the model... the
tough part :)
Originally I figured that when something happened in the system that
wasn't allowed, it could simply throw a custom exception that extends
RuntimeException which I could then catch and handle appropriately in
another layer if need be. Unfortunately I don't think this is very
practical for a web application, say where the user submits a form and
has several errors that you want to notify them of at once.
I've been looking at Spring, its Validator interface and some of the
other associated classes but it looks as though they're geared more
towards working in the Controller layer, very far away from my domain
model. This of course seems contrary to the fundamental spirit of
DDD. I understand that form validation is a concern that's largely
orthogonal to DDD, but I'm curious how others have modeled exceptions
in their business logic and how they tie that in to a web form
validation framework.
Thanks in advance for any input!
-Cliff
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/domaindrivendesign/
<*> To unsubscribe from this group, send an email to:
domaindrivendesign-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/