index.xml 106 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Businessmanual on </title>
    <link>http://beta.seedstack.org/sections/businessmanual/</link>
    <description>Recent content in Businessmanual on </description>
    <generator>Hugo -- gohugo.io</generator>
    <atom:link href="http://beta.seedstack.org/sections/businessmanual/index.xml" rel="self" type="application/rss+xml" />
    
    <item>
      <title>Aggregates and their lifecycle</title>
      <link>http://beta.seedstack.org/docs/business/manual/lifecycle/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>http://beta.seedstack.org/docs/business/manual/lifecycle/</guid>
      <description>

&lt;p&gt;Domain objects have a lifecycle: they are created, go through various states and eventually die (either being archived
or deleted). Many of these objects are simple, transient objects which are simply created with a call to their constructor
and thrown away after use. There is no need to complicate such objects. But some objects are more rich, with a complex
lifecycle and numerous relationships with other objects. The challenges of managing these objects can be addressed by
three patterns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Aggregates&lt;/strong&gt;, which define clear ownership and boundaries between domain objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Repositories&lt;/strong&gt;, which encapsulate the logic of persisting and retrieving aggregates.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Factories&lt;/strong&gt;, which encapsulate the logic of creating aggregates.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&#34;aggregates&#34;&gt;Aggregates&lt;/h1&gt;

&lt;p&gt;Most business domains have very interconnected Entities, sometimes up to the point where there is always a path going from
any Entity to any other. We can try to minimize the number of associations in our design, and this is a good practice
but it can lead to a translation loss between business and software. In a typical object model, it is difficult to clearly
see the boundaries of a change. This is particularly acute in systems with concurrent access such as Web applications.&lt;/p&gt;

&lt;div class=&#34;callout callout-info&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
            
                &lt;i class=&#34;fa fa-info-circle icon-2x&#34; style=&#34;line-height: 30px;&#34;&gt;&lt;/i&gt;
            
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;It is difficult to guarantee the consistency of changes to objects in a model with complex associations and no clear
boundaries between objects. Considering every object as independent from each other is not a valid approach, but on the
other side, refreshing every object because of a change is not practical. A balanced solution must be found.&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;To help us with this delicate problem, the Aggregate pattern can be applied. It is a cluster of associated objects that
are considered as a unit for the purpose of data changes. Each Aggregate has root and a boundary which determines what
is inside the Aggregate. The Aggregate root is a specific Entity contained in the Aggregate. It is the only entry-point
of the Aggregate, meaning that it is the only Aggregate Entity that client objects can hold references to. Other objects
of the Aggregate are only accessible through the context of the Aggregate root.&lt;/p&gt;

&lt;h2 id=&#34;characteristics&#34;&gt;Characteristics&lt;/h2&gt;

&lt;p&gt;The following rules apply to Aggregates:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Aggregate root has a global identity and is responsible for checking invariants within the Aggregate.&lt;/li&gt;
&lt;li&gt;Non-root Entities inside the Aggregate have a local identity that is only unique within the Aggregate.&lt;/li&gt;
&lt;li&gt;Code outside the Aggregate can only hold references to the Aggregate root. The root can hand references to internal
entities but they must only use them transiently and not hold to the reference. Value Objects can be handed without
any concern because they are immutable and side-effect free.&lt;/li&gt;
&lt;li&gt;Only Aggregate roots can be loaded directly from the persistence. All other objects must be found by traversal of
associations.&lt;/li&gt;
&lt;li&gt;Any change within the Aggregate boundary must satisfy all the Aggregate invariants.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;declaration&#34;&gt;Declaration&lt;/h2&gt;

&lt;p&gt;Creating an Aggregate with the Business Framework, consists in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating a package with the Aggregate name,&lt;/li&gt;
&lt;li&gt;Create all the domain objects belonging to the Aggregate in this package,&lt;/li&gt;
&lt;li&gt;From all those domain objects, one Entity should be created as the &lt;strong&gt;Aggregate root&lt;/strong&gt;. This step is the focus of this
section.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To create an Aggregate root using the Business framework you have three choices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Extend the &lt;code&gt;BaseAggregateRoot&lt;/code&gt; class. This class directly extends &lt;code&gt;BaseEntity&lt;/code&gt; and as such the &lt;code&gt;equals()&lt;/code&gt;, &lt;code&gt;hashCode()&lt;/code&gt;
and &lt;code&gt;compareTo()&lt;/code&gt; methods will be provided out-of-the-box.&lt;/li&gt;
&lt;li&gt;Implement the &lt;code&gt;AggregateRoot&lt;/code&gt; interface. You must implement the &lt;code&gt;equals()&lt;/code&gt;, &lt;code&gt;hashCode()&lt;/code&gt; and &lt;code&gt;compareTo()&lt;/code&gt; methods in
this case.&lt;/li&gt;
&lt;li&gt;Simply annotate any class with the &lt;code&gt;@DomainAggregateRoot&lt;/code&gt; annotation. In this case, you won&amp;rsquo;t be able to use helpers and
tools from the framework.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With the two first options (base class and interface), you have to provide a generic parameter with the type of the
Aggregate root identifier.&lt;/p&gt;

