Domain-driven design of DDD

Source: Internet
Author: User

1. How to extract models from DDD domain-driven design practices

2. Domain-driven design of DDD: aggregation, entity, and value object

3. DDD domain-driven design: Infrastructure Layer

What is a domain service? In the DDD book, some classes or methods are not good for Entity A, and it is not good for entity B, because it is likely to involve multiple entities or aggregate interactions (or multiple entities of the same type), put the code into the domain service, in fact, domain services are very similar to the traditional three-layer BLL. Only methods have no attributes and are stateless. It is best to use verbs and service as the suffix, but when it comes to practice, in many cases, it is difficult to distinguish between the implementation of the domain entity itself and the implementation of the domain service. Apart from the methods that require operations (usually parameters) for multiple entities, some operations on a single entity are hard to be strictly differentiated. In fact, they can be applied to both entities and domain services, but there will be technical implementation problems, such as how to inject hasty problems into the entity, if the domain service is deployed, it is easy to inject. Another point is that entities or aggregation is best not to call domain services. It is really unnecessary. If so, there will be injection problems, therefore, it is more appropriate to use some methods. If systemic judgment is involved, such as searching tables with the unique user name, it should be placed in the domain service so that the application layer can call , Domain services are calling warehousing.

1. warehousing Interface

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; using DDD. infrastructure; using DDD. infrastructure. domain; namespace DDD. domain. arrange {public interface iplanarrangerepository: irepository <planarrange >{/// <summary> /// check whether the project name exists /// </Summary> /// <Param name = "xmmc"> </param> /// <returns> </returns> bool existsxmmc (string xmmc ); /// <summary> /// whether it has been issued /// </Summary> /// <Param name = "APPC"> </param> // <Param name = "nd"> </param> /// <Param name = "xzqdm"> </param> /// <returns> </returns> bool issent (int appc, int nd, string xzqdm); // <summary> // statistical plan table, metric data stored /// </Summary> /// <Param name = "year"> </param> /// <Param name = "xzqdm"> </ param> // <returns> </returns> indicatorarea totalsendtoindicator (INT year, string xzqdm );}}
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; using DDD. infrastructure. domain; namespace DDD. domain. indicator {public interface iplanindicatorrepository: irepository <planindicator >{/// <summary> /// obtain reserved metrics /// </Summary> /// <Param name = "year"> </param>/ // <Param name = "xzqdm"> </param> // <returns> </returns> indicatorarea totalreserveindicator (INT year, string xzqdm); // <summary> // obtain the indicator issued by the specified administrative region (plan indicator) /// </Summary> /// <Param name = "year"> </param> /// <Param name = "xzqdm"> </param> /// <returns> </returns> indicatorarea totalreceiveindicator (INT year, string xzqdm ); /// <summary> /// obtain the indicator issued to the specified administrative region. /// </Summary> /// <Param name = "year"> </param> // /<Param name = "xzqdm"> </param> // <returns> </returns> indicatorarea totalsendtoindicator (INT year, string xzqdm ); /// <summary> /// whether it has been issued /// </Summary> /// <Param name = "APPC"> </param> // <Param name = "nd"> </param> /// <Param name = "xzqdm"> </param> /// <returns> </returns> bool issent (int appc, int nd, string xzqdm ); /// <summary> /// check whether a project has been delivered/// </Summary> /// <Param name = "APPC"> </param> // <param name = "nd"> </param> // <Param name = "xzqdm"> </param> // <Param name = "xfxzqdm"> </Param >/// <returns> </returns> bool exists (int appc, int nd, string xzqdm, string xfxzqdm );}}

  

2. Domain services

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; using DDD. domain. indicator; using DDD. infrastructure; namespace DDD. domain. arrange {// <summary> /// Scheduler Service /// </Summary> public class arrangeservice {private readonly iplanarrangerepository repository; /// <summary> /// can the service use multiple different repository interfaces? // </Summary> private readonly iplanin Dicatorrepository indicatorrepository; Public arrangeservice (iplanarrangerepository repository, iplanindicatorrepository indicatorrepository) {This. repository = repository; this. indicatorrepository = indicatorrepository;} // <summary> // register an indicator item, projects can be left blank, /// </Summary> /// <Param name = "planarrange"> </param> /// <Param name = "planarranges"> </ param> Public void register (planarrange P Lanarrange, ilist <planarrange> planarranges) {checkandthrow (planarrange, false); planarrange. Register (); If (planarranges! = NULL) {foreach (VAR item in planarranges) {item. APPC = planarrange. APPC; item. nD = planarrange. nd; item. xzqdm = planarrange. xzqdm; item. register ();}}} /// <summary> /// this method is called for judgment during modification. /// </Summary> /// <Param name = "planarrange"> </param> public void checkupdate (planarrange) {checkandthrow (planarrange, true);} private void checkandthrow (planarrange, bool isupdate ){ If (repository. issent (planarrange. APPC, planarrange. nd, planarrange. xzqdm) {Throw new domainexception ("batch issued, not allowed to register or modify");} If (isupdate) {var original = repository. find (planarrange. ID); If (original. xmmc! = Planarrange. xmmc & repository. existsxmmc (planarrange. xmmc) {Throw new domainexception ("the project name already exists") ;}} else if (repository. existsxmmc (planarrange. xmmc) {Throw new domainexception ("the project name already exists");} checkoverplus (planarrange, isupdate );} /// <summary> /// determine whether the remaining indicators are sufficient // <p> the total indicator is equal to the reserved part in the indicator decomposition. If the remaining indicator is at the county level, </P> // <p> the remaining indicators issued by the municipal level are equal to the total Indicators issued by the county level (including projects that have been issued and not yet issued by the Project) </P> /// </Summary> /// <Param name = "planarrange"> </Param> private void checkoverplus (planarrange, bool isupdate) {// total number of metrics. Should domain events be used here? indicatorarea totalindicator = NULL; If (planarrange. zbjb = indicatorgrade. province | planarrange. zbjb = indicatorgrade. city) {totalindicator = indicatorrepository. totalreserveindicator (planarrange. nd, planarrange. xzqdm);} else if (planarrange. zbjb = indicatorgrade. county) {totalindicator = ind Icatorrepository. totalreceiveindicator (planarrange. nd, planarrange. xzqdm);} If (totalindicator! = NULL) {// The scheduler unit is Mu var xfindicator = repository. totalsendtoindicator (planarrange. nd, planarrange. xzqdm); xfindicator + = planarrange. jhsy; If (isupdate) {var original = repository. find (planarrange. ID); xfindicator-= original. jhsy;} If (greaterthan (xfindicator. GD, totalindicator. GD) {Throw new domainexception ("");} If (greaterthan (xfindicator. nyd, totalindicator. nyd) {Throw new domainexception ("insufficient agricultural land surplus index");} If (greaterthan (xfindicator. wlyd, totalindicator. wlyd) {Throw new domainexception ("unused remaining metrics ");}}} /// <summary> ///// </Summary> /// <Param name = "Mu"> </param> /// <Param name = "hectare "> </param> // <returns> </returns> private bool greaterthan (decimal Mu, decimal hectare) {decimal left = 0; decimal right = 0; If (MU> 0 & Mu % 15 = 0) {left = MU/15; Right = hectare ;} else {left = Mu * 666.6666667 m; Right = hectare * 10000;} return left> right ;}}}
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; using DDD. infrastructure; namespace DDD. domain. indicator {// <summary> // plan indicator registration service /// </Summary> public class indicatorservice {private readonly iplanindicatorrepository repository; Public indicatorservice (repeated repository) {This. repository = repository;} // <summa Ry> // register the metric item. If it is at the national level, projects is blank /// </Summary> /// <Param name = "planindicator"> </param> /// <Param name = "planindicators"> </Param> Public void register (planindicator, ilist <planindicator> planindicators) {If (planindicator. zbjb! = Indicatorgrade. country) {var totalarea = planindicator. indicatorarea + indicatorarea. sum (planindicators. select (t => T. indicatorarea); checkandthrow (planindicator, totalarea, false); // ensures the integrity of the aggregate foreach (VAR item in planindicators) {item. APPC = planindicator. APPC; item. nD = planindicator. nd; item. xzqdm = planindicator. xzqdm; item. register () ;}} planindicator. register ();} // <summary> // This method is When the modification is made, call the judgment /// </Summary> /// <Param name = "planindicator"> </param> Public void checkupdate (planindicator) {checkandthrow (planindicator, planindicator. indicatorarea, true );} /// <summary> /// this method is called for judgment during modification. /// </Summary> /// <Param name = "planindicator"> </param> private void checkandthrow (planindicator, indicatorarea area, bool isupdate) {var original = isupdate? Repository. find (planindicator. ID): NULL; If (repository. issent (planindicator. APPC, planindicator. nd, planindicator. xzqdm) {Throw new domainexception ("batch issued, not allowed to register or modify");} // when it is issued, it is judged that one administrative region can only have one document number if (planindicator. xfxzqdm! = Planindicator. xzqdm) {If (isupdate) {If (original. xfxzqdm! = Planindicator. xfxzqdm & exists (planindicator) {Throw new domainexception ("multiple times cannot be issued to the same administrative region in the same batch");} else if (exists (planindicator )) {Throw new domainexception ("multiple times cannot be issued to the same administrative region in the same batch");} var overindicator = totaloverplusindicator (planindicator. nd, planindicator. xzqdm); If (isupdate) {overindicator + = original. indicatorarea;} If (area. nyd> overindicator. nyd) {Throw new domainexception ("insufficient agricultural land surplus index");} If (area. GD> overindicator. GD) {Throw new domainexception ("insufficient farmland surplus index");} If (area. wlyd> overindicator. wlyd) {Throw new domainexception ("available indicators are insufficient ");}} /// <summary> /// obtain the remaining metrics /// </Summary> /// <Param name = "year"> </param> /// <Param name = "xzqdm"> </param> // <returns> </returns> private indicatorarea totaloverplusindicator (INT year, string xzqdm) {return repository. totalreceiveindicator (year, xzqdm)-repository. totalreserveindicator (year, xzqdm)-repository. totalsendtoindicator (year, xzqdm);} private bool exists (planindicator) {return repository. exists (planindicator. APPC, planindicator. nd, planindicator. xzqdm, planindicator. xfxzqdm );}}}

  

Domain-driven design of DDD

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.