DDD Domain Driven design: Domain Services

Source: Internet
Author: User
Tags foreach bool

What is a domain service, the DDD book is to say that some classes or methods, put entity A is not good, put entity B is not good, because it is likely to involve multiple entities or aggregation of interaction (also may be many of the same type of entity), this should be the code into the Domain Services, Domain Services in fact, with the traditional three-layer BLL very similar, only the method has no attributes, there is no state, and it is best to use the verb name, service suffix, but the real time to practice, many times it is difficult to distinguish between the domain entity itself or the domain service area to achieve, In addition to the methods that require operations (typically parameters) for multiple entities, some of the operations of a single entity is difficult to distinguish strictly, actually put the entity and Domain Services can be, but there will be technical implementation problems, such as the entity inside how to inject a hasty problem, if the field services, it is easy to inject And one thing is that the entity or aggregation is best not to invoke Domain Services, really unnecessary, if there will be injection problems, so the more appropriate practice is that some methods, if it involves systematic judgment, such as user name only this lookup table, then put into the domain service, let the application layer to invoke, Domain Services are going to call 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> {///<summar Y>///project name exists///</summary>///<param name= "Xmmc" ></param>///&L
        t;returns></returns> bool Existsxmmc (string xmmc);
        <summary>///has been issued///</summary>///<param name= "APPC" ></param> <param name= "nd" ></param>///<param name= "XZQDM" ></param>///<return
     
        s></returns> BOOL Issent (int APPC, int nd, string xzqdm); <summary>///Statistical Scheduling table, the index data that has been stored///</summary>///<param name= "year" ></ Param>///<parAm Name= "XZQDM" ></param>///<returns></returns> indicatorarea totalsendtoindicator (i
    NT 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>///Get reservation metric///</summary>///<param name= "Year" ></param>// /<param name= "XZQDM" ></param>///<returns></returns> Indicatorarea Totalreservei
        Ndicator (int year, string xzqdm); <summary>///Obtain the target (plan index) issued by the designated administrative Region///</summary>///<param name= "year" ></ param>///<param name= "XZQDM" ></param>///<returns></returns> Indicat
        Orarea totalreceiveindicator (int year, string xzqdm); <summary>///to obtain the indicators issued to the designated administrative Region///</summary>///<param name= "year" ></param>///<param name= "XZQDM" ></param>///<returns></returns> indica
     
        Torarea totalsendtoindicator (int year, string xzqdm);
        <summary>///has been issued///</summary>///<param name= "APPC" ></param> <param name= "nd" ></param>///<param name= "XZQDM" ></param>///<return
        s></returns> BOOL Issent (int APPC, int nd, string xzqdm); 
        <summary>///There are already issued projects///</summary>///<param name= "APPC" ></param> <param name= "nd" ></param>///<param name= "XZQDM" ></param>///<pa Ram Name= "XFXZQDM" ></param>///<returns></returns> bool Exists (int APPC, int nd, str
    ing XZQDM, string xfxzqdm); }
}

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

