Re: Thoughts and questions about data mapping and DDD in general

"Rupesh Kumar Tiwari [email protected] [domaindrivendesign]" <[email protected]> Thu, 27 Apr 2017 17:30:20 -0400
Newsgroups gmane.comp.programming.domain-driven-design
Message-ID <CACWF3vVbvErnz-07FMwom9CsP9hwPsGhrz3QpHfa-Yd99NKmLQ@mail.gmail.com>
--94eb2c19105e262ad0054e2cabac
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="UTF-8"

Hi Pedro,
I loved the way you explained every steps.
I also practice DDD an=
d in my project we did all of the above steps exactly
you said.


However we=
 do 2 things different.


*1) How we save domain objects*
In order to save d=
omain models we do not convert it in some other dto.
We just save the Domai=
n Model as it is. We use Entity Framework and it
supports Code First that i=
s what we use. Our Repository implementation in
the Infrastructure directly=
 saves Entity ( Aggregate Root ) in Database.
Therefore, we do not need map=
per here.


*2) How we map domain object to UI dtos*
In our application laye=
r itself we have class called as Assembler and that
class basically convert=
s Model To DTO and DTO to Model.
For example if I have Order Aggregate Root=
 Then I have Order Folder in
Application Service Layer. Order Folder has Or=
derAppService.cs and
OrderAssembler.cs.
OrderAppService takes dependency of=
 OrderAssembler and calls MapToDto
method by giving domain entity. Basicall=
y we have our mapper in Application
layer itself.


Hope this helps!














=












Thanks,
Rupesh kumar Tiwari




On Thu, Apr 27, 2017 at 12:44 PM, pedro=
[email protected]
[domaindrivendesign] <[email protected]=
> wrote:


>
>
> Hey guys,
>
>
> How do you manage data mapping in DDD?
>
> =
To me, to follow DDD principles, we'd need to do the following:
>
>
>    - =
clients have their own DTO, to represent the data in their level
>    - cli=
ents do NOT work with domain objects such entities, aggregates
>    ... dom=
ain model, in general;
>    - clients call, via controllers/application ser=
vices, the domain
>    services from the domain
>    - domain services talk=
 to repositories, and both work with the domain
>    model
>    - repositor=
y definition belongs to the domain layer, but
>    implementation belongs t=
o infrastructure;
>    - repository implementation need mappers to convert =
from domain model
>    to DTO and vice-versa;
>
> Some consequences of the =
DDD are that, in the domain model, I'd never put
> JPA annotations (and any=
 other infrastructure-related stuff). DTOs, in the
> infrastructure layer, =
would be the classes that are actually mapped with
> such a thing.
>
> But =
then we need data mapping between client and domain and also domain
> and i=
nfrastructure.
> Where do you usually put it? Would it be kind of separate =
modules such as:
>
>    - a module (separate library) that maps client DTO'=
s to domain
>    objects, and the model depends on it;
>    - a module (sep=
arate library) that maps domain objects to
>    infrastructure (example: DB=
) model and vice-versa, and the infrastructure
>    depends on it (reposito=
ry implementation receives domain objects as
>    parameters, convert them =
or build whatever query it needs, persist/fetch
>    data and, eventually, =
converts data back to domain objects to return to the
>    caller);
>
> Tha=
t's the only way I can think of, preserving the DDD principles.
> Doing so,=
 we kinda preserve domain talking about domain, infra talking
> about data,=
 no 'infra-related' configuration on domain objects, and also no
> clients =
speaking in the domain terms.
>
> What do you think? How do you play with d=
ata mapping?
> By the way, do you agree with my words?
>
> Cheers,
> Pedro
=
>
> 
>

--94eb2c19105e262ad0054e2cabac
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable





<head>

<style type=3D"text/css">
<!--

/* start of attachment style */
       .ygrp-photo-title{
         clear: both;
         font-size: smaller;
         height: 15px;
         overflow: hidden;
         text-align: center;
         width: 75px;
       }
       div.ygrp-photo{
         background-position: center;
         background-repeat: no-repeat;
         background-color: white;
         border: 1px solid black;
         height: 62px;
         width: 62px;
       }

       div.photo-title=20
         a,
         div.photo-title a:active,
         div.photo-title a:hover,
         div.photo-title a:visited {
           text-decoration: none;=20
       }

       div.attach-table div.attach-row {
         clear: both;
       }

       div.attach-table div.attach-row div {
         float: left;
         /* margin: 2px;*/
       }

       p {
         clear: both;
         padding: 15px 0 3px 0;
	 overflow: hidden;
       }

       div.ygrp-file {
         width: 30px;
         valign: middle;
       }
       div.attach-table div.attach-row div div a {
         text-decoration: none;
       }

       div.attach-table div.attach-row div div span {
         font-weight: normal;
       }

       div.ygrp-file-title {
         font-weight: bold;
       }
 /* end of attachment style */
        -->
        </style>
        </head>
