title: Domain Objects to map
body: <p>We are building a micro-service REST api in groovy as part of a more complex application. This service would only just be a simple CRUD operations. </p>
  
  <p>We are using groovy and on a GET we need to pull a record from the database ( mongodb ), and return the object after changing the format of some fields ( for backward compatibility ), something on these lines:</p>
  
  <pre><code>def object = collection.findOne(query)
  object.field3 = object.field2.collect { [..]}
  return object
  </code></pre>
  
  <p>I am not very experienced in groovy or java ( I worked more extensively on ruby/python/clojure ), but I would normally have a domain object:</p>
  
  <pre><code>class Foo {
    String _id
    String field1
    Array field2
  
  
    Map toMap() {  
  
      [field1: field1, field3: field2.collect {}, field2: field2]
    }
  }
  </code></pre>
  
  <p>and then just go</p>
  
  <pre><code>foo = new Foo(objectMapFromDb)
  foo.toMap()
  </code></pre>
  
  <p>and some of the benefits that I see by using this approach is that I can look at the domain object and have an idea of what the structure of the object in the database is, I can get some type safety if I want to but more importantly ( and that's what drove my decision ) is that the code for converting the map is encapsulated in a function and can be easily re used in other part of the project </p>
  
  <p>A more senior developer questioned this approach mentioning YAGNI and stating that domain objects are too complex, and reverted back to just changing changing the map as above. </p>
  
  <p>I am a bit puzzled, I tried to look around for information about avoiding domain objects but I can't find anything similar to that ( I have looked at the anaemic domain model, but this seems to take it to the extreme ). About yagni I don't feel it applies in this case.</p>
  
  <p>I personally don't see anything terribly wrong with that, for a simple application seems like a fine solution, but the novelty of this approach really stimulate my curiosity.</p>
  
  <p>Any thought on this? Am I missing something?
  Thanks</p>