Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I’ve decided to use DDD in one of my pet projects to find out what the fuss is about! Let me start off by saying that this (DDD) is the way software SHOULD be written, I’ve seen some strange patterns and conventions but DDD is really the most natural way of writing code.

Interesting points that I noted started with DDD:

  • I can now dream about code without being reminded of constraints set upon us by databases Persisting data now takes the backseat when developing a complex system (This might come back to bite me)
  • I feel a lot closer to the business and business problem at hand
  • Discussions between you and the client is now done on their neutral ground
  • Discussions between developers are in line with the code
  • I learned to love refactoring (You better make peace with that right now)

Question 1: Is an entity allowed to make repository calls? For instance, if you need to validate that a certain field is unique in the system, should you write a domain service or can the entity make a call to the repo?

Question 2: What is the best practice for validating a new entity? Should you have a Validate() function, or can the validation be done in the constructor?

Your thoughts on this?

share|improve this question

migrated from stackoverflow.com Apr 23 '11 at 13:51

This question came from our site for professional and enthusiast programmers.

2 Answers 2

up vote 4 down vote accepted

Yes, you can call repositories from within entities. But should you do it? Probably not, it often gives you more technical problems that it will benefit you, it also has the risk of making performance tuning very difficult (mostly because it invites bad design).

The validation example also does not seem to justify calling repositories from your entities. Also your example seems to imply that your object can exist in an invalid state, this should be avoided, your domain object should always be in a valid state.

Validation should already start at domain object creation time. If the creation logic is simple, you can keep this kind of logic in the objects constructor. But still the creation logic is kind of separate from the rest of the logic as it’s only needed during creation time and not during the rest of the lifecycle of an object. Thus when creation logic gets complex enough, like calling repositories, you should spinoff this separate concern to another object, a factory object.

You clarified that you use frontend technology that creates objects for you. In that case, I would say that those object are just application value objects that can be used for input to your object factories (or constructors for that matter). Application validation of the fields though, should probably stay separate from your domain though.

In general you should try to avoid giving domain objects setters for all properties. Instead you should provide domain actions on the object. Thus instead of having a method like: setOrderStatus(Order.PROCESSED) you should have a method processOrder(). This way each method will always result in a valid state (or else throw an exception). Note that I write in general, as for trivial fields that have no connection to domain logic and are just representing data, you can still use setters.

For simple data centric applications this kind of approach might give too much overhead though. But then again, that’s not where DDD is meant to be used.

share|improve this answer
    
Thanks Kdeveloper, Please have a look at the comments I gave duffymo on the same question. Anything you would like to add? –  silverappy Apr 25 '11 at 11:08
    
Not much realy. Q1: If you need to check data from repositories before creating an object, use a factory. Q2, do you have webforms that need need validation, use your front end technology and then call an your constructor/factory/service. –  Kdeveloper Apr 25 '11 at 18:59
    
That makes sense, thanks –  silverappy Apr 26 '11 at 6:08

Here's my take on your questions:

  1. "Allowed"? You'll have problems in life if you believe too much in dogma. You and your code can do anything that makes sense. In this case I'd say it doesn't make sense. If I'm dealing with an entity that has been persisted and comes from a repository, it should be 100% valid. If it's not, I'm allowing invalid data in my database. If it's newly constructed, I'd say that programming by contract should prohibit me from creating any invalid instances. So there's never a need to ask a database if an entity is valid. It's too late.
  2. Programming by contract would say you'd have pre-condition rules in the constructor to ensure that you can never create an invalid instance. Every instance you create should be fully initialized, valid, and 100% ready for use by clients. I do have validator classes, but those are usually for web apps that validate and bind input variables from web forms.

DDD and rules of thumb are great, but don't get too caught up in them. Know the rules; know when to break the rules.

share|improve this answer
1  
Thanks duffymo, For question 1, how would you know that a number is unique without calling back to the DB? Question 2: In using silverlight as a front end, so all new entities will be created there, how would you handle Programming by contract there? (I don't necessarily control the creation of new objects myself) –  silverappy Apr 23 '11 at 12:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.