{"id":642,"date":"2017-11-02T14:43:12","date_gmt":"2017-11-02T13:43:12","guid":{"rendered":"http:\/\/blog.ansuz.nl\/?p=642"},"modified":"2017-11-02T14:43:12","modified_gmt":"2017-11-02T13:43:12","slug":"separating-domain-model-and-application-model","status":"publish","type":"post","link":"http:\/\/blog.ansuz.nl\/index.php\/2017\/11\/02\/separating-domain-model-and-application-model\/","title":{"rendered":"Separating Domain Model and Application Model"},"content":{"rendered":"<p>Let&#8217;s take a simple retail app as an example. The app has the following (partial) domain model:<\/p>\n<p><a href=\"http:\/\/blog.ansuz.nl\/wp-content\/domain.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/blog.ansuz.nl\/wp-content\/domain-300x91.png\" alt=\"\" width=\"300\" height=\"91\" class=\"aligncenter size-medium wp-image-643\" srcset=\"http:\/\/blog.ansuz.nl\/wp-content\/domain-300x91.png 300w, http:\/\/blog.ansuz.nl\/wp-content\/domain-500x152.png 500w, http:\/\/blog.ansuz.nl\/wp-content\/domain.png 743w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>It can be quite tempting to use a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Domain_model\" target=\"_blank\">domain model<\/a> as an application model (as in the &#8220;M&#8221; in <a href=\"https:\/\/en.wikipedia.org\/wiki\/Model%E2%80%93view%E2%80%93controller\" target=\"_blank\">MVC<\/a>). Your application obviously needs to use the data presented in the domain model. At first, it may seem logical to access this data directly, e.g. for showing a product name on a product details page. There are several problems with this approach though.<\/p>\n<p>The domain model may be too verbose for your application&#8217;s needs. Let&#8217;s say you don&#8217;t care about the image details, but only want the URL. The app may also want to check to see if a product has an image associated with it before trying to display it. To solve this, the <code>Product<\/code> and <code>Image<\/code> objects above can be combined into one, like below.<\/p>\n<p><a href=\"http:\/\/blog.ansuz.nl\/wp-content\/app_model.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/blog.ansuz.nl\/wp-content\/app_model-300x196.png\" alt=\"\" width=\"300\" height=\"196\" class=\"aligncenter size-medium wp-image-646\" srcset=\"http:\/\/blog.ansuz.nl\/wp-content\/app_model-300x196.png 300w, http:\/\/blog.ansuz.nl\/wp-content\/app_model.png 337w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Another benefit is that you keep things <a href=\"https:\/\/en.wikipedia.org\/wiki\/Don%27t_repeat_yourself\" target=\"_blank\">DRY<\/a>. The validation logic is not repeated in various classes (think different Presenters in the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Model%E2%80%93view%E2%80%93presenter\" rel=\"noopener\" target=\"_blank\">MVP<\/a> pattern).<\/p>\n<p>To facilitate the conversion from domain model to app model, you could introduce a converter class. Something like the below.<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">java<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"java\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">public<\/span> <span style=\"color: #000000;font-weight: bold\">class<\/span> ModelConverter <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000000;font-weight: bold\">public<\/span> AppProduct convertProduct<span style=\"color: #009900\">&#040;<\/span>DomainProduct product<span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ Imagine a nice implementation here...<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #000000;font-weight: bold\">return<\/span> appProduct<span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>During conversion you can also do some more data validation to make sure your app doesn&#8217;t break when back-end data is incomplete. In this example, for instance, you shouldn&#8217;t rely on the <code>image<\/code> always being populated in the <code>Product<\/code> object.<\/p>\n<p>All in all, separating your domain model from your application model will help you build a more stable app, trim data floating around and reduce code duplication (by centralising data validation).<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s take a simple retail app as an example. The app has the following (partial) domain model: It can be quite tempting to use a domain model as an application model (as in the &#8220;M&#8221; in MVC). Your application obviously &hellip; <a href=\"http:\/\/blog.ansuz.nl\/index.php\/2017\/11\/02\/separating-domain-model-and-application-model\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112,52],"tags":[],"class_list":["post-642","post","type-post","status-publish","format-standard","hentry","category-android","category-java"],"_links":{"self":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/642","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/comments?post=642"}],"version-history":[{"count":15,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/642\/revisions"}],"predecessor-version":[{"id":659,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/642\/revisions\/659"}],"wp:attachment":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/media?parent=642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/categories?post=642"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/tags?post=642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}