1

To end 2014 year I got a simple question I think. I would like to use "DDD" a bit more, and I'm currently trying to experiment various usecases to learn more about DDD.

My current usecase is the following :

  • we have a new database schema that is using a classic pattern in our company : modeling our nomenclature table as "id / code / label". I think it's a pretty classic case when using hibernate for example.

But in the OO world things get "complciated" for something this simple when using a API like JDBC or QueryDSL. I need to fetch an object by its code, retrieve its id or load the full object and then set it as a one to one relation in another object.

I wondering :

  • this kind of nomenclature can be an enum (or a class with String cosnatnts depending on the developer). in DDD terms, it is my ValueObject
  • the id  /code / label in the database is not i18n friendly (it's not a prerequisite) so I don't see its advantages. Except when the table can be updated dynamically and the usecase is "pick something in a combobox loaded from this table and build a relation with another object : but that's all because if you have business rules that must be applied you need to know the new code etc etc).

My questions are :

  • do you often use the id / ocde / label pattern in your database model.
  • how do your model your nomenclature data ? (country is perhaps not the best example :) but no matter what how do you model it ? without thinking much I would say database table for country; but for some status : "valid, waiting validation, rejected" ?
  • do you model your valueObjects using this pattern ?
  • or do you use lots of enum and only store their toString (or ordinal) in the database ?

In the Java OO objects world, I'm currently thinking that it is easier to manipulate enum that objects loaded from the database. I need to build repositories to load them for example. And it will be so simple to use them as enums. I'm searching some recomfort here or perhaps am I missing something so obvious ?

thanks

see you in 2015 !

Update 1 : We can create a "Budget" and the first one is mark as Initial and the next ones are marked as "Corrective" (with a increment). For example, we can have a list of Budgets :"Initial Budget", "Corrective budget #1", "Corrective budget #2".

For this we have this database design : a Budget Table, a Version Budge with a foreign key between the two. the Version budget only contains an ID, a CODE and a LABEL.

Personnaly, I would like to remove this table. I don't see the advantages of this structure. And from the OO perspective, when I'm creating a budget I can query the databse to see if I need to create an Inital or Corrective budget (using a count query) then I can set the right enum to my new budget. But with the current design I need to query the database using the CODE that I want, select the ID and set the ID. So yes, it's really database oriented. Where is the DDD part ? a ValueObject is something that describe, quantify something. In my case seems good to me. A Version describe the current status of my Budget. I can comapre two versions just but checking their code, they don't have lifecycle (I don't want this one in particular).

How to you handle this type of usecases ?

It's only a simple example because I found that if you ask a database admin he would surely said that all seems good : using primary key, modeling relations, enforing constraints, using foreign key and avoid data duplication.

Thanks again Mike and Doctor for their comments.

Archange
  • 397
  • 5
  • 21
  • 1
    If your object has identity (i.e. a code) which you'll retrieve it by then it isn't a value object. – DoctorMick Dec 31 '14 at 14:18
  • I'm 100% sure about that. For example if I have a table with the TAX inside. The really basic structure can be ID / CODE / PERCENTAGE / LABEL. What's the difference, when using this structure, between the 20% TAX and another 20% TAX ? none I think. If we add temporal property perhaps that can be a entity but just with a percentage and a code for fetching it, isn't it a valueObject ? – Archange Dec 31 '14 at 14:41
  • No, value objects don't have identity and can't exist without an entity to associate them with. It sounds to me like we're talking about reference data so this might be useful: http://stackoverflow.com/questions/5478253/loading-a-value-object-in-list-or-dropdownlist-ddd. If the value doesn't change when attached to something (an order or similar) then at that point the tax for the order could be a value object. – DoctorMick Dec 31 '14 at 14:49
  • You want to use D(omain)DD but your questions are all about the database. This is your problem, you haven't yet understood what DDD is about. Your current mindset is totally unrelated to DDD. I can say that, right now, you're using the DDD anti-pattern. – MikeSW Dec 31 '14 at 15:14
  • @MikeSW : I agree with you. I'm not the one who design the database. I'm talking about the database design because I want to change it, because it is the focus of a lot of people and in particular at my current company. My question are how to handle that, explain the concepts to my colleagues etc. – Archange Dec 31 '14 at 15:45
  • @DoctorMick : are you telling me that because I'm using an ID column means that I'm currently facing an entity ? What I'm describing is the current point of view of the project I'm working on (database design oriented). And What I'm asking is : what do you think of removing this kind of table and transforming them into java enums. Using enums, I would associate this enum (modeling a status for example) directly with an entity. I'm updating my question with a current example. Thank you both of you for yours comments. – Archange Dec 31 '14 at 15:49
  • @Archange You don't get it. DDD has nothing to do with the db. Any of it. Persistence ignorance is one of the basic principles of DDD. Once you ask yourself about the db, you're outside DDD. – MikeSW Dec 31 '14 at 16:39

1 Answers1

0

I will hook in in your country example. In most cases, country will be a value object. There is nothing that will reference a country entity and that should know that if the values of the country changes it is still the same country. In fact, the country could be represented as an enum, and some nasty resource lookup functions that translate the Iso3 into a usefull display text. What we do is, we define it as a value object class with iso3, displayname and some other static information. Now out of this value object we define a kind of "power enum" (I still miss a standard term here). The class implementing the country value object gets a private constructor and static properties for each of its values (for each country) and explicit cast operators from and to int. Now you can treat it just like a normal enum of your programing language. The advantage to a normal enum beside having more property fields is, that it also can have methods (of course query methods, that don't change the state of the object). You can even use polymorphism (some countries with different behaviour than others). You could also load the content of the enums from a database table (without the statics then and a static lookupByIso3 method instead).

This you could make with some other "enum like" value objects, too. Imagine Currencies (it could have conversion methods that are implemented polymorphic). The handling of the daily exchange rates is a different topic though.

If the set of values is not fixed (for example another value object candidate like postal adress) then it is not a value object enum, but a standard value object that could be instantiated with the values you want.

To decide if you can live with something as a value object, you can use the following question: Do you want copy semantic, or reference semantic? If you ever change a property of the object, should all places where you used it update, too, or should they stay as they are? If the latter, than the "changed" object is a new and different value object. Another question would be, if you need to track changes to an object realizing that it remains the "same" despite of changing values. And if you have a value object, where you only want specific instances to exist, it is a kind of enum described above.

Does that somehow help you?

Holger Thiemann
  • 1,042
  • 7
  • 17
  • Country potentially has tons of attributes like phone code, currency, time zone, language, EU member or not, VAT number prefix, you name it. So how can you be sure you will never need this? – Alexey Zimarev Mar 10 '15 at 12:49
  • I think in DDD you would not model country with all attributes you can imagine from reality, but just the attribute that the bounded context needs. In another bounded context you would model it with other attributes that are needed there. Also the things you describe are different concepts and not simple properties of country. What I meant with the "country" is the country itself. Not statistics of it like population. But even if you have to change some of its properties, the question is still if this needs to be "the same" after that or if you can go with it as a "new" value. – Holger Thiemann Mar 10 '15 at 16:39