Modeling interactions between 2 aggregates

"Luis Abreu [email protected] [domaindrivendesign]" <[email protected]> Mon, 08 Aug 2016 11:48:54 +0000
Newsgroups gmane.comp.programming.domain-driven-design
Message-ID <CAA0ri5CAdaKpkB-bjJBH3c2829UWmJeKsW=+bkd6YAA1yG5H5g@mail.gmail.com>
--047d7ba96fc4d33c8905398e0023
Content-Type: text/plain; charset=UTF-8

Hello guys,

I'm trying to refactor a legacy app and I'm having some problems when
trying to model the interaction between 2 aggregates: Request and
PaymentList. Requests represent refund requests made by workers and are
responsible for calculating the ammount of refund each worker should
receive. Payment lists are used to represent the payment of several
request's refunds (basicaly, payment lists aggregate several requests and
they're used to generate SEPA files that are sent to the banks)

Internally, each one of this aggregates has its internal states represented
by simple object graphs, which are mapped into an existing database with
nhibernate. We currently have something like this:

RequestsState  * ----- 1 PaymentListsState

In other words, Request has a PaymenlListId field, which allows nulls and,
when set, must reference an existing payment list. The problem is that the
tables are using autonumbers and that means that I can't write code like
this to create a new payment list:

//code from service layer
var paymentList = new PaymentList(new PaymentListState() );
paymentList.createListForBanks(cmd); //cmd is a command with all the info
required for creating new list
_repository.Save(paymentList)


//code for createListForBanks
public void CreateListForBanks(NewPaymentList cmd ){
 //other internal info set from cmd
 IEnumerable<Request> requests = cmd.Requests;
 foreach(var request in requests){
  request.Pay(this); //this.ID is still 0 because it hasn't been saved
 }
}

The problem is that the payment list's id is still not set, so the
pseudo-code presented will not work. in order for it to work, I must put
the foreach statement outside ot the CreateListsForBanks method like this:
paymentList.createListForBanks(cmd); //cmd is a command with all the info
required for creating new list
_repository.Save(paymentList)
 IEnumerable<Request> requests = cmd.Requests;
 foreach(var request in requests){
  request.Pay(this); //this.ID is still 0 because it hasn't been saved
 }

Or I'll have to pass the repository inside the CreateListForBanks method so
that the objetct gets saved before marking the request as payed.

Unfortunately, none of those options makes any sense for me, but I might
not be seing the obvious...Since I can't add new fields to the database (my
first thought was to add a GUID or something like that), what would be the
best solution for this problem?

thanks!


Regards,
Luis

--047d7ba96fc4d33c8905398e0023
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">Hello guys,<div><br></div><div>I&#39;m trying to refactor =
a legacy app and I&#39;m having some problems when trying to model the inte=
raction between 2 aggregates: Request and PaymentList. Requests represent r=
efund requests made by workers and are responsible for calculating the ammo=
unt of refund each worker should receive. Payment lists are used to represe=
nt the payment of several request&#39;s refunds (basicaly, payment lists ag=
gregate several requests and they&#39;re used to generate SEPA files that a=
re sent to the banks)</div><div><br></div><div>Internally, each one of this=
 aggregates has its internal states represented by simple object graphs, wh=
ich are mapped into an existing database with nhibernate. We currently have=
 something like this:<br><br>RequestsState =C2=A0* ----- 1 PaymentListsStat=
e</div><div><br></div><div>In other words, Request has a PaymenlListId fiel=
d, which allows nulls and, when set, must reference an existing payment lis=
t. The problem is that the tables are using autonumbers and that means that=
 I can&#39;t write code like this to create a new payment list:</div><div><=
br></div><div>//code from service layer</div><div>var paymentList =3D new P=
aymentList(new PaymentListState() );</div><div>paymentList.createListForBan=
ks(cmd); //cmd is a command with all the info required for creating new lis=
t</div><div>_repository.Save(paymentList)</div><div><br><br>//code for crea=
teListForBanks<br>public void CreateListForBanks(NewPaymentList cmd ){</div=
><div>=C2=A0//other internal info set from cmd</div><div>=C2=A0IEnumerable&=
lt;Request&gt; requests =3D cmd.Requests; =C2=A0<br></div><div>=C2=A0foreac=
h(var request in requests){</div><div>=C2=A0 request.Pay(this); //this.ID i=
s still 0 because it hasn&#39;t been saved</div><div>=C2=A0}</div><div>}</d=
iv><div><br></div><div>The problem is that the payment list&#39;s id is sti=
ll not set, so the pseudo-code presented will not work. in order for it to =
work, I must put the foreach statement outside ot the CreateListsForBanks m=
ethod like this:</div><div><div>paymentList.createListForBanks(cmd); //cmd =
is a command with all the info required for creating new list</div><div>_re=
pository.Save(paymentList)</div><div>=C2=A0IEnumerable&lt;Request&gt; reque=
sts =3D cmd.Requests; =C2=A0=C2=A0=C2=A0<br></div><div><div>=C2=A0foreach(v=
ar request in requests){</div><div>=C2=A0 request.Pay(this); //this.ID is s=
till 0 because it hasn&#39;t been saved</div><div>=C2=A0}</div></div><br cl=
ass=3D"inbox-inbox-Apple-interchange-newline"></div><div>Or I&#39;ll have t=
o pass the repository inside the CreateListForBanks method so that the obje=
tct gets saved before marking the request as payed.=C2=A0</div><div><br></d=
iv><div>Unfortunately, none of those options makes any sense for me, but I =
might not be seing the obvious...Since I can&#39;t add new fields to the da=
tabase (my first thought was to add a GUID or something like that), what wo=
uld be the best solution for this problem?</div><div><br></div><div>thanks!=
</div><div><br></div><div><br></div><div>Regards,</div><div>Luis</div><div>=
<br></div><div><br></div><div><br></div></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: Luis Abreu &lt;[email protected]&gt;         <hr style=
=3D"height:2px ; border-width:0; color:#E3E3E3; background-color:#E3E3E3;">
    </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=3D24782/stime=3D1470656948" 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=3DX3oDMTJlYXEwbzBjBF9TAzk3MzU5NzE0BGdycElkAzgxMTY5Mj=
MEZ3Jwc3BJZAMxNzA1MDA3MTgxBHNlYwN2dGwEc2xrA3ZnaHAEc3RpbWUDMTQ3MDY1Njk0OA--"=
 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=3DX3oDMTJmZ2lxcWE5BF9TAzk3MzU5NzE0BGdycEl=
kAzgxMTY5MjMEZ3Jwc3BJZAMxNzA1MDA3MTgxBHNlYwN2dGwEc2xrA3ZtYnJzBHN0aW1lAzE0Nz=
A2NTY5NDg-" style=3D"text-decoration: none;">New Members</a></span>
      <span class=3D"ct" style=3D"color: #ff7900;">1</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=3DX3oDMTJkMmtlZXZlBF9TAzk3ND=
c2NTkwBGdycElkAzgxMTY5MjMEZ3Jwc3BJZAMxNzA1MDA3MTgxBHNlYwNmdHIEc2xrA2dmcARzd=
GltZQMxNDcwNjU2OTQ4" 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>

--047d7ba96fc4d33c8905398e0023--