<html>
<head>
<style type=3D"text/css">
<!--
#ygrp-mkp {
  border: 1px solid #d8d8d8;
  font-family: Arial;
  margin: 10px 0;
  padding: 0 10px;
}

#ygrp-mkp hr {
  border: 1px solid #d8d8d8;
}

#ygrp-mkp #hd {
  color: #628c2a;
  font-size: 85%;
  font-weight: 700;
  line-height: 122%;
  margin: 10px 0;
}

#ygrp-mkp #ads {
  margin-bottom: 10px;
}

#ygrp-mkp .ad {
  padding: 0 0;
}

#ygrp-mkp .ad p {
  margin: 0;
}

#ygrp-mkp .ad a {
  color: #0000ff;
  text-decoration: none;
}
-->
</style>
</head>
<body>



<!-- |**|begin egp html banner|**| -->

<br><br>

<!-- |**|end egp html banner|**| -->


<div dir=3D"ltr"><div class=3D"gmail_default" style=3D"font-family:verdana,=
sans-serif;font-size:small;color:#000000">Hi Pedro,</div><div class=3D"gmai=
l_default" style=3D"font-family:verdana,sans-serif;font-size:small;color:#0=
00000">I loved the way you explained every steps.=C2=A0</div><div class=3D"=
gmail_default" style=3D"font-family:verdana,sans-serif;font-size:small;colo=
r:#000000">I also practice DDD and in my project we did all of the above st=
eps exactly you said.</div><div class=3D"gmail_default" style=3D"font-famil=
y:verdana,sans-serif;font-size:small;color:#000000"><br></div><div class=3D=
"gmail_default" style=3D"font-family:verdana,sans-serif;font-size:small;col=
or:#000000">However we do 2 things different.=C2=A0</div><div class=3D"gmai=
l_default" style=3D"font-family:verdana,sans-serif;font-size:small;color:#0=
00000"><br></div><div class=3D"gmail_default" style=3D"font-family:verdana,=
sans-serif;font-size:small;color:#000000"><b>1) How we save domain objects<=
/b></div><div class=3D"gmail_default" style=3D"font-family:verdana,sans-ser=
if;font-size:small;color:#000000">In order to save domain models we do not =
convert it in some other dto.=C2=A0</div><div class=3D"gmail_default" style=
=3D"font-family:verdana,sans-serif;font-size:small;color:#000000">We just s=
ave the Domain Model as it is. We use Entity Framework and it supports Code=
 First that is what we use. Our Repository implementation in the Infrastruc=
ture directly saves Entity ( Aggregate Root ) in Database.</div><div class=
=3D"gmail_default" style=3D"font-family:verdana,sans-serif;font-size:small;=
color:#000000">Therefore, we do not need mapper here.</div><div class=3D"gm=
ail_default" style=3D"font-family:verdana,sans-serif;font-size:small;color:=
#000000"><br></div><div class=3D"gmail_default" style=3D"font-family:verdan=
a,sans-serif;font-size:small;color:#000000"><b>2) How we map domain object =
to UI dtos</b></div><div class=3D"gmail_default" style=3D"font-family:verda=
na,sans-serif;font-size:small;color:#000000">In our application layer itsel=
f we have class called as Assembler and that class basically converts Model=
 To DTO and DTO to Model.=C2=A0</div><div class=3D"gmail_default" style=3D"=
font-family:verdana,sans-serif;font-size:small;color:#000000">For example i=
f I have Order Aggregate Root Then I have Order Folder in Application Servi=
ce Layer. Order Folder has OrderAppService.cs and OrderAssembler.cs.=C2=A0<=
/div><div class=3D"gmail_default" style=3D"font-family:verdana,sans-serif;f=
ont-size:small;color:#000000">OrderAppService takes dependency of OrderAsse=
mbler and calls MapToDto method by giving domain entity. Basically we have =
our mapper in Application layer itself.=C2=A0</div><div class=3D"gmail_defa=
ult" style=3D"font-family:verdana,sans-serif;font-size:small;color:#000000"=
><br></div><div class=3D"gmail_default" style=3D"font-family:verdana,sans-s=
erif;font-size:small;color:#000000">Hope this helps!</div><div class=3D"gma=
il_default" style=3D"font-family:verdana,sans-serif;font-size:small;color:#=
000000"><br></div><div class=3D"gmail_default" style=3D"font-family:verdana=
,sans-serif;font-size:small;color:#000000"><br></div><div class=3D"gmail_de=
fault" style=3D"font-family:verdana,sans-serif;font-size:small;color:#00000=
0"><br></div><div class=3D"gmail_default" style=3D"font-family:verdana,sans=
-serif;font-size:small;color:#000000"><br></div><div class=3D"gmail_default=
" style=3D"font-family:verdana,sans-serif;font-size:small;color:#000000"><b=
r></div><div class=3D"gmail_default" style=3D"font-family:verdana,sans-seri=
f;font-size:small;color:#000000"><br></div><div class=3D"gmail_default" sty=
le=3D"font-family:verdana,sans-serif;font-size:small;color:#000000"><br></d=
iv><div class=3D"gmail_default" style=3D"font-family:verdana,sans-serif;fon=
t-size:small;color:#000000"><br></div><div class=3D"gmail_default" style=3D=
"font-family:verdana,sans-serif;font-size:small;color:#000000"><br></div><d=
iv class=3D"gmail_default" style=3D"font-family:verdana,sans-serif;font-siz=
e:small;color:#000000"><br></div></div><div class=3D"gmail_extra"><br clear=
=3D"all"><div><div class=3D"gmail_signature" data-smartmail=3D"gmail_signat=
ure"><div dir=3D"ltr"><div><div dir=3D"ltr"><div><font size=3D"2"><br><span=
 style=3D"background-color:rgb(255,255,255)"><br><font face=3D"verdana, san=
s-serif" color=3D"#000000">Thanks,</font></span></font></div><div><span sty=
le=3D"background-color:rgb(255,255,255)"><font size=3D"2" face=3D"verdana, =
sans-serif" color=3D"#000000">Rupesh kumar Tiwari</font></span></div><div s=
tyle=3D"color:rgb(102,51,0);font-family:verdana,sans-serif"><font size=3D"2=
"><b>=C2=A0</b></font></div></div></div></div></div></div>
<br><div class=3D"gmail_quote">On Thu, Apr 27, 2017 at 12:44 PM, <a href=3D=
"mailto:[email protected]">[email protected]</a> [domaindrivend=
esign] <span dir=3D"ltr">&lt;<a href=3D"mailto:domaindrivendesign@yahoogrou=
ps.com" target=3D"_blank">[email protected]</a>&gt;</span>=
 wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;bor=
der-left:1px #ccc solid;padding-left:1ex">




<u></u>


















=20
<div style=3D"background-color:#fff">
<span style=3D"display:none">=C2=A0</span>




<div id=3D"m_-5464398084282839754ygrp-mlmsg">
  <div id=3D"m_-5464398084282839754ygrp-msg">




    <div id=3D"m_-5464398084282839754ygrp-text">
=20=20=20=20=20=20
=20=20=20=20=20=20
      <p></p><p><span>Hey guys,=C2=A0</span></p><p><span><br></span></p><p>=
<span>How do you manage data mapping in DDD?</span></p><p>To me, to follow =
DDD principles, we&#39;d need to do the following:</p><p></p><ul><li>client=
s have their own DTO, to represent the data in their level</li><li>clients =
do NOT work with domain objects such entities, aggregates ... domain model,=
 in general;</li><li>clients call, via controllers/application services, th=
e domain services from the domain</li><li>domain services talk to repositor=
ies, and both work with the domain model</li><li>repository definition belo=
ngs to the domain layer, but implementation belongs to infrastructure;</li>=
<li>repository implementation need mappers to convert from domain model to =
DTO and vice-versa;</li></ul><div>Some consequences of the DDD are that, in=
 the domain model, I&#39;d never put JPA annotations (and any other infrast=
ructure-related stuff). DTOs, in the infrastructure layer, would be the cla=
sses that are actually mapped with such a thing.=C2=A0</div><div><br></div>=
<div>But then we need data mapping between client and domain and also domai=
n and infrastructure.=C2=A0</div><div>Where do you usually put it? Would it=
 be kind of separate modules such as:</div><div><ul><li>a module (separate =
library) that maps client DTO&#39;s to domain objects, and the model depend=
s on it;</li><li>a module (separate library) that maps domain objects to in=
frastructure (example: DB) model and vice-versa, and the infrastructure dep=
ends on it (repository implementation receives domain objects as parameters=
, convert them or build whatever query it needs, persist/fetch data and, ev=
entually, converts data back to domain objects to return to the caller);</l=
i></ul><div>That&#39;s the only way I can think of, preserving the DDD prin=
ciples.=C2=A0</div><div>Doing so, we kinda preserve domain talking about do=
main, infra talking about data, no &#39;infra-related&#39; configuration on=
 domain objects, and also no clients speaking in the domain terms.=C2=A0</d=
iv><div><br></div><div>What do you think? How do you play with data mapping=
?=C2=A0</div><div>By the way, do you agree with my words?</div><div><br></d=
iv><div>Cheers,=C2=A0</div><div>Pedro</div></div><p></p>


    <p></p></div>
=20=20=20=20=20


=20=20=20=20
    <div style=3D"color:#fff;height:0"></div>