2. Field Service

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>///Schedule Service///</summary> public class Arrangeservice {
        Private readonly iplanarrangerepository repository; <summary>///Service can use a number of different repository interfaces///</summary> private readonly Iplanin
     
        Dicatorrepository indicatorrepository;
            Public Arrangeservice (iplanarrangerepository repository, iplanindicatorrepository indicatorrepository) {
            This.repository = repository;
        This.indicatorrepository = indicatorrepository; ///<summary>///Registration indicator items, if the national level, then projects can not be///</summary>/// ;p Aram Name= "Planarrange" ></param>///<param name= "Planarranges" ></param> public void Register (Planarrange planarrange, ilist<planarrange> planarranges) {Checkandthrow (P
            Lanarrange, 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 modified when the call is judged by the///</summary>
            <param name= "Planarrange" ></param> public void Checkupdate (Planarrange planarrange) {
        Checkandthrow (Planarrange, true); } private void Checkandthrow (Planarrange planarrange, bool isupdate) {if (repository.
            Issent (PLANARRANGE.APPC, Planarrange.nd, PLANARRANGE.XZQDM)){throw new Domainexception ("Batch has been 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 ("project name already exists"); } else if (repository.
            EXISTSXMMC (PLANARRANGE.XMMC)) {throw new Domainexception ("project name already exists");
        } checkoverplus (Planarrange, isupdate); ///<summary>///to determine whether the remaining indicators are sufficient///<p> The total index is equal to the set in the index decomposition, if the county level, then the municipal level to the county </p&gt
        ; <p> remaining indicators equal to the total index-issued indicators (including items have been issued and not issued items) </p>///</summary>///<param name= "Planarrang E "></param> private void Checkoverplus (Planarrange planarrange, bool isupdate) {//always refer to
           Number, is this supposed to be a field event? 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 = indicator
            Repository.totalreceiveindicator (Planarrange.nd, PLANARRANGE.XZQDM); } if (Totalindicator!= null) {//plan unit is mu var xfindicator = reposit Ory.
                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 ("insufficient arable land remaining indicators"); } if (GreaterThan (Xfindicator.nyd, totalindicator.nyd)) {throw new do
                Mainexception ("Insufficient agricultural land remaining indicators"); } if (GreaterThan (Xfindicator.wlyd, Totalindicator.wlyd)) {throw new
                Domainexception ("Insufficient remaining indicators not available");  }}///<summary>//////</summary>///<param Name= "Mu" ></param>///<param name= "hectare" ></param>///<returns></return
            s> private bool GreaterThan (decimal mu, decimal hectare) {decimal left = 0;
            decimal right = 0;
                if (mu > 0 && mu% = 0) {left = MU/15;
            right = hectare;
      else {left = mu * 666.6666667M;          right = hectare * 10000;
        Return to 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>///program Indicator Registration Service///</summary> public class Indicatorservice
     
        {Private readonly iplanindicatorrepository repository;
        Public Indicatorservice (Iplanindicatorrepository repository) {this.repository = repository; ///<summary>///Registration indicator Item, if the national level, then projects is empty///</summary>///<p Aram Name= "Planindicator" ></param>///<param name= "planindicators" ></param> public V OID Register (planindicator planindicator, ilist<planindicator> planindicators) {if (planindicat Or.
                                ZBJB!= indicatorgrade.country) {var Totalarea = Planindicator.indicatorarea + IndicaTorarea.sum (Planindicators.select (t => t.indicatorarea));
                Checkandthrow (Planindicator, Totalarea, false); Guaranteed Aggregation Integrity 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 modified when calling the judgment of the///</summary>///<param name= "p Lanindicator "></param> public void Checkupdate (Planindicator planindicator) {Checkan
        Dthrow (Planindicator, Planindicator.indicatorarea, true); ///<summary>///This method is modified when calling the judgment of the///</summary>///<param name= "p Lanindicator "></param> private void Checkandthrow (PlanindicaTor 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 has been issued, not allowed to register or modify");
                //Issued when judged, a administrative region can only be a number if (PLANINDICATOR.XFXZQDM!= planindicator.xzqdm) { if (isupdate) {if (original).  XFXZQDM!= planindicator.xfxzqdm && Exists (planindicator)) {throw new
                    Domainexception ("In the same lot, not allowed to the same administrative region issued several times"); } else if (Exists (Planindicator)) {throw new domaine
                Xception ("In the same lot, not allowed to the same administrative region issued several times");
            } var overindicator = Totaloverplusindicator (planindicator.nd, PLANINDICATOR.XZQDM); if (iSupdate) {overindicator + = original.
            Indicatorarea; } if (area.
            Nyd > Overindicator.nyd) {throw new domainexception ("insufficient agricultural land remaining indicators");
            } if (Area.gd > overindicator.gd) {throw new domainexception ("insufficient arable land remaining indicators"); } if (area.
            Wlyd > Overindicator.wlyd) {throw new domainexception ("insufficient unused remaining indicators"); }}///<summary>///get remaining indicators///</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 planindicator) {return repository.
        Exists (PLANINDICATOR.APPC, Planindicator.nd, PLANINDICATOR.XZQDM, PLANINDICATOR.XFXZQDM); }
    }
}

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.