&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;public class Order extends BaseAggregateRoot&amp;lt;Long&amp;gt; {
    private Long orderId;
    private Date checkoutDate;
    private double price;
    private List&amp;lt;OrderItem&amp;gt; items;

    Order() {
    }

    @Override
    public Long getEntityId() {
        return orderId;
    }

    public void addOrderItem(int quantity, long productId, double price) {
        OrderItem orderItem = new OrderItem();
        orderItem.setQuantity(quantity);
        orderItem.setProductId(productId);
        orderItem.setPrice(price);
        items.add(orderItem);
    }

    public void clearOrderItems() {
        items.clear();
    }

    public Long getOrderId() {
        return orderId;
    }

    ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h1 id=&#34;repositories&#34;&gt;Repositories&lt;/h1&gt;

&lt;p&gt;A Repository is used to store and retrieve Aggregates from persistence. Aggregates are only manipulated by repositories
through their Aggregate roots.&lt;/p&gt;

&lt;h2 id=&#34;default-repository&#34;&gt;Default repository&lt;/h2&gt;

&lt;p&gt;The Business Framework provides a default repository that can perform CRUD actions on an aggreate.
It can be injected with the &lt;code&gt;Repository&lt;/code&gt; interface and a qualifier.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@Inject @Jpa
private Repository&amp;lt;Customer, String&amp;gt; customerRepo;

...
Customer customer = customerRepo.load(customerId);
&lt;/code&gt;&lt;/pre&gt;

&lt;div class=&#34;callout callout-info&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
            
                &lt;i class=&#34;fa fa-info-circle icon-2x&#34; style=&#34;line-height: 30px;&#34;&gt;&lt;/i&gt;
            
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;By default, you have to explicitly specify the qualifier.
But if you have only one persistence and one default repository, you can configure the one to use.
The default repository can be configured for all the application:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[org.example.*]
default.repository.qualifier=org.seedstack.jpa.Jpa
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And then overriden for a specific aggregate root:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[org.example.domain.customer.Customer]
default.repository.qualifier=another-qualifier
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;default.repository.qualifier&lt;/code&gt; property expects a qualifier annotation class or a string when the qualifier use &lt;code&gt;@Named(&amp;quot;another-qualifer&amp;quot;)&lt;/code&gt;.&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;h2 id=&#34;explicit-repository&#34;&gt;Explicit repository&lt;/h2&gt;

&lt;p&gt;The default CRUD repository is interesting to start quickly and might be enough for some use cases.
But it is possible to add your own repositories, in order to extend the CRUD behavior with your domain
requirements.&lt;/p&gt;

&lt;p&gt;First create a repository interface extending &lt;code&gt;GenericRepository&lt;/code&gt;. This interface is usually located in the aggregate package.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import org.seedstack.business.domain.Repository;

public interface OrderRepository extends GenericRepository&amp;lt;Order, Long&amp;gt; {

     Order findOrderByCategory(String categoryId)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;div class=&#34;callout callout-info&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
            
                &lt;i class=&#34;fa fa-info-circle icon-2x&#34; style=&#34;line-height: 30px;&#34;&gt;&lt;/i&gt;
            
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;It is possible not to use the &lt;code&gt;GenericRepository&lt;/code&gt; interface and instead annotate the class with the &lt;code&gt;@DomainRepository&lt;/code&gt; annotation.
But you won&amp;rsquo;t be able to use the framework&amp;rsquo;s helpers like the assembler DSL &lt;code&gt;fromRepository()&lt;/code&gt; method.&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Then add an implementation in the infrastructure layer.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class OrderJpaRepository extends BaseJpaRepository&amp;lt;Order, Long&amp;gt; implements OrderRepository {

    @Override
    public Order findOrderByCategory(String categoryId){ ... }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;usage&#34;&gt;Usage&lt;/h2&gt;

&lt;p&gt;An explicit repository can be injected like a default one (with the &lt;code&gt;Repository&lt;/code&gt; interface) or with its own interface:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@Inject
private Repository&amp;lt;Order, Long&amp;gt; repository;
&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code&gt;@Inject
private OrderRepository repository;
&lt;/code&gt;&lt;/pre&gt;

&lt;h1 id=&#34;factories&#34;&gt;Factories&lt;/h1&gt;

&lt;p&gt;A factory is used to &lt;strong&gt;create domain objects&lt;/strong&gt;, checking that provided data is complete and consistent.&lt;/p&gt;

&lt;p&gt;To be created by a factory the domain object must also implements &lt;code&gt;Producible&lt;/code&gt;. This is necessary because all
the domain objects are not producible by a factory. For instance an entity is only be producible by an aggregate
root.&lt;/p&gt;

&lt;p&gt;The types implementing &lt;code&gt;DomainObject&lt;/code&gt; and &lt;code&gt;Producible&lt;/code&gt; are the followings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AggregateRoot,&lt;/li&gt;
&lt;li&gt;DomainPolicy,&lt;/li&gt;
&lt;li&gt;ValueObject,&lt;/li&gt;
&lt;li&gt;DomainEvent,&lt;/li&gt;
&lt;li&gt;DomainService.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;default-factory&#34;&gt;Default factory&lt;/h2&gt;

&lt;p&gt;The default factory has a single method &lt;code&gt;create&lt;/code&gt; with varargs that will match via reflection the constructor corresponding
to the passed arguments. The created domain object should implement the desired constructors:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class Customer extends BaseAggregate&amp;lt;Long&amp;gt; {
    private Long id;

    Customer() {
        ...
    }

    Customer(String firstName, String lastName) { // This constructor will be called
        ...
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The default factory can then be injected and used by invoking its &lt;code&gt;create()&lt;/code&gt; method with arguments unambiguously corresponding
to only one constructor:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@Inject
Factory&amp;lt;Customer&amp;gt; factory;

Customer customer = factory.create(&amp;quot;John&amp;quot;, &amp;quot;Doe&amp;quot;);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;One benefit over the plain constructor approach is that default factories will invoke identity generation (see &lt;a href=&#34;#identity-generation&#34;&gt;below&lt;/a&gt;)
and/or validation automatically after object instantiation. The&lt;/p&gt;

&lt;p&gt;As other factories this method will provide validation on the created object. But it won&amp;rsquo;t survive to refactoring, so be careful using the method (ie. unit test it!).&lt;/p&gt;

&lt;div class=&#34;callout callout-info&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
            
                &lt;i class=&#34;fa fa-info-circle icon-2x&#34; style=&#34;line-height: 30px;&#34;&gt;&lt;/i&gt;
            
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;This factory can only be used to create domain objects that implement the &lt;code&gt;Producible&lt;/code&gt; and &lt;code&gt;DomainObject&lt;/code&gt; interfaces. Classes
extending Business framework base classes will already implement those interfaces but annotated POJO, such as policies or
services, must implement them explicitly. As an alternative, you implement the &lt;code&gt;GenericDomainPolicy&lt;/code&gt; and &lt;code&gt;GenericDomainService&lt;/code&gt;
interfaces instead.&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;h2 id=&#34;explicit-factory&#34;&gt;Explicit factory&lt;/h2&gt;

&lt;p&gt;An explicit factory is composed of an interface, which is located in the package of the aggregate it constructs, and an
implementation which can be located either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the aggregate package too,&lt;/li&gt;
&lt;li&gt;Or in an infrastructure package if it is dependent upon a specific technology.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The factory interface has to extend the &lt;code&gt;GenericFactory&lt;/code&gt; interface:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;package org.mycompany.myapp.domain.model.order;

import org.javatuples.Triplet;
import org.seedstack.business.domain.GenericFactory;

public interface OrderFactory extends GenericFactory&amp;lt;Order&amp;gt; {

    Order createOrder(String customerId, Date checkoutDate, Double price,
            List&amp;lt;Triplet&amp;lt;Integer, Double, Long&amp;gt;&amp;gt; orderItemTriplets);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Order&lt;/code&gt; is the type which is the expected to be returned by all the create methods.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;createOrder&lt;/code&gt; method creates an &lt;code&gt;Order&lt;/code&gt; aggregate with the required parameters. Some parameters can be grouped with a tuple like
&lt;code&gt;oderItemTriplets&lt;/code&gt; which represents a list &lt;code&gt;OrderItem&lt;/code&gt; entities belonging to the &lt;code&gt;Order&lt;/code&gt; aggregate (See
&lt;a href=&#34;#!/business-doc/concepts/oop#tuples&#34;&gt;here&lt;/a&gt; for more information on tuple pattern).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The factory implementation must extend the &lt;code&gt;BaseFactory&lt;/code&gt; abstract class and implement its own interface.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;package org.mycompany.myapp.domain.model.order;

import org.javatuples.Triplet;
import org.seedstack.business.domain.BaseFactory;
import org.mycompany.myapp.domain.customer.CustomerId;

public class OrderFactoryImpl extends BaseFactory&amp;lt;Order&amp;gt; implements OrderFactory {

    @Override
    public Order createOrder(String customerId) {
        Order o = new Order();

        o.setCustomerId(new CustomerId(customerId));
        o.setCheckoutDate(new Date());

        return o;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here, the factory encapsulates the logic of creating a minimal but valid &lt;code&gt;Order&lt;/code&gt; aggregate. This order can be further
populated by an assembler or by custom logic.&lt;/p&gt;

&lt;div class=&#34;callout callout-info&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
            
                &lt;i class=&#34;fa fa-info-circle icon-2x&#34; style=&#34;line-height: 30px;&#34;&gt;&lt;/i&gt;
            
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;When the implementation and its interface share the same package, the &lt;strong&gt;implementation should be in package visibility&lt;/strong&gt;.
It prevents any direct use of the implementation.&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;h1 id=&#34;identity-generation&#34;&gt;Identity generation&lt;/h1&gt;

&lt;p&gt;Factories provide methods to create entities with a well defined identity. But sometimes, you want to delegate the identity
creation, for instance to an Oracle sequence. For this use case Seed provides an &lt;strong&gt;identity generation strategies&lt;/strong&gt;.
A generation strategy makes sure a unique identity is provided to any new Entity before it is even persisted.&lt;/p&gt;

&lt;h2 id=&#34;declaration-1&#34;&gt;Declaration&lt;/h2&gt;

&lt;p&gt;Below is an aggregate using the identity strategy:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;package org.mycompany.myapp.domain.model.myaggregate;

public class MyAggregate extends BaseAggregateRoot&amp;lt;UUID&amp;gt; {

    @Identity(handler = UUIDHandler.class)
    private UUID id;

    private String name;
    private MyEntity mySubEntity;
    private Set&amp;lt;MyEntity&amp;gt; mySubEntities;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Below is an Entity using the identity strategy:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;package org.mycompany.myapp.domain.model.myaggregate;

public class MyEntity extends BaseEntity&amp;lt;Long&amp;gt; {

    @Identity(handler = SequenceHandler.class)
    private Long id;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;@Identity&lt;/code&gt; annotation is applied on attribute holding the object identity. This annotation takes two arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;handler&lt;/code&gt;: strategy implementation&lt;/li&gt;
&lt;li&gt;&lt;code&gt;source&lt;/code&gt;: a String that can be used in a custom handler. For instance, it could provide a SEQUENCE name for DB.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Only specifying the identity strategy is not enough to effectively generate an identity. An implementation of the strategy
must be configured:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[org.mycompany.myapp.domain.model.myaggregate.MyAggregate]
identity.handler-qualifier = simple-UUID

[org.mycompany.myapp.domain.model.myaggregate.MyEntity]
identity.handler-qualifier = oracle-sequence
identity.sequence-name = SEQ_TEST
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this case we can see that the &lt;code&gt;simple-UUID&lt;/code&gt; implementation will be used for &lt;code&gt;MyAggregate&lt;/code&gt;. Similarly, the &lt;code&gt;oracle-sequence&lt;/code&gt;
implementation will be used for &lt;code&gt;MyEntity&lt;/code&gt; but is further configured with a sequence name.&lt;/p&gt;

&lt;h2 id=&#34;usage-1&#34;&gt;Usage&lt;/h2&gt;

&lt;p&gt;The chosen identity strategy is applied:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automatically, on methods annotated with the &lt;code&gt;@Create&lt;/code&gt; annotation. They are intercepted to apply the identity strategy
on their return value.&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;public class MyAggregateFactoryDefault extends BaseFactory&amp;lt;MyAggregate&amp;gt;
        implements MyAggregateFactory {

    @Create
    @Override
    public MyAggregate createMyAggregate(String name) {
        MyAggregate myAggregate = new MyAggregate();
        myAggregate.setName(name);

        MyEntity myEntity = createMyEntity();
        myAggregate.setMyEntity(myEntity);

        return myAggregate;
    }

    @Create
    MyEntity createMyEntity() {
        return new MyEntity();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;Manually, by injecting the &lt;code&gt;IdentityService&lt;/code&gt; service and invoking its &lt;code&gt;identify()&lt;/code&gt; method with the entity to generate
an identity for as argument.&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;public class MyAggregateFactoryDefault extends BaseFactory&amp;lt;MyAggregate&amp;gt;
        implements MyAggregateFactory {

    @Inject
    IdentityService identityService;

    @Override
    public MyAggregate createMyAggregate(String name) {
        MyAggregate myAggregate = new MyAggregate();
        identityService.identify(myAggregate);
        myAggregate.setName(name);

        MyEntity myEntity = new MyEntity();
        identityService.identify(myEntity);
        myAggregate.setMyEntity(myEntity);

        return myAggregate;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that identity generation doesn&amp;rsquo;t walk the object graph to generate identities for eventual sub-entities. You must
trigger identity generation (automatically or manually) separately on each entity.&lt;/p&gt;

&lt;p&gt;&lt;div class=&#34;callout callout-tips&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
                &lt;i class=&#34;fa fa-lightbulb-o icon-2x&#34; style=&#34;line-height: 30px;&#34;&gt;&lt;/i&gt;
            
            
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;If all methods of a factory delegate identity generation to Seed, a &lt;code&gt;@Create&lt;/code&gt; annotation can be applied directly at the
class or interface level.&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;&lt;/p&gt;

&lt;h2 id=&#34;custom-identity-handler&#34;&gt;Custom identity handler&lt;/h2&gt;

&lt;p&gt;&lt;img src=&#34;img/manage-entity-spi.svg&#34; alt=&#34;identity-seed&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Two different options are available to define custom identity handlers:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;img/manage-entity-usage.png&#34; alt=&#34;identity-seed&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Below is an example of a basic Timestamp id generation strategy:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;package org.mycompany.myapp.infrastructure.domain;

import org.seedstack.business.domain.BaseEntity;
import org.seedstack.business.domain.identity.IdentityHandler;

@Named(&amp;quot;timestamp-id&amp;quot;)
public class TimestampIdentityHandler implements IdentityHandler&amp;lt;BaseEntity&amp;lt;Long&amp;gt;, Long&amp;gt; {

    @Override
    public Long handle(BaseEntity&amp;lt;Long&amp;gt; entity, Map&amp;lt;String, String&amp;gt; entityConfig) {
        return new Date().getTime();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;provided-identity-strategies&#34;&gt;Provided identity strategies&lt;/h2&gt;

&lt;h3 id=&#34;sequencehandler&#34;&gt;SequenceHandler&lt;/h3&gt;

&lt;p&gt;Handles sequence generated ID. Two implementations are provided:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;OracleSequenceHandler&lt;/code&gt;: Get next oracle sequence value for new entity id. The following properties.&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;[org.mycompany.myapp...YourEntity]
identity.handler-qualifier = oracle-sequence
identity.sequence-name = your_sequence_name
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;InMemorySequenceHandler&lt;/code&gt;: To be used &lt;strong&gt;ONLY&lt;/strong&gt; for testing (preserves behaviour without a database). The following
properties.&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;[org.mycompany.myapp...YourEntity]
identity.handler-qualifier = inmemory-sequence
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&#34;uuidhandler&#34;&gt;UUIDHandler&lt;/h3&gt;

&lt;p&gt;Use for handling UUID generated ID. One implementation is provided:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;SimpleUUIDHandler&lt;/code&gt;: Get new random UUID from java.util.UUID.randomUUID(). Need one property using entity props
configuration:&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;[org.mycompany.myapp...YourEntity]
identity.handler-qualifier = simple-UUID
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For a full description of Entity properties configuration, refer to this
&lt;a href=&#34;#!/business-doc/hands-on-domain/entity#configuration-spi&#34;&gt;documentation&lt;/a&gt;.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Assemblers</title>
      <link>http://beta.seedstack.org/docs/business/manual/assemblers/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>http://beta.seedstack.org/docs/business/manual/assemblers/</guid>
      <description>

&lt;p&gt;The assembler pattern is used to transfer a representation of the state of &lt;em&gt;Aggregates&lt;/em&gt; to &lt;em&gt;DTO/Representation&lt;/em&gt; objects.
The Business Framework provides a interface and few base classes to ease the development of assemblers.&lt;/p&gt;

&lt;h1 id=&#34;default-assembler:239bb10033b541f8eba91ab88a07c463&#34;&gt;Default assembler&lt;/h1&gt;

&lt;p&gt;By default, if your mapping is obvious, you don&amp;rsquo;t have to create an explicit assembler. You just add the &lt;code&gt;@DtoOf&lt;/code&gt; annotation
on your DTO class to link them to their related Aggregate root:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@DtoOf(Product.class)
class ProductRepresentation {
    ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can then inject a ModelMapper-based assembler with the &lt;code&gt;@ModelMapper&lt;/code&gt; annotation:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@Inject
@ModelMapper
Assembler&amp;lt;Product, ProductRepresentation&amp;gt; productAssembler;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This assembler uses the default settings of the &lt;a href=&#34;http://modelmapper.org/&#34;&gt;ModelMapper library&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&#34;callout callout-warning&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
            
            
                &lt;i class=&#34;fa fa-exclamation-triangle icon-2x&#34;&gt;&lt;/i&gt;
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;It is strongly recommended to always have a DTO. &lt;strong&gt;Never serialize domain objects to your clients.&lt;/strong&gt; Even with the default
assembler you can define a DTO that is a flat and/or partial mapping of your domain Aggregate. ModelMapper will
automatically figure out the field mapping if you respect &lt;a href=&#34;http://modelmapper.org/getting-started/#mapping&#34;&gt;their conventions&lt;/a&gt;.
Besides, you should always have integration tests that checks that the automatic mapping is correct.&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;h1 id=&#34;explicit-assembler:239bb10033b541f8eba91ab88a07c463&#34;&gt;Explicit assembler&lt;/h1&gt;

&lt;p&gt;Create an assembler extending &lt;code&gt;BaseAssembler&lt;/code&gt; class. It will contains
the logic of the copy between aggregate and DTO. Then, inject your
assembler in your class.&lt;/p&gt;

&lt;p&gt;There are two methods to implement:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;doAssembleDtoFromAggregate(dto, aggregate)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;doMergeAggregateWithDto(aggregate, dto)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first method creates a DTO from an aggregate root. The second
merge the mutable fields of an &lt;strong&gt;existing&lt;/strong&gt; aggregate with the data
from the dto.&lt;/p&gt;

&lt;div class=&#34;callout callout-info&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
            
                &lt;i class=&#34;fa fa-info-circle icon-2x&#34; style=&#34;line-height: 30px;&#34;&gt;&lt;/i&gt;
            
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;Note that the aggregate identity should never be updated by the DTO.&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;h1 id=&#34;example:239bb10033b541f8eba91ab88a07c463&#34;&gt;Example&lt;/h1&gt;

&lt;p&gt;An assembler assembling a representation of a product.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class ProductAssembler extends BaseAssembler&amp;lt;Product,ProductRepresentation&amp;gt; {

    @Override
    protected void doAssembleDtoFromAggregate(ProductRepresentation targetDto, 
	         Product sourceAggregate) {
        // Flatten the id
        targetDto.setStoreId(sourceAggregate.getAggregateId().getStoreId());
        targetDto.setProductCode(sourceAggregate.getAggregateId().getProductCode());
         
        targetDto.setName(sourceAggregate.getName());
        targetDto.setDescription(sourceAggregate.getDescription());
    }
	 
    @Override
    protected void doMergeAggregateWithDto(Product targetAggregate, 
	        ProductRepresentation sourceDto) {
        // The id is not modified
        targetAggregate.setName(sourceDto.getName());
        targetAggregate.setDescription(sourceDto.getDescription());
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can inject the assembler via its interface or directly via the
implementation class. Both way are acceptable, but the first way
provides a more encapsulated interface to the developer using it.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@Inject
Assembler&amp;lt;Product, ProductRepresentation&amp;gt; productAssembler;

@Inject
ProductAssembler productAssembler;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// assemble a representation
representation = productAssembler.assembleDtoFromAggregate(product);

// merge an aggregate
Product productToMerge = catalog.retrieve(productId);
productAssembler.mergeAggregateWithDto(productToMerge, productRepresentationSource);
catalog.update(productToMerge);
&lt;/code&gt;&lt;/pre&gt;

&lt;h1 id=&#34;domain-specific-language:239bb10033b541f8eba91ab88a07c463&#34;&gt;Domain-Specific Language&lt;/h1&gt;

&lt;p&gt;Assembler implementations remains simple, but its usage can become
tedious when using lists or complexe worflows. To simplify this use
cases the Business Framework provides a DSL throught
&lt;code&gt;FluentAssembler&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&#34;lists:239bb10033b541f8eba91ab88a07c463&#34;&gt;Lists&lt;/h2&gt;

&lt;p&gt;Replace common boilerplate code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;List&amp;lt;ProductRepresentation&amp;gt; representations = new ArrayList&amp;lt;&amp;gt;();
for (Product product : products {
    representations.add(productAssembler.assembleDtoFromAggregate(product))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;by oneliners:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fluentAssembler.assemble(products).to(ProductRepresentation.class)
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;tuples:239bb10033b541f8eba91ab88a07c463&#34;&gt;Tuples&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;fluentAssembler.assemble(product, order).to(RecipeRepresentation.class)
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;qualified-assemblers:239bb10033b541f8eba91ab88a07c463&#34;&gt;Qualified assemblers&lt;/h2&gt;

&lt;p&gt;You can specify the assembler the DSL should use by specifying a
qualifier.
For instance, the following example use the &lt;strong&gt;default ModelMapper
assembler&lt;/strong&gt;.
So in this case you don&amp;rsquo;t even have to create an assembler class.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fluently.assemble(products).with(AssemblerTypes.MODEL_MAPPER).to(MyDto.class)
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;advanced-usage:239bb10033b541f8eba91ab88a07c463&#34;&gt;Advanced usage&lt;/h2&gt;

&lt;p&gt;When we merge a representation to an aggregate, the assembler expects
an existing aggregate root instance. Normally you have to retreive
this instance from a repository or to create it from a factory. This
can become a little tedious when you have to do it a lot. Or by adding
few metadata to your DTO, you can have the DSL doing it for you.&lt;/p&gt;

&lt;h3 id=&#34;get-an-aggregate-from-its-factory:239bb10033b541f8eba91ab88a07c463&#34;&gt;Get an aggregate from its factory&lt;/h3&gt;

&lt;p&gt;If the aggregate root to merge doesn&amp;rsquo;t exists, you can tell the DSL to
create it from its factory.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fluentAssembler.merge(repr).into(Product.class).fromFactory();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It will search the aggregate root&amp;rsquo;s factory
(&lt;code&gt;GenericFactory&amp;lt;Product&amp;gt;&lt;/code&gt;). Then it will search the appropriate
method to call. In order to indicate to the DSL which method should be called,
annotate the DTO&amp;rsquo;s getters matching the factory method&amp;rsquo;s parameters
with &lt;code&gt;@MatchingFactoryParameter(index=0)&lt;/code&gt;. The index represents the
position of the parameters in the factory method.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class ProductRepesentation {

    private Short storeId;
    private Short productCode;
    private String name;
    private String description

    @MatchingFactoryParameter(index=0)
    public Short getStoreId() { ... }

    @MatchingFactoryParameter(index=1)
    public Short getProductCode() { ... }

    @MatchingFactoryParameter(index=2)
    public Short getName() { ... }

    public Short getDescription() { ... }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code&gt;public interface ProductFactory extends GenericFactory&amp;lt;Product&amp;gt; {

	Product createProduct(Short storeId, Short productCode, String name);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&#34;get-an-aggregate-from-its-repository:239bb10033b541f8eba91ab88a07c463&#34;&gt;Get an aggregate from its repository&lt;/h3&gt;

&lt;p&gt;If the aggregate root to merge already exists, you can tell the DSL to
get it from its repository. If the DSL doesn&amp;rsquo;t find the aggregate root
from the repository, two strategies are possible. The first throw an
exception, the second fall back to the &lt;code&gt;fromFactory()&lt;/code&gt; method.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;try {
    product = fluentAssembler.merge(representation).into(Product.class)
        .fromRepository().orFail();

} catch (AggregateNotFoundException e) {
    return Response.status(Response.Status.NOT_FOUND).build();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code&gt;product = fluentAssembler.merge(repr).into(Product.class)
    .fromRepository().thenFromFactory();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It will search the aggregate root&amp;rsquo;s repository
(&lt;code&gt;GenericRepository&amp;lt;Product&amp;gt;&lt;/code&gt;). Then it will call its &lt;code&gt;load()&lt;/code&gt;
method.
In order to indicate to the DSL how to find the ID,
annotate the DTO getter matching the aggregate root ID with
&lt;code&gt;@MatchingEntityId&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&#34;callout callout-info&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
            
                &lt;i class=&#34;fa fa-info-circle icon-2x&#34; style=&#34;line-height: 30px;&#34;&gt;&lt;/i&gt;
            
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;If the ID is composite annotate the getter
methods matching the ID constructor parameters with
&lt;code&gt;@MatchingEntityId(index=0)&lt;/code&gt;. In this case, the index is mandatory and
represents the position of the parameters in the constructor method.&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;pre&gt;&lt;code&gt;public class ProductRepesentation {

    private Short storeId;
    private Short productCode;
    private String name;
    private String description

    @MatchingEntityId(index=0)
    public Short getStoreId() { ... }

    @MatchingEntityId(index=1)
    public Short getProductCode() { ... }

    public Short getName() { ... }

    public Short getDescription() { ... }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code&gt;public class ProductId extends BaseValueObject {

	private Short storeId;
	private Short productCode;

	public ProductId(Short storeId, Short productCode) {
		this.storeId = storeId;
		this.productCode = productCode;
	}

    // Getters ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h1 id=&#34;automation-with-modelmapper:239bb10033b541f8eba91ab88a07c463&#34;&gt;Automation with ModelMapper&lt;/h1&gt;

&lt;p&gt;DTOs are meant to expose domain objects or a part of these objects.
So there are often similarities between the DTOs and the domain
object.
This can lead to a lot of boilerplate code when the assembler doesn&amp;rsquo;t
hold complex logic but just populates objects.&lt;/p&gt;

&lt;p&gt;For this use case, you can now &lt;strong&gt;use a default&lt;/strong&gt; assembler based on
&lt;a href=&#34;http://modelmapper.org/&#34;&gt;ModelMapper&lt;/a&gt;. It&amp;rsquo;s an &lt;em&gt;automatic assembler&lt;/em&gt;
which provides an &lt;strong&gt;intelligent mapping&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&#34;how-to-use-it:239bb10033b541f8eba91ab88a07c463&#34;&gt;How to use it ?&lt;/h2&gt;

&lt;p&gt;The only thing you have to do is to annotate the DTO with
&lt;code&gt;@DtoOf(MyAggregate.class)&lt;/code&gt;. It tells the framework to bind a default
assembler for assembling &lt;code&gt;MyAggregate&lt;/code&gt; into the annotated DTO.
After you can inject the assembler as follows, and use it as usual.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@DtoOf(MyProduct.class)
public class MyDto { ... }
&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code&gt;@Inject
@ModelMapper
private Assembler&amp;lt;MyAggregate,MyDto&amp;gt; myDtoAssemebler;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;how-does-it-work:239bb10033b541f8eba91ab88a07c463&#34;&gt;How does it work ?&lt;/h2&gt;

&lt;p&gt;It uses a intelligent mapping provided by the ModelMapper library.
You can find the detailed explanation here
&lt;a href=&#34;http://modelmapper.org/user-manual/how-it-works/&#34;&gt;matching documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&#34;can-i-customize-it:239bb10033b541f8eba91ab88a07c463&#34;&gt;Can I customize it ?&lt;/h2&gt;

&lt;p&gt;Yes. If there are ambiguities, or if you only want to change the mapping of
one field, you don&amp;rsquo;t need to fall back to the old assembler. You can
just create an assembler extending the &lt;code&gt;ModelMapperAssembler&lt;/code&gt; class and
implement the methods returning a &lt;code&gt;ModelMapper&lt;/code&gt; instance.&lt;/p&gt;

&lt;p&gt;&lt;div class=&#34;callout callout-info&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
            
                &lt;i class=&#34;fa fa-info-circle icon-2x&#34; style=&#34;line-height: 30px;&#34;&gt;&lt;/i&gt;
            
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;See here &lt;a href=&#34;http://modelmapper.org/user-manual/property-mapping/&#34;&gt;how to override the mapping&lt;/a&gt;.&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;It supports:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deep mapping&lt;/li&gt;
&lt;li&gt;Skipping properties&lt;/li&gt;
&lt;li&gt;Providers&lt;/li&gt;
&lt;li&gt;Conditional mapping&lt;/li&gt;
&lt;li&gt;String mapping&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&#34;can-i-see-some-code:239bb10033b541f8eba91ab88a07c463&#34;&gt;Can I see some code ?&lt;/h3&gt;

&lt;p&gt;Below is an example from the &lt;a href=&#34;https://github.com/seedstack/store-webapp-sample&#34;&gt;Web application sample&lt;/a&gt;.
The Product contains a field &lt;code&gt;entityId&lt;/code&gt; and a field &lt;code&gt;categoryId&lt;/code&gt;, but the representation only contains a field
&lt;code&gt;id&lt;/code&gt;. So ModelMapper doesn&amp;rsquo;t know which field match.&lt;/p&gt;

&lt;p&gt;To fix this, extend &lt;code&gt;ModelMapperAssembler&lt;/code&gt; and add a &lt;code&gt;PropertyMap&lt;/code&gt; to the &lt;code&gt;modelMapper&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;div class=&#34;callout callout-info&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
            
                &lt;i class=&#34;fa fa-info-circle icon-2x&#34; style=&#34;line-height: 30px;&#34;&gt;&lt;/i&gt;
            
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;Note that, as you implemented an assembler, you don&amp;rsquo;t use the default
assembler anymore. So remove the &lt;code&gt;@DtoOf&lt;/code&gt; annotation on the DTO.&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class ProductModelMapperAssembler extends ModelMapperAssembler&amp;lt;Product, ProductRepresentation&amp;gt; {

    @Override
    protected void configureAssembly(ModelMapper modelMapper) {
        modelMapper.addMappings(new PropertyMap&amp;lt;Product, ProductRepresentation&amp;gt;() {
            @Override
            protected void configure() {
                // Required due to the ambiguity with the categoryId field
                map().setId(source.getEntityId());
            }
        });
    }

    @Override
    protected void configureMerge(ModelMapper modelMapper) {
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then use it as usual.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@Inject
private Assembler&amp;lt;Product, ProductRepresentation&amp;gt; assembler;
&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code&gt;assembler.assembleDtoFromAggregate(aProduct);
&lt;/code&gt;&lt;/pre&gt;

&lt;hr /&gt;
</description>
    </item>
    
    <item>
      <title>Basics</title>
      <link>http://beta.seedstack.org/docs/business/manual/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>http://beta.seedstack.org/docs/business/manual/</guid>
      <description>

&lt;p&gt;This manual will begin by to introducing and explaining all the concepts that you will need to know and master to build
successful business applications. These concepts come from the DDD software approach which is central to the Business
framework. You will NOT be presented with in-depth coverage of all the DDD subtleties, so to go beyond the basics you
should have a look at the &lt;a href=&#34;../bibliography&#34;&gt;bibliography&lt;/a&gt;. Moreover, links will be provided in the text when related
quality content is available.&lt;/p&gt;

&lt;p&gt;Domain-Driven Design is &lt;strong&gt;more about a way of thinking than about code&lt;/strong&gt; but this approach has nonetheless a great
impact on the way your business will be coded. DDD does not introduce new concepts or design patterns on its own. As
a matter of fact, you will surely recognize some patterns you already know behind DDD names. It goes beyond design
patterns though, as it dictates when, where and how to use them to solve real business problems.&lt;/p&gt;

&lt;div class=&#34;callout callout-info&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
            
                &lt;i class=&#34;fa fa-info-circle icon-2x&#34; style=&#34;line-height: 30px;&#34;&gt;&lt;/i&gt;
            
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;As its name suggest, DDD is focused on the business and its model. This model exists independently of applications and
specific use cases and should be designed a such. Applications are clients of the model: software that will use this
model to address enterprise needs. Naturally, particular use cases and UI ideas can be used as inputs for thinking about
the model as the software is produced. But the model itself should be independent of its clients.&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;h1 id=&#34;architecture&#34;&gt;Architecture&lt;/h1&gt;

&lt;p&gt;For starters, it is important to know that DDD doesn&amp;rsquo;t require a particular architecture. Since the domain is defined
and contained inside a &lt;a href=&#39;http://beta.seedstack.org/glossary#bounded-context&#39;&gt;bounded context&lt;/a&gt;, it doesn&amp;rsquo;t influence the architecture of the whole application
or system. There are several pertinent architectural styles that you can apply to the surroundings of the domain. Some
are very broad and tend to define every aspect of the system, others are more focused and try to address a specific
demand.&lt;/p&gt;

&lt;p&gt;The business framework can itself adapt to several of these architectural styles. In this section we will present
the &lt;strong&gt;layers architecture pattern&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&#34;layers&#34;&gt;Layers&lt;/h2&gt;

&lt;p&gt;The layers architecture pattern is commonly used in Web, enterprise and desktop applications. In this architecture
pattern, the concerns are separated into well-defined layers.&lt;/p&gt;

&lt;h3 id=&#34;traditional-definition&#34;&gt;Traditional definition&lt;/h3&gt;

&lt;p&gt;The main rule of this pattern is that each layer may couple only to itself and below. The strict layers architecture
only allows to couple to the layer directly below. We recommend to use the &lt;strong&gt;relaxed layers architecture&lt;/strong&gt; though, which
allows any higher-level layer to couple to any layer below it.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;img/layers.png&#34; alt=&#34;DDD applied to layers architecture&#34; /&gt;&lt;/p&gt;

&lt;h3 id=&#34;dependency-inversion-principle&#34;&gt;Dependency Inversion Principle&lt;/h3&gt;

&lt;p&gt;In the traditional view of the layers architecture, the infrastructure is at the bottom, containing technical
mechanisms like persistence, messaging or any component dependent on third-party libraries. So every upper-layer must
couple to the infrastructure layer to use the technical facilities. It is not desirable in DDD where we want to avoid
any coupling of the domain to the infrastructure. In fact we want to avoid any coupling of any layer to the
infrastructure. To achieve this independence, we will apply the &lt;strong&gt;&lt;a href=&#39;http://beta.seedstack.org/glossary#dependency-inversion-principle&#39;&gt;Dependency Inversion Principle&lt;/a&gt;&lt;/strong&gt; which
states that:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;High-level modules should not depend on low-level modules. Both should depend on abstractions.&lt;/p&gt;

&lt;p&gt;Abstractions should not depend upon details. Details should depend upon abstractions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What does it mean in terms of code, is that a low-level component should implement interfaces defined by high components.
As such, we can move the infrastructure layer to the side.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;img/layers_dip.png&#34; alt=&#34;DDD applied to layers architecture&#34; /&gt;&lt;/p&gt;

&lt;p&gt;As an example of this architecture, we would have a &lt;code&gt;JpaCustomerRepository&lt;/code&gt; implementation class which would belong to
the infrastructure and which implements the &lt;code&gt;CustomerRepository&lt;/code&gt; interface defined in the domain layer. When injecting
the repository elsewhere like in services, we would only use the interface. The
&lt;a href=&#34;http://beta.seedstack.org/docs/seed/concepts/dependency-injection/&#34;&gt;dependency injection mechanism&lt;/a&gt; of the Java framework would provide
the correct implementation from the infrastructure.&lt;/p&gt;

&lt;h2 id=&#34;interface-layer&#34;&gt;Interface layer&lt;/h2&gt;

&lt;p&gt;The interface layer contains the components that handle &lt;strong&gt;interactions with other systems&lt;/strong&gt;, such as Web application
views, REST resources, Web-Services, etc&amp;hellip;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It handles the &lt;strong&gt;interpretation&lt;/strong&gt;, &lt;strong&gt;validation&lt;/strong&gt; and &lt;strong&gt;translation&lt;/strong&gt; of the inputs.&lt;/li&gt;
&lt;li&gt;It handles the &lt;strong&gt;serialization&lt;/strong&gt; of the outputs, such as DTO classes to JSON, XML, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;application-layer&#34;&gt;Application layer&lt;/h2&gt;

&lt;p&gt;The application layer is responsible for &lt;strong&gt;driving the workflow of the application&lt;/strong&gt;, executing the use cases of the system.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;These operations are independent of the interfaces by which they are exposed.&lt;/li&gt;
&lt;li&gt;This layer is well suited for &lt;strong&gt;spanning transactions&lt;/strong&gt;, high-level &lt;strong&gt;logging&lt;/strong&gt; and &lt;strong&gt;security&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The application layer is thin in terms of domain logic, it merely &lt;strong&gt;coordinates the domain layer objects&lt;/strong&gt; to perform
the actual work through &lt;strong&gt;Application Services&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;domain-layer&#34;&gt;Domain layer&lt;/h2&gt;

&lt;p&gt;The Domain Layer is where the &lt;strong&gt;business is expressed&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The domain is independent of the use cases of the system, but is used to achieve their realization,&lt;/li&gt;
&lt;li&gt;It is a very &lt;strong&gt;behaviour-rich&lt;/strong&gt; and &lt;strong&gt;expressive&lt;/strong&gt; model of the domain, based on entities, values objects and aggregates.&lt;/li&gt;
&lt;li&gt;It contains additional blocks, such as domain services, repositories, factories, policies, etc&amp;hellip;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;infrastructure-layer&#34;&gt;Infrastructure layer&lt;/h2&gt;

&lt;p&gt;The infrastructure layer contains the technology-specific implementations of interfaces defined in other layers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It supports all of the three other layers in different ways, facilitating communication between the layers.&lt;/li&gt;
&lt;li&gt;It consists of everything that would still exist without the application: external libraries, database engine,
application server, messaging backend and so on.&lt;/li&gt;
&lt;li&gt;This layer can be completely replaced by another one with other technological choices without altering the system behavior.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We often declare interfaces in &lt;strong&gt;Interfaces&lt;/strong&gt;, &lt;strong&gt;Application&lt;/strong&gt; and &lt;strong&gt;Domain&lt;/strong&gt; layers and implement them in the
infrastructure Layer. A good example is the repository: interfaces are located in the domain layer but the
implementation is in the infrastructure.&lt;/p&gt;

&lt;h1 id=&#34;supple-design&#34;&gt;Supple design&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;The ultimate purpose of software is to serve users. But first, that
same software has to serve developers. [Eric Evans]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A software is not a piece code written in a one shot sprint. It is
project that evolves. A lot of developers will have to work on it, to
refactor it or add new features. Supple design is about making it
a pleasure. It should invite to change and avoid the maintenance hell.&lt;/p&gt;

&lt;p&gt;Complex and monolithic methods make hard to refactor or reuse parts of
the code. When developers are not confident about what the software
does, they start duplicating code. When this happens, iterative
process and refactoring stops.&lt;/p&gt;

&lt;p&gt;A supple design reveals the intent of the design. Side effects should
contained and easy to predict. Behaviour should be obvious making it
easy to reason about without having to investigate the implementation.&lt;/p&gt;

&lt;p&gt;There is no one-way to achieve this suppleness but Eric Evans provides
us some clues with a set of patterns. We will try to define and
illustrate them.&lt;/p&gt;

&lt;h2 id=&#34;intention-revealing-interface&#34;&gt;Intention-Revealing Interface&lt;/h2&gt;

&lt;p&gt;This concept is all about communication between developers through
code. Names of classes and methods should describe intents instead of
means. The &lt;em&gt;ubiquitous language&lt;/em&gt; must be used to define these
names. It will improves the comprehension of the code by the other
members of the team.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If a developer must consider the implementation of a component in
order to use it, the value of encapsulation is lost.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Implementation changes should not affect the expected behaviour.&lt;/p&gt;

&lt;p&gt;You can also make methods signatures clearer using meaningful Value
Objects and Entities rather than primitives and associated classes
(eg. String, long, BigDecimal, etc.). For instance, given the
following method:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;void setAddress(String address)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;How the client developer can know what the address should look like ?
Does the model allow to change the address or is it just for
initialization purpose ? Using the concept of &lt;em&gt;Intention-Revealing
Interface&lt;/em&gt; will leads us to this newer version of the method:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;void changeAddress(Address address)
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;side-effect-free-function&#34;&gt;Side-Effect-Free Function&lt;/h2&gt;

&lt;p&gt;We just explained how a good naming can improve the comprehension of a
model, but this is not always enougth. Complex methods usually call a
combination of other methods. When all these nested methods have
side-effects, it become hard to predict the overall behaviour.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In computer science side-effect means any modification in the state of
a system.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lets say we are working on a software managing teams. We had to
implement a method allowing to merged teams. So we first wrote this:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;teamOne.add(teamTwo)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ok, it takes members of the &lt;em&gt;teamTwo&lt;/em&gt; and add them to the
&lt;em&gt;teamOne&lt;/em&gt;. It does the job. Wait ! What happens to &lt;em&gt;teamTwo&lt;/em&gt; ? Has it
been modified also ? We can&amp;rsquo;t be sure whithout looking at the code of
the &lt;em&gt;add&lt;/em&gt; method because this method is not &lt;em&gt;side-effect-free&lt;/em&gt;. After
refactoring the code, here is the second version:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;Team newTeam = teamOne.mergeWith(teamTwo)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the newer version, &lt;em&gt;teamOne&lt;/em&gt; and &lt;em&gt;teamTwo&lt;/em&gt; are not
modified. Instead we create a new object, like that developers don&amp;rsquo;t
have to understand the implementation. The knowledge is capture in the
newly created team. The idea here is not to remove all the
side-effects otherwise the code will just allow you to perform
queries. But side-effects should be contained in small entities
methods and complex computation isoltated in value objects.&lt;/p&gt;

&lt;h2 id=&#34;assertion&#34;&gt;Assertion&lt;/h2&gt;

&lt;p&gt;In the previous section we talk about limiting side-effects. But some
of them will remains. The problem is that when they are implicit, it becomes
impossible to anticipate the changes on the state of the application
whithout following the execution path. Which breaks the
encapsulation. So in order to make them explicit use &lt;em&gt;assertions&lt;/em&gt;. It
can be done using method post-condition (for instance, with &lt;code&gt;@Valid&lt;/code&gt; from the
Validation JSR) or by using unit tests.&lt;/p&gt;

&lt;h2 id=&#34;going-further&#34;&gt;Going further&lt;/h2&gt;

&lt;p&gt;In this article, we described methods for making intentions explicit
using &lt;em&gt;Intention-Revealing Interface&lt;/em&gt;, &lt;em&gt;Side-Effect-Free Functions&lt;/em&gt;
and &lt;em&gt;Assertion&lt;/em&gt;. These methods allow us to communicate intents,
protecting encapsulation. This is very important if we want to keep
the advantages of using a &lt;em&gt;Object-oriented-language&lt;/em&gt;. It also allow
the client developer to focus on his design instead of having
understand all the inner workings of the API he uses.&lt;/p&gt;

&lt;p&gt;In his book &amp;laquo;Domain Driven Design&amp;raquo;, Eric Evans go further describing
three additional concepts: &lt;em&gt;Conceptual contour&lt;/em&gt;, &lt;em&gt;Standalone&lt;/em&gt;,
&lt;em&gt;Closure of Operations&lt;/em&gt;. I invite you to read his book if you want to
have all the tools to reach the suppleness.&lt;/p&gt;

&lt;h1 id=&#34;package-layout&#34;&gt;Package layout&lt;/h1&gt;

&lt;p&gt;We propose and recommend a well-defined package layout for organizing your business code, which is described in this
section.&lt;/p&gt;

&lt;h2 id=&#34;the-domain&#34;&gt;The domain&lt;/h2&gt;

&lt;h3 id=&#34;standalone-domain&#34;&gt;Standalone domain&lt;/h3&gt;

&lt;p&gt;If you want to build a reusable domain it must be located in its own project. It is the recommended way to build a domain
because it allows to reuse it across applications and doesn&amp;rsquo;t cost more than in-application domains. In that case, the
domain must be named and live in its own package, for instance:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;org.myorganization.domains.mydomain
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here the domain is named &lt;code&gt;mydomain&lt;/code&gt;. Its prefix would naturally be adapted to your organization rules.&lt;/p&gt;

&lt;h3 id=&#34;in-application-domain&#34;&gt;In-application domain&lt;/h3&gt;

&lt;p&gt;If you plan to place the domain inside to your application (which is not recommended), you can just code it along
your application packages. In that case it can stay without name and live as a subpackage of your project, for instance:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;org.myorganization.myproject.domain
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here the domain is not named, as it lives under the domain subpackage of the project. The project package would naturally
be adapted to your organization rules.&lt;/p&gt;

&lt;h3 id=&#34;package-organization&#34;&gt;Package organization&lt;/h3&gt;

&lt;p&gt;In the DDD approach, entities are grouped in sets called aggregates. Aggregates are represented as a package containing
an aggregate root and possibly additional entities. This package also contains the factory (interface and implementation),
the repository interface and the possible value objects or policies related to your aggregate.
Value objects and policies which may be used by multiple aggregates should be placed in a shared package.
Services are located in their own package.&lt;/p&gt;

&lt;p&gt;Implementations can be located in the &lt;code&gt;internal&lt;/code&gt; subpackage if they are independent of technical aspects like a third-party
library. Otherwise they must be located in the infrastructure.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-plain&#34;&gt;org.myorganization.domains.mydomain (can also be org.myorganization.myproject.domain for in-application domain)
    - model
        - myaggregate1
            - MyAggregateRoot
            - MyEntity1
            - MyEntity2
            - MyAggregateFactory
            - MyValueObject
            - MyRepository
            - MyPolicy
            - internal
                - MyPolicyImpl
                - MyAggregateFactoryImpl
        - myaggregate2
            ...

    - services
        - MyService1
        - MyService2
        - internal
            - MyService1Impl

    - shared
        - MySharedValueObject
        - MySharedPolicy1
        - internal
            - MySharedPolicy1Impl
&lt;/code&gt;&lt;/pre&gt;

&lt;div class=&#34;callout callout-warn&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
            
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;The domain should never depends on specific technical aspects. As such no infrastructure package have a place in a reusable
domain. The infrastructure for the domain should reside in the client of the domain (the application using the domain) as
it is specific to it (a same domain can be persisted very differently in different applications).&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;h2 id=&#34;the-application&#34;&gt;The application&lt;/h2&gt;

&lt;p&gt;The application layer contains application services which should be located in the application package. Implementations
can be in &lt;code&gt;internal&lt;/code&gt; subpackages if they are independent of technical aspects (third-party library). Otherwise, they must
be located in the infrastructure package.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-plain&#34;&gt;org.myorganization.myproject
    - application
        - services
            - MyService3
            - MyService4
            - internal
                - MyService3Impl

    - infrastructure
        - services
            - 3rdparty-lib
                - MyService4Lib
        - repositories
            - jpa
                - MyRepositoryJpa
&lt;/code&gt;&lt;/pre&gt;

&lt;div class=&#34;callout callout-info&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
            
                &lt;i class=&#34;fa fa-info-circle icon-2x&#34; style=&#34;line-height: 30px;&#34;&gt;&lt;/i&gt;
            
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;Note that the infrastructure also contains the implementation of domain concepts related to a specific technology, such
as implementations of repositories or of some services.&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;h2 id=&#34;the-interfaces&#34;&gt;The interfaces&lt;/h2&gt;

&lt;pre&gt;&lt;code class=&#34;language-plain&#34;&gt;org.myorganization.myproject
    - rest
        - usecase1
            - UseCase1Assembler
            - UserCase1Resource
            - UseCase1Finder
            - UseCase1Representation
        - usecase2
            ...

    - infrastructure
        - finders
            - jpa
                - UseCase1FinderJpa
                - UseCase2FinderJpa
&lt;/code&gt;&lt;/pre&gt;

&lt;h1 id=&#34;tuples&#34;&gt;Tuples&lt;/h1&gt;

&lt;p&gt;A tuple is a data structure corresponding to a sequence of immutable
objects. It&amp;rsquo;s just like an array of objects but typed and which can&amp;rsquo;t be changed,
i.e. tuples are immutable.&lt;/p&gt;

&lt;p&gt;Tuples are commonly used to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Represent a set of data without creating temparary object
that have no real meaning.&lt;/li&gt;
&lt;li&gt;Easy access to, and manipulation of, a data set.&lt;/li&gt;
&lt;li&gt;Returning mutliple values from a method.&lt;/li&gt;
&lt;li&gt;Passing multiple values to a method through a single parameter.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the Business framework, we usually see them in factories and
assemblers. As implementation for tuples we use
&lt;a href=&#34;http://javatuples.org&#34;&gt;javatuples.org&lt;/a&gt;. It provides tuple classes
from one to ten elements:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Unit&amp;lt;A&amp;gt; (1 element)
Pair&amp;lt;A,B&amp;gt; (2 elements)
Triplet&amp;lt;A,B,C&amp;gt; (3 elements)
Quartet&amp;lt;A,B,C,D&amp;gt; (4 elements)
Quintet&amp;lt;A,B,C,D,E&amp;gt; (5 elements)
Sextet&amp;lt;A,B,C,D,E,F&amp;gt; (6 elements)
Septet&amp;lt;A,B,C,D,E,F,G&amp;gt; (7 elements)
Octet&amp;lt;A,B,C,D,E,F,G,H&amp;gt; (8 elements)
Ennead&amp;lt;A,B,C,D,E,F,G,H,I&amp;gt; (9 elements)
Decade&amp;lt;A,B,C,D,E,F,G,H,I,J&amp;gt; (10 elements)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Plus a couple of very common 2-element tuple classes equivalent to
Pair, just for the sake of code semantics:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;KeyValue&amp;lt;A,B&amp;gt;
LabelValue&amp;lt;A,B&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;All the tuple classes respect the following properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Typesafe&lt;/li&gt;
&lt;li&gt;Immutable&lt;/li&gt;
&lt;li&gt;Iterable&lt;/li&gt;
&lt;li&gt;Serializable&lt;/li&gt;
&lt;li&gt;Comparable (implements Comparable)&lt;/li&gt;
&lt;li&gt;Implementing equals(&amp;hellip;) and hashCode()&lt;/li&gt;
&lt;li&gt;Implementing toString()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Usage:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;Pair&amp;lt;Integer, String&amp;gt; pair = new Pair&amp;lt;Integer, String&amp;gt;(10, &amp;quot;foo&amp;quot;);
Integer value0 = pair.getValue0();
String value1 = pair.getValue1();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We also provide an helper class to create tuples.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;Pair&amp;lt;Integer, String&amp;gt; pair = Tuples.create(10, &amp;quot;foo&amp;quot;);
Tuple tuple = Tuples.create(10, &amp;quot;foo&amp;quot;, new Customer());
&lt;/code&gt;&lt;/pre&gt;

&lt;h1 id=&#34;qualified-injection&#34;&gt;Qualified injection&lt;/h1&gt;

&lt;p&gt;The Business framework supports the use of qualifiers defined by the JSR 330. This feature provides the ability to
create multiple implementations for the same interface. This is useful when you have multiple algorithms or implementation
technologies for an interface.&lt;/p&gt;

&lt;h2 id=&#34;an-example-with-policies&#34;&gt;An example with policies&lt;/h2&gt;

&lt;p&gt;This pattern can be used in various situations but proves itself very useful in the case of DDD policies. To leverage it,
define a Policy interface as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;@Policy
public interface TaxesPolicy {

    Integer computeTaxes(Order order);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then define an implementation annotated by a qualifier. The annotation &lt;code&gt;@Named&lt;/code&gt; allows to qualify an implementation with
a String.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;@Named(&amp;quot;FR&amp;quot;)
public class FranceTaxesPolicy implements TaxesService {

    Integer computeTaxes(Order order){
        ...
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Finally you can inject it as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;@Inject @Named(&amp;quot;FR&amp;quot;)
private TaxesPolicy frenchTaxes;

@Inject @Named(&amp;quot;UK&amp;quot;)
private TaxesPolicy ukTaxes;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or you can select it dynamically using the &lt;code&gt;&lt;a data-toggle=&#34;tooltip&#34; data-html=&#34;true&#34; title=&#39;org.&amp;amp;#8203;seedstack.&amp;amp;#8203;business.&amp;amp;#8203;domain.&amp;amp;#8203;DomainRegistry&#39; href=&#39;http://seedstack.org/javadoc/org/seedstack/business/domain/DomainRegistry.html&#39;&gt;DomainRegistry&lt;/a&gt;&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;TaxesPolicy userTaxesPolicy = domainRegistry.getPolicy(TaxesPolicy.class, userLocal);
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;use-a-custom-qualifier&#34;&gt;Use a custom qualifier&lt;/h2&gt;

&lt;p&gt;If a qualifier is often used, you can create a custom qualifier annotation as follows.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.inject.Qualifier;

@Qualifier
@Target({ TYPE, METHOD, FIELD })
@Retention(RUNTIME)
public @interface France {
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then you use it like the &lt;code&gt;@Named&lt;/code&gt; annotation.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;@Inject @France
private TaxesService taxesService;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;list-of-concepts-supporting-qualifiers&#34;&gt;List of concepts supporting qualifiers&lt;/h2&gt;

&lt;p&gt;Qualified injection can be used on these concepts out-of-the-box:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Assembler,&lt;/li&gt;
&lt;li&gt;Repository,&lt;/li&gt;
&lt;li&gt;Factory,&lt;/li&gt;
&lt;li&gt;Policy,&lt;/li&gt;
&lt;li&gt;Service,&lt;/li&gt;
&lt;li&gt;Finder.&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>Domain events</title>
      <link>http://beta.seedstack.org/docs/business/manual/events/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>http://beta.seedstack.org/docs/business/manual/events/</guid>
      <description>

&lt;p&gt;Seed Business Framework contains an API to manage domain events. The &lt;code&gt;EventService&lt;/code&gt; service is used to fire events. Events
must be immutable and extend &lt;code&gt;DomainEvent&lt;/code&gt; (which extends &lt;code&gt;BaseValueObject&lt;/code&gt;).&lt;/p&gt;

&lt;div class=&#34;callout callout-info&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
            
                &lt;i class=&#34;fa fa-info-circle icon-2x&#34; style=&#34;line-height: 30px;&#34;&gt;&lt;/i&gt;
            
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;&lt;strong&gt;Notice:&lt;/strong&gt; Events can also be defined by implementing the &lt;code&gt;Event&lt;/code&gt; interface. Nevertheless, this requires to
implement &lt;code&gt;equals()&lt;/code&gt; and &lt;code&gt;hashCode()&lt;/code&gt; methods. Otherwise event test fixtures and call cycle detection will not work.&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;For instance this event&amp;hellip;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class MyEvent extends DomainEvent {
	...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&amp;hellip;could be fired as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@Inject
private EventService

@Inject
private MyEventFactory eventFactory;

eventService.fire(eventFactory.createMyEvent());
&lt;/code&gt;&lt;/pre&gt;

&lt;h1 id=&#34;handling-events&#34;&gt;Handling events&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;EventHandlers&lt;/strong&gt; must implement &lt;code&gt;EventHandler&lt;/code&gt; in order to receive fired events:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class MyHandler implements EventHandler&amp;lt;MyEvent&amp;gt; {
    @Override
    public void handle(MyEvent event) {
        ...
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;MyHandler&lt;/code&gt; implements &lt;code&gt;EventHandler&amp;lt;MyEvent&amp;gt;&lt;/code&gt; which means it listens to events of &lt;code&gt;MyEvent&lt;/code&gt; type.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;handle&lt;/code&gt; method has to be implemented to define the handler&amp;rsquo;s behaviour.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;synchronous-behavior&#34;&gt;Synchronous behavior&lt;/h2&gt;

&lt;p&gt;Events are fired synchronously and &lt;strong&gt;belong to current transaction&lt;/strong&gt;. Depending on &lt;code&gt;Exception&lt;/code&gt; management, a fired exception might rollback the transaction.&lt;/p&gt;

&lt;h2 id=&#34;event-inheritance&#34;&gt;Event inheritance&lt;/h2&gt;

&lt;p&gt;If a triggered event is assignable to &lt;code&gt;MyEvent&lt;/code&gt; (by inheritance), it will also be handled - consequently, a handler that &lt;code&gt;implements EventHandler&amp;lt;DomainEvent&amp;gt;&lt;/code&gt; will be called on any event implementing &lt;code&gt;DomainEvent&lt;/code&gt;.&lt;/p&gt;

&lt;h1 id=&#34;testing-events&#34;&gt;Testing events&lt;/h1&gt;

&lt;p&gt;The &lt;strong&gt;seed-business-core-test&lt;/strong&gt; module provides an &lt;code&gt;EventFixture&lt;/code&gt; class for integration tests on events.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Test that a given event was handled by an expected &lt;code&gt;EventHandler&lt;/code&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;@Inject
private EventFixture fixture;
...
fixture.given(eventFactory.createMyEvent())
    .whenFired()
    .wasHandledBy(MyHandler.class);
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;Test that a given event was handled by exactly a provided list of &lt;code&gt;EventHandler&lt;/code&gt;s:&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;@Inject
private EventFixture fixture;
...
fixture.given(eventFactory.createMyEvent())
    .whenFired()
    .wasHandledExactlyBy(MyHandler.class, MyHandler2.class);
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;Test that a given event was not handled by an expected  &lt;code&gt;EventHandler&lt;/code&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;@Inject
private EventFixture fixture;
...
fixture.given(eventFactory.createMyEvent())
    .whenFired()
    .wasNotHandledBy(MyHandler3.class);
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;Test that a given event was generated from an expected &lt;code&gt;method()&lt;/code&gt; with appropriate &lt;em&gt;parameters&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;@Inject
private EventFixture fixture;
...
MyEvent myEvent = eventFactory.createMyEvent(SOME_EVENT_PARAM);
fixtures.given(MyService.class)
     .whenCalled(&amp;quot;doSomething&amp;quot;, SOME_METHOD_PARAM)
     .eventWasHandledBy(myEvent, MyHandler.class);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Test if &lt;code&gt;MyHandler&lt;/code&gt; handler received &lt;code&gt;myEvent&lt;/code&gt; event when &lt;code&gt;doSomething()&lt;/code&gt; method of &lt;code&gt;MyService&lt;/code&gt; is called.&lt;/p&gt;

&lt;h1 id=&#34;provided-events&#34;&gt;Provided events&lt;/h1&gt;

&lt;h2 id=&#34;aggregate-events&#34;&gt;Aggregate events&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Seed Business Framework&lt;/strong&gt; provides following events:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;AggregateReadEvent&lt;/code&gt;: triggered when reading an aggregate - eg. repository &lt;code&gt;load()&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;&lt;code&gt;AggregatePersistedEvent&lt;/code&gt;: triggered when persisting an aggregate - eg. repository &lt;code&gt;save()&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;&lt;code&gt;AggregateDeletedEvent&lt;/code&gt;: triggered when deleting an aggregate - eg. repository &lt;code&gt;delete()&lt;/code&gt; method&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Above behaviour is defined by method annotations, respectively: &lt;code&gt;@Read&lt;/code&gt;, &lt;code&gt;@Persist&lt;/code&gt; and &lt;code&gt;@Delete&lt;/code&gt;.
These annotations are only intercepted (and functional) within a repository class implementing &lt;code&gt;GenericRepository&lt;/code&gt; (read &lt;a href=&#34;#!/business-doc/hands-on-domain/repository&#34;&gt;more&lt;/a&gt; on repositories).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This mechanism is disabled by default.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To enable this feature, use following property:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[org.seedstack.business.event]
domain.watch=true
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Handle aggregate read events:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define a custom read method:&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;public interface MyRepository extends GenericRepository&amp;lt;AgregateRoot, AggregateIdKey&amp;gt; {
    ...
    @Read
    AgregateRoot loadByName(String name);
    ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;GenericRepository&lt;/code&gt; methods (load, delete, persist, save) are already annotated with appropriate annotations.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;The repository reading method is called, triggering an &lt;code&gt;AggregateReadEvent&lt;/code&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;// fire an AggregateReadEvent for the AgregateRoot
productRepository.loadByName(aggregateName);
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;MyHandler&lt;/code&gt; handles the triggered &lt;code&gt;AggregateReadEvent&lt;/code&gt; event:&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;// handle an AggregateReadEvent
class MyHandler implements EventHandler&amp;lt;AggregateReadEvent&amp;gt; {
    public void handle(BaseRepositoryEvent event) {
        ...
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;div class=&#34;callout callout-info&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
            
                &lt;i class=&#34;fa fa-info-circle icon-2x&#34; style=&#34;line-height: 30px;&#34;&gt;&lt;/i&gt;
            
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;&lt;strong&gt;IMPORTANT:&lt;/strong&gt; Above handler receives all &lt;code&gt;AggregateReadEvent&lt;/code&gt; from any repository
&lt;code&gt;@Read&lt;/code&gt; annotated method. Since &lt;code&gt;AggregateReadEvent&lt;/code&gt; events contain the aggregate root
class and a context with the called method and its arguments, the handler behaviour can be defined accordingly.&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;hr /&gt;

&lt;p&gt;Since all &amp;laquo;aggregate events&amp;raquo; extend &lt;code&gt;BaseAggregateEvent&lt;/code&gt;, it is possible to intercept them all in one handler:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// handle an BaseAggregateEvent
class MyHandler implements EventHandler&amp;lt;BaseAggregateEvent&amp;gt; {
    public void handle(BaseRepositoryEvent event) {
        // if &amp;quot;event&amp;quot; depends on Product aggregate
        if (Product.class.isAssignableFrom(event.getAggregateRoot())) {
            ...
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;blockquote&gt;
&lt;p&gt;See above &lt;a href=&#34;#!/business-doc/hands-on-domain/events#handling-events&#34;&gt;handlers&lt;/a&gt; documentation for more detail about their generic behaviour.&lt;/p&gt;
&lt;/blockquote&gt;
</description>
    </item>
    
    <item>
      <title>Domain model</title>
      <link>http://beta.seedstack.org/docs/business/manual/model/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>http://beta.seedstack.org/docs/business/manual/model/</guid>
      <description>

&lt;p&gt;The domain model is expressed through three patterns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Entity&lt;/strong&gt; which is an object defined primarily by its identity.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Value Object&lt;/strong&gt; which is an object with no conceptual identity and solely defined by is attributes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Service&lt;/strong&gt; which hold domain logic that doesn&amp;rsquo;t clearly belong to an entity nor a value object.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&#34;entities:48ee0a3512ab0b5afa0cb3ef9950802a&#34;&gt;Entities&lt;/h1&gt;

&lt;p&gt;Entities are used to represent a domain concept which has an identity.&lt;/p&gt;

&lt;p&gt;Often DDD beginners have a tendency to focus more on data than on the software. This often leads in all the concepts of
the &amp;laquo;domain&amp;raquo; being coded as entities. Specially, in anemic entities using only getters and setters. Using them is not
wrong but it&amp;rsquo;s not enough to hold the insights of a domain. That&amp;rsquo;s why designing entities should be taken very carefully.&lt;/p&gt;

&lt;p&gt;The entity concept should be used for an element of your domain when you take care of its identity. An identity must be
unique and immutable. Even it an object can change during its lifetime, its identity must stay untouched. If this is not
true for your element, you should consider using a &lt;a href=&#34;#value-objects&#34;&gt;value object&lt;/a&gt; instead.&lt;/p&gt;

&lt;p&gt;When designing an entity, the critical part is to determine what is the identity and how to create it. An identity can be
hold by a set of properties. In this case, a value object can be well fitted to guaranty the identity consistency and immutability.&lt;/p&gt;

&lt;p&gt;There are different kind of creation strategies for identities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The client can pass values handling himself the uniqueness.&lt;/li&gt;
&lt;li&gt;The application can generate the identity using an algorithm.&lt;/li&gt;
&lt;li&gt;The application can rely on an external identity generator, like a database sequence.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first case is easily handled using factories. The other cases, can be usually more complicated, but the Business
framework provides an API for them (see &lt;a href=&#34;../lifecycle#identity-generation&#34;&gt;identity generation&lt;/a&gt;).&lt;/p&gt;

&lt;h2 id=&#34;declaration:48ee0a3512ab0b5afa0cb3ef9950802a&#34;&gt;Declaration&lt;/h2&gt;

&lt;p&gt;To create an Entity using the Business framework you have three choices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Extend the &lt;code&gt;BaseEntity&lt;/code&gt; class. The &lt;code&gt;equals()&lt;/code&gt;, &lt;code&gt;hashCode()&lt;/code&gt; and &lt;code&gt;compareTo()&lt;/code&gt; methods will be provided out-of-the-box. You
must implement the &lt;code&gt;getEntityId()&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;Implement the &lt;code&gt;Entity&lt;/code&gt; interface. You must implement the &lt;code&gt;equals()&lt;/code&gt;, &lt;code&gt;hashCode()&lt;/code&gt;, &lt;code&gt;compareTo()&lt;/code&gt; and &lt;code&gt;getEntityId()&lt;/code&gt;
methods in this case.&lt;/li&gt;
&lt;li&gt;Simply annotate any class with the &lt;code&gt;@DomainEntity&lt;/code&gt; annotation. In this case, you won&amp;rsquo;t be able to use helpers and
tools from the framework.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With the two first options (base class and interface), you have to provide a generic parameter with the type of the
Entity identifier.&lt;/p&gt;

&lt;h2 id=&#34;example:48ee0a3512ab0b5afa0cb3ef9950802a&#34;&gt;Example&lt;/h2&gt;

&lt;p&gt;Consider the following example in which a &lt;code&gt;Customer&lt;/code&gt; Entity is identified by an e-mail of String type.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class Customer extends BaseEntity&amp;lt;String&amp;gt; {
    private String email;
    private Address address;
    private List&amp;lt;Order&amp;gt; orders;

    /* Package protected constructor */
    Customer (String identity, Address address) {
        this.email = identity;
        ...
    }

    @Override
    public String getEntityId() {
        return this.email;
    }

    /* Meaningful methods */
    public void changeAddress(Address newAddress) { ... }

    /* Getters */
    public Address getAddress() { ... }
    public String getEmail() { ... }
    public List&amp;lt;Order&amp;gt; getOrders() { ... }
    
    /* Try to avoid setters as they allow to alter the internal state of the entity */
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h1 id=&#34;value-objects:48ee0a3512ab0b5afa0cb3ef9950802a&#34;&gt;Value-Objects&lt;/h1&gt;

&lt;p&gt;An object that don&amp;rsquo;t have a conceptual identity but is just describing some characteristics of a thing is called a
Value Object. Because the most visible objects in a model are usually Entities, there is a natural tendency to assign
an identity to every domain object. But this tendency should be refrained. Here are the main characteristics of a
Value Object:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It measures, quantifies or describes a thing in the domain.&lt;/li&gt;
&lt;li&gt;It is immutable, meaning that its state cannot be changed after creation.&lt;/li&gt;
&lt;li&gt;It describes a conceptual whole. Its attributes are related to each other and are all participating to the description.
This means that all the required values should be assigned upon creation (i.e. in the constructor).&lt;/li&gt;
&lt;li&gt;It is comparable to others using value equality.&lt;/li&gt;
&lt;li&gt;Its behavior is side-effect free.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;declaration-1:48ee0a3512ab0b5afa0cb3ef9950802a&#34;&gt;Declaration&lt;/h2&gt;

&lt;p&gt;To create a Value Object using the Business framework you have three choices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Extend the &lt;code&gt;BaseValueObject&lt;/code&gt; class. In this case, the &lt;code&gt;equals()&lt;/code&gt;, &lt;code&gt;hashCode()&lt;/code&gt; and &lt;code&gt;compareTo()&lt;/code&gt; methods will be
provided out-of-the-box.&lt;/li&gt;
&lt;li&gt;Implement the &lt;code&gt;ValueObject&lt;/code&gt; interface. You must implement the &lt;code&gt;equals()&lt;/code&gt;, &lt;code&gt;hashCode()&lt;/code&gt; and &lt;code&gt;compareTo()&lt;/code&gt; methods in
this case.&lt;/li&gt;
&lt;li&gt;Simply annotate any class with the &lt;code&gt;@DomainValueObject&lt;/code&gt; annotation. In this case, you won&amp;rsquo;t be able to use helpers and
tools from the framework.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;example-1:48ee0a3512ab0b5afa0cb3ef9950802a&#34;&gt;Example&lt;/h2&gt;

&lt;p&gt;Cars are identified by a &amp;laquo;Vehicle Identification Number&amp;raquo; (VIN). See this &lt;a href=&#34;http://en.wikipedia.org/wiki/Vehicle_Identification_Number#Components_of_the_VIN&#34;&gt;wikipedia article&lt;/a&gt;
for the conceptual definition. To simplify our example, we will just consider the VIN as composed respectively of 3 parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;3 DIGITS : World Manufacturer Identifier&lt;/li&gt;
&lt;li&gt;6 DIGITS : Vehicle descriptor section&lt;/li&gt;
&lt;li&gt;8 DIGITS : Vehicle identifier section&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is a possible implementation of the VIN class:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;package org.mycompany.myapp.shared.domain.after.sales.vehicle;

import org.seedstack.business.domain.BaseValueObject;

public class VIN extends BaseValueObject {
    private final String worldManufacturerIdentifier;
    private final String vehicleDescriptorSection;
    private final String vehicleIdentifierSection;

    public vehicleIdentificationNumber(
            String worldManufacturerIdentifier,
            String vehicleDescriptorSection,
            String vehicleIdentifierSection) {
        this.worldManufacturerIdentifier = worldManufacturerIdentifier;
        this.vehicleDescriptorSection = vehicleDescriptorSection;
        this.vehicleIdentifierSection = vehicleIdentifierSection;
    }

    public vehicleIdentificationNumber(String vin) {
        this.worldManufacturerIdentifier = vin.substring(0,3);
        this.vehicleDescriptorSection = vin.substring(3,9);
        this.vehicleIdentifierSection = vin.substring(9,17);
    }

    /* Getters */
    public String getWorldManufacturerIdentifier() { ... }
    public String getVehicleDescriptorSection() { ... }
    public String getVehicleIdentifierSection() { ... }

    @Override
    public String toString(){
        return worldManufacturerIdentifier + vehicleDescriptorSection
                + vehicleIdentifierSection;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;usage-as-identifiers:48ee0a3512ab0b5afa0cb3ef9950802a&#34;&gt;Usage as identifiers&lt;/h2&gt;

&lt;p&gt;Value Object can also be used to represent complex identifiers for entities. For instance, you can use the VIN class defined
in the example above to identity a &lt;code&gt;Vehicle&lt;/code&gt; class. You can also add meaning and behavior to a simple value by embedding
it into a Value Object:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;package org.mycompany.myapp.domain.customer;

import javax.persistence.Embeddable;
import org.seedstack.business.domain.BaseValueObject;

public class CustomerId extends BaseValueObject {
    private String value;

    public CustomerId(String customerId) {
        this.value = customerId;
    }
    public String getValue() {
        return value;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this example, the &lt;code&gt;CustomerId&lt;/code&gt; Value Object add meaning to the plain string. You won&amp;rsquo;t manipulate a String anymore
in your code but a CustomerId, with its own type. This type can evolve later to provide additional behavior or to
be adapt its internal structure.&lt;/p&gt;

&lt;h1 id=&#34;services:48ee0a3512ab0b5afa0cb3ef9950802a&#34;&gt;Services&lt;/h1&gt;

&lt;p&gt;Services are stateless objects that implement logic which doesn&amp;rsquo;t fit in the aggregates.&lt;/p&gt;

&lt;p&gt;Services can be found in various locations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The domain, where services contain pure business logic. Naming should come from the &lt;a href=&#39;http://beta.seedstack.org/glossary#ubiquitous-language&#39;&gt;ubiquitous language&lt;/a&gt; or
be introduced into it if necessary. Parameters and return values should be domain objects. Example: a bank account transfer service.&lt;/li&gt;
&lt;li&gt;The infrastructure, where services deal with specific technological aspects. Example: a notification sending service.&lt;/li&gt;
&lt;li&gt;The application, where services contain coordination logic between other services and are more tied to a specific use-case.
These services are often the ideal place to begin and end a transaction.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&#34;callout callout-info&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
            
                &lt;i class=&#34;fa fa-info-circle icon-2x&#34; style=&#34;line-height: 30px;&#34;&gt;&lt;/i&gt;
            
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;A good service is always stateless. That doesn&amp;rsquo;t mean that a service cannot change the global state of the application
(that is, it may have side effects), but it should never hold a state of its own that could affect its behavior.&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;h2 id=&#34;declaration-2:48ee0a3512ab0b5afa0cb3ef9950802a&#34;&gt;Declaration&lt;/h2&gt;

&lt;p&gt;Creating a Service with the Business Framework, consists in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating a service interface, annotated with the &lt;code&gt;@Service&lt;/code&gt; annotation. Business Framework Services are POJOs, there is no mandated super interface.&lt;/li&gt;
&lt;li&gt;Creating a service implementation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;example-2:48ee0a3512ab0b5afa0cb3ef9950802a&#34;&gt;Example&lt;/h2&gt;

&lt;p&gt;Let&amp;rsquo;s consider a bank account transfer, which is a service belonging to the domain.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From the domain of a &lt;em&gt;bank account management&lt;/em&gt; perspective, the transfer consists of an amount debited on an
account which is credited on another account.&lt;/li&gt;
&lt;li&gt;The inherent logic of the transfer does not belong to any of the accounts but to a service of the Domain.
It&amp;rsquo;s implementation can be rather simple or complex depending on the rules applying to the process (currency exchange
rate, transfer authorisation between countries, amount on originating account, etc&amp;hellip;).&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;@Service
public interface AccountTransferService {

    public AccountTransferReport transferMoney(Account toBeDebited, Account toBeCredited,
            Amount transferAmount);

}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;div class=&#34;callout callout-info&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
            
                &lt;i class=&#34;fa fa-info-circle icon-2x&#34; style=&#34;line-height: 30px;&#34;&gt;&lt;/i&gt;
            
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;Avoid services named like &amp;laquo;AccountManagement&amp;raquo; as they tend to become the place to handle all behaviour for a (sub)domain
instead of clearly specifying the intent and responsibility.&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Finders</title>
      <link>http://beta.seedstack.org/docs/business/manual/finders/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>http://beta.seedstack.org/docs/business/manual/finders/</guid>
      <description>

&lt;p&gt;A Finder is an object that queries the domain to get lists of domain objects optionally matching some criteria. A Finder
queries the domain but returns representations.&lt;/p&gt;

&lt;p&gt;Creating a Finder with the Business Framework, consists in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating a finder interface,&lt;/li&gt;
&lt;li&gt;Creating a finder implementation, located in the infrastructure as it depends upon a persistence technology.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Business Framework Finders are POJOs, there is no mandated interface. Just annotate your finder interface with the
&lt;code&gt;@Finder&lt;/code&gt; annotation.&lt;/p&gt;

&lt;h1 id=&#34;interface:d921404f3054cf1d9eb23d31757af3f0&#34;&gt;Interface&lt;/h1&gt;

&lt;p&gt;Create the interface for your finder. A finder is annotated with &lt;code&gt;@Finder&lt;/code&gt; and is declared as read only.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@Finder
public interface CustomerFinder {

    List&amp;lt;CustomerRepresentation&amp;gt; findAllCustomers();

}
&lt;/code&gt;&lt;/pre&gt;

&lt;h1 id=&#34;implementation:d921404f3054cf1d9eb23d31757af3f0&#34;&gt;Implementation&lt;/h1&gt;

&lt;p&gt;In the infrastructure layer, provide the implementation (here with JPA):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class CustomerJpaFinder implements CustomerFinder {

    @Inject
    private FluentAssembler assembler;

    @Override
    public List&amp;lt;CustomerRepresentation&amp;gt; findAllCustomer() {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery&amp;lt;Customer&amp;gt; q = cb.createQuery(Customer.class);
        q.select(q.from(Customer.class));

        return fluentAssembler
                   .assemble(entityManager.createQuery(q).getResultList())
                   .to(CustomerRepresentation.class);
    }

}
&lt;/code&gt;&lt;/pre&gt;
</description>
    </item>
    
    <item>
      <title>Pagination</title>
      <link>http://beta.seedstack.org/docs/business/manual/pagination/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>http://beta.seedstack.org/docs/business/manual/pagination/</guid>
      <description>

&lt;p&gt;The data presentation API helps developers presenting the data from their domain to external actors. Those are
generally remote REST clients like a browser, web services consumers or RPC/RMI clients. The data presentation API supports
patterns often used in data restitution: pagination, infinite-scroll, random chunk access.&lt;/p&gt;

&lt;h1 id=&#34;concepts&#34;&gt;Concepts&lt;/h1&gt;

&lt;p&gt;The following describes the API. We&amp;rsquo;ll specialize some concepts already presented like the finder.
We focus the creation of this API on solving the problem of returning portion result out of very big one.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;http://beta.seedstack.org/puml/business/business-api-interfaces-finder.puml.png&#34; alt=&#34;domain aggregates&#34; /&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;Range&lt;/strong&gt; is just a class that represents an &lt;strong&gt;offset&lt;/strong&gt; and a &lt;strong&gt;size&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;Result&lt;/strong&gt; symbolises a canonical representation of the ranged return of a request made from a persistence. It holds:

&lt;ul&gt;
&lt;li&gt;the list that contains the result itself,&lt;/li&gt;
&lt;li&gt;the actual list size,&lt;/li&gt;
&lt;li&gt;the full size of the whole request.&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;View&lt;/strong&gt; represents a viewpoint of an already existing list. Its focus is the restitution of a portion of the given
list.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;RangeFinder&amp;lt;Item,Criteria&amp;gt;&lt;/code&gt; is a High level interfaces for finder that sum up the following assertion: Given a
&lt;strong&gt;Range&lt;/strong&gt; and a &lt;strong&gt;Criteria&lt;/strong&gt; please find the &lt;strong&gt;Result&lt;/strong&gt; for the Item type.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&#34;callout callout-info&#34;&gt;
Notice that &lt;strong&gt;Criteria&lt;/strong&gt; here, is not a type, but a generic to be substituted when subclassing. For
instance a &lt;code&gt;Map&amp;lt;String, Object&amp;gt;&lt;/code&gt; or a custom class.
&lt;/div&gt;

&lt;p&gt;In order to &lt;strong&gt;move out the complex computation (page, chunk, ..) away from the RangeFinder&lt;/strong&gt; we only provide him a
Range. Its first and only objective is to fetch data from persistence according to a given criteria. View management is
completely orthogonal to the restitution a given list from a criteria.&lt;/p&gt;

&lt;p&gt;The actual &lt;em&gt;Range&lt;/em&gt; can be greater than the needed portion. This way, the result can be reused according that the Criteria
and the result size has not changed.&lt;/p&gt;

&lt;h1 id=&#34;example&#34;&gt;Example&lt;/h1&gt;

&lt;p&gt;Create a finder by extending the &lt;code&gt;BaseRangeFinder&lt;/code&gt; class. This abstract class needs you to implement two methods.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;computeResultList()&lt;/code&gt; method which should return the list of matching entity with the expected range.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;computeFullRequestSize()&lt;/code&gt; method should return the size of complete list matching the criteria.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For instance create the following interface:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; @Finder
 public interface Dto1Finder extends RangeFinder&amp;lt;Dto1, Map&amp;lt;String, Object&amp;gt;&amp;gt; {

     PaginatedView&amp;lt;ProductRepresentation&amp;gt; findItemByQuery(Page page, String searchQuery);
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Implement it as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@JpaUnit(&amp;quot;my-unit&amp;quot;)
@Transactional
public class Dto1SimpleJpaFinder extends BaseRangeFinder&amp;lt;Dto1, String&amp;gt;
    implements Dto1Finder {

    @Inject
    private EntityManager entityManager;

    @Override
    public PaginatedView&amp;lt;ProductRepresentation&amp;gt; findItemByQuery(Page page, String query) {
        Range range = Range.rangeFromPageInfo(page.getIndex(), page.getCapacity());
        Result&amp;lt;Dto1&amp;gt; result = find(range, query);
        return new PaginatedView&amp;lt;Dto1&amp;gt;(result, page);
    }

    @Override
    protected List&amp;lt;Dto1&amp;gt; computeResultList(Range range , String criteria) {
        CriteriaQuery&amp;lt;AggRoot1&amp;gt; query = getAggRoot1CriteriaQuery(criteria);
        List&amp;lt;AggRoot1&amp;gt; resultList = entityManager.createQuery(query)
                .setFirstResult((new BigDecimal(range.getOffset()).intValue()))
                .setMaxResults(new BigDecimal(range.getSize()).intValue())
                .getResultList();

        return assemblers.assembleDtoFromAggregate(resultList);
    }

    @Override
    protected long computeFullRequestSize(String criteria) {
        CriteriaQuery&amp;lt;Long&amp;gt; query = getAggRoot1CountCriteriaQuery(criteria);
        return entityManager.createQuery(query).getSingleResult();
    }

    ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then, inject the finder with its interface and use it as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;@Inject
Dto1Finder dto1Finder;

@GET
@Rel(&amp;quot;search&amp;quot;)
@Produces(MediaType.APPLICATION_JSON)
public Response list(@QueryParam(&amp;quot;q&amp;quot;) String searchQuery,
                     @DefaultValue(&amp;quot;0&amp;quot;) @QueryParam(&amp;quot;pageIndex&amp;quot;) Long pageIndex,
                     @DefaultValue(&amp;quot;10&amp;quot;) @QueryParam(&amp;quot;pageSize&amp;quot;) Integer pageSize) {

    // Call the finder with the requested page
    Page page = new Page(pageIndex, pageSize);
    PaginatedView&amp;lt;Dto1&amp;gt; view = dto1Finder.findItemByQuery(page, searchQuery);

    // Create an HAL representation with the page and the total number of elements
    Dto1sRepresentation representation = new Dto1sRepresentation(page,
        view.getResultSize());

    // Add the list of item to the representation
    representation.embedded(&amp;quot;items&amp;quot;, view.getView());

    // If a next page is available add to link to it
    if (view.hasNext()) {
        Page next = view.next();

        representation.link(&amp;quot;next&amp;quot;, relRegistry.uri(&amp;quot;search&amp;quot;)
                .set(&amp;quot;pageIndex&amp;quot;, next.getIndex())
                .set(&amp;quot;pageSize&amp;quot;, next.getCapacity()).expand());
    }

    // If a previous page is available add to link to it
    if (view.hasPrev()) {
        Page prev = view.prev();

        representation.link(&amp;quot;prev&amp;quot;, relRegistry.uri(&amp;quot;search&amp;quot;)
                .set(&amp;quot;pageIndex&amp;quot;, prev.getIndex())
                .set(&amp;quot;pageSize&amp;quot;, prev.getCapacity()).expand());
    }

    return Response.ok(representation).build();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;div class=&#34;callout callout-info&#34;&gt;
    &lt;div style=&#34;display:table; width: 100%&#34;&gt;
        &lt;div style=&#34;display:table-cell; width: 50px; vertical-align: middle;&#34;&gt;
            
            
                &lt;i class=&#34;fa fa-info-circle icon-2x&#34; style=&#34;line-height: 30px;&#34;&gt;&lt;/i&gt;
            
            
            
        &lt;/div&gt;
        &lt;div style=&#34;display:table-cell; vertical-align: middle;&#34;&gt;
            &lt;p&gt;The following example uses the HAL media type. For more information about it, read the documentation about
&lt;a href=&#34;http://beta.seedstack.org/docs/seed/manual/rest/restful-api/&#34;&gt;hypermedia&lt;/a&gt;.&lt;/p&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Policies</title>
      <link>http://beta.seedstack.org/docs/business/manual/policies/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>http://beta.seedstack.org/docs/business/manual/policies/</guid>
      <description>

&lt;p&gt;A policy is a variant of the Strategy pattern to encapsulate one or more business rules.&lt;/p&gt;

&lt;h1 id=&#34;definition:e02385777766c7d11797dbe33d94a45d&#34;&gt;Definition&lt;/h1&gt;

&lt;p&gt;Creating a Policy with the Business Framework, consists in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating a policy interface in the domain,&lt;/li&gt;
&lt;li&gt;Creating a policy implementation, located either in the domain or in the infrastructure if it depends upon any
specific technology.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Business Framework Policies are POJOs, there is no mandated interface. Just annotate your policy interface with the
&lt;code&gt;@DomainPolicy&lt;/code&gt; annotation.&lt;/p&gt;

&lt;h1 id=&#34;usage:e02385777766c7d11797dbe33d94a45d&#34;&gt;Usage&lt;/h1&gt;

&lt;p&gt;To use a policy, simply inject it in your code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@Inject
MyPolicy myPolicy;
&lt;/code&gt;&lt;/pre&gt;

&lt;h1 id=&#34;example:e02385777766c7d11797dbe33d94a45d&#34;&gt;Example&lt;/h1&gt;

&lt;p&gt;Let&amp;rsquo;s consider a bonus policy for car sellers according to the sales they&amp;rsquo;ve done.
This policy could allow the sellers - and their manager - to know what the amount of their bonus is at a given time in
the current month.&lt;/p&gt;

&lt;h2 id=&#34;bonuspolicy-interface:e02385777766c7d11797dbe33d94a45d&#34;&gt;BonusPolicy interface&lt;/h2&gt;

&lt;p&gt;In &lt;code&gt;org.mycompany.myapp.policy&lt;/code&gt; package&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;package org.mycompany.myapp.policy;

import org.mycompany.myapp.domain.model.carsold.CarSold;
import org.mycompany.myapp.domain.shared.vo.Price;
import org.seedstack.business.domain.DomainPolicy;

import java.util.List;

@DomainPolicy
public interface BonusPolicy {
	Price computeBonus(List&amp;lt;CarSold&amp;gt; carSolds);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;bonuspolicy-implementation:e02385777766c7d11797dbe33d94a45d&#34;&gt;BonusPolicy implementation&lt;/h2&gt;

&lt;p&gt;Below is an implementation example.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;org.mycompany.myapp.policy.internal&lt;/code&gt; package&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;package org.mycompany.myapp.policy.internal;

import ...

public class BonusPolicyInternal implements BonusPolicy{

    @Override
	public Price computeBonus(List&amp;lt;CarSold&amp;gt; soldCarsList) {
        ...
		return finalBonus;
	}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;bonuspolicy-usage:e02385777766c7d11797dbe33d94a45d&#34;&gt;BonusPolicy usage&lt;/h2&gt;

&lt;p&gt;Wherever the policy is used to compute the bonus of a particular employee, just retrieve the cars sold by this employee
(through the sold cars repository) and call the policy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Interface&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;package org.mycompany.myapp.application.service;

import org.mycompany.myapp.domain.model.employee.Employee;
import org.seedstack.business.Service;

@Service
public interface SalesBonusService {
    double calculateCurrentBonusFor(Employee employee);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;Implementation&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;package org.mycompany.myapp.application.service.internal;

import ...

public class SalesBonusServiceInternal implements SalesBonusService {
	
	@Inject
	CarSoldRepository carSoldRepository;
	
	@Inject
	CarForSaleRepository carForSaleRepository;
	
	@Inject
	BonusPolicy bonusPolicy;
	
	@Override
	public double calculateCurrentBonusFor(Employee employee) {
		List&amp;lt;CarSold&amp;gt; findByEmployee = carSoldRepository
             .findByEmployee(employee.getEntityId());
		Price bonus = bonusPolicy.computeBonus(findByEmployee);
		return bonus.getPriceValue();
	}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then, one can just inject the service and compute a bonus whenever an &lt;code&gt;Employee&lt;/code&gt; entity is at reach (eg. while building
an employee profile page).&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>