</div>






=20=20












</blockquote></div><br></div>




<!-- |**|begin egp html banner|**| -->

<br>


<br>

<!-- |**|end egp html banner|**| -->


<div width=3D"1" style=3D"color: white; clear: both;"/>__._,_.___</div>

=20=20=20=20=20=20
=20=20

=20=20=20=20
    <div id=3D"fromDMARC" style=3D"clear:both; margin-top: 10px;">
         <hr style=3D"height:2px ; border-width:0; color:#E3E3E3; backgroun=
d-color:#E3E3E3;">
         Posted by: Rupesh Kumar Tiwari &lt;[email protected]&gt;         <h=
r style=3D"height:2px ; border-width:0; color:#E3E3E3; background-color:#E3=
E3E3;">
    </div>
<!-- Start Recommendations -->
<!-- End Recommendations -->



<!-- |**|begin egp html banner|**| -->

  <img src=3D"http://geo.yahoo.com/serv?s=3D97476590/grpId=3D8116923/grpspI=
d=3D1705007181/msgId=3D24819/stime=3D1494000664" width=3D"1" height=3D"1"> =
<br>

<!-- |**|end egp html banner|**| -->

=20=20
<!-- |**|begin egp html banner|**| -->

<br>



=20=20=20
=20=20=20=20=20
=20
        <!-- |**|begin egp html banner|**| -->
        <div id=3D"ygrp-vital" style=3D"background-color: #f2f2f2; font-fam=
ily: Verdana; font-size: 10px; margin-bottom: 10px; padding: 10px;">

        <span id=3D"vithd" style=3D"font-weight: bold; color: #333; text-tr=
ansform: uppercase; "><a href=3D"https://groups.yahoo.com/neo/groups/domain=
drivendesign/info;_ylc=3DX3oDMTJlb3ZxMzAwBF9TAzk3MzU5NzE0BGdycElkAzgxMTY5Mj=
MEZ3Jwc3BJZAMxNzA1MDA3MTgxBHNlYwN2dGwEc2xrA3ZnaHAEc3RpbWUDMTQ5NDAwMDY2NA--"=
 style=3D"text-decoration: none;">Visit Your Group</a></span>

     <ul style=3D"list-style-type: none; margin: 0; padding: 0; display: in=
line;">
            <li style=3D"border-right: 1px solid #000; font-weight: 700; di=
splay: inline; padding: 0 5px; margin-left: 0;">
      <span class=3D"cat"><a href=3D"https://groups.yahoo.com/neo/groups/do=
maindrivendesign/members/all;_ylc=3DX3oDMTJmZ2ZwbWthBF9TAzk3MzU5NzE0BGdycEl=
kAzgxMTY5MjMEZ3Jwc3BJZAMxNzA1MDA3MTgxBHNlYwN2dGwEc2xrA3ZtYnJzBHN0aW1lAzE0OT=
QwMDA2NjQ-" style=3D"text-decoration: none;">New Members</a></span>
      <span class=3D"ct" style=3D"color: #ff7900;">3</span>
    </li>
                                              </ul>
  </div>


<div id=3D"ft" style=3D"font-family: Arial; font-size: 11px; margin-top: 5p=
x; padding: 0 2px 0 0; clear: both;">
  <a href=3D"https://groups.yahoo.com/neo;_ylc=3DX3oDMTJkZWZhbHVjBF9TAzk3ND=
c2NTkwBGdycElkAzgxMTY5MjMEZ3Jwc3BJZAMxNzA1MDA3MTgxBHNlYwNmdHIEc2xrA2dmcARzd=
GltZQMxNDk0MDAwNjY0" style=3D"float: left;"><img src=3D"http://l.yimg.com/r=
u/static/images/yg/img/email/new_logo/logo-groups-137x15.png" height=3D"15"=
 width=3D"137" alt=3D"Yahoo! Groups" style=3D"border: 0;"/></a>
  <div style=3D"color: #747575; float: right;"> &bull; <a href=3D"https://i=
nfo.yahoo.com/privacy/us/yahoo/groups/details.html" style=3D"text-decoratio=
n: none;">Privacy</a> &bull; <a href=3D"mailto:domaindrivendesign-unsubscri=
[email protected]?subject=3DUnsubscribe" style=3D"text-decoration: none;">=
Unsubscribe</a> &bull; <a href=3D"https://info.yahoo.com/legal/us/yahoo/uto=
s/terms/" style=3D"text-decoration: none;">Terms of Use</a> </div>
</div>

<!-- |**|end egp html banner|**| -->

  </div> <!-- ygrp-msg -->

=20=20=20


  <br>

<!-- |**|end egp html banner|**| -->


<div style=3D"color: white; clear: both;"/>__,_._,___</div>
</body>

</html>

--94eb2c19105e262ad0054e2cabac--