ABP Basic Practice training, a simple blog (to increase the search) and other features two:

Source: Internet
Author: User

Personally think that the application of the service layer is the core of the ABP framework, mastered this can basically hand work, in the last blog we created a classification table entity class, this time we will be around this entity class to implement and modify the method of checking.

One: Create a good related directory structure
First of all, we set up the relevant folders in the application layer to store the DTO (data transmission objects), as well as interface, method implementation class, etc., using the DTO can better achieve the performance layer and model layer decoupling, can also be more convenient serialization.

Two: Realizing the mapping of DTO class and entity
To create a base DTO class

If the Dto class is the same type as the entity class, you can implement automatic mapping directly at the head of the Dto class (as shown in the figure), and you must customize the mapping rules if you have a different type.
Let me give you a simple example! More detailed usage can be on the internet Baidu AutoMapper tutorial.
Open the ***abpapplicationmodule class that is located in the application tier

Custom mapping Rule
 cfg. Createmap<channels.channel, Channels.Dto.ChannelDto (). Formember (dto => dto.state, map => map. Mapfrom (m => m.state = = 1 true:false));

The Dto class that is based here is created. We then create a class to query the required input parameters:

Three: Create service Interfaces and implementation classes
The name of the service interface and the implementation class must be specification such as: Ichannelappservice, Channelappservice, front of the interface plus I, the initials need to be capitalized, and the end must be ***appservice.

Interface code:

 Public interface Ichannelappservice:iapplicationservice {///<summary>///find column list///
        </summary>///<param name= "Gettourdein" ></param>///<returns></returns>
        list<channeldto> getchannellist (getchannelinput input);
        <summary>///Insert Column///</summary>///<param name= "Channel" ></param>
        <returns></returns> int Insertchannel (channeldto Channel);
        <summary>///Update column///</summary>///<param name= "Channel" ></param>
        <returns></returns> int Updatechannel (channeldto Channel);
        <summary>///Delete Column///</summary>///<param name= "Channel" ></param>
        <returns></returns> int delchannel (int id); <summary>///PaginationGet column///</summary>///<param name= "ShowHidden" ></param>///<param name= "pa Geindex "></param>///<param name=" pageSize "></param>///<returns></returns > ipagedresult<channeldto> getchannelpagelist (list<channeldto> channellist, bool ShowHidden = False , int pageIndex = 0, int pageSize = Int.
        MaxValue); <summary>///get column Details///</summary>///<param name= "Gettourdein" ></par
    am>///<returns></returns> channeldto getchannel (getchannelinput input); }

Implementation code:

 public class Channelappservice:blogabpappservicebase, Ichannelappservice {#region injected public Irepo
        Sitory<channel> _irepository;

        Public Channelappservice (irepository<channel> irepository) {_irepository = irepository; 
        #endregion #region curd function///<summary>///lookup column list///</summary> <param name= "Gettourdein" ></param>///<returns></returns> public Lis t<channeldto> getchannellist (Getchannelinput input) {list<channeldto> Channel = new List
            <ChannelDto> (); if (input!= null) {Channel = Mapper.map<list<channeldto>> (_irepository.getall (). Where (c => c.state = = 1)//. Whereif (!string.
            IsNullOrEmpty (Gettourdein.name), C => c.name.contains (gettourdein.name)) can increase the conditions autonomously; } rEturn Channel; ///<summary>///Insert Column///</summary>///<param name= "Channel" ></p
            aram>///<returns></returns> public int insertchannel (Channeldto gechannel) {
                try {Channel Channel = mapper.map<channeldto, channel> (Gechannel);

            Return _irepository.insertandgetid (Channel);

            catch (System.Exception) {return 0; }///<summary>///update columns///</summary>///<param name= "Channel"
        ></param>///<returns></returns> public int updatechannel (channeldto gechannel)
                {try {Channel Channel = mapper.map<channeldto, channel> (Gechannel);
                _irepository.update (Channel);
            return 1;
      }      catch (System.Exception) {return 0; }///<summary>///delete columns///</summary>///<param name= "Channel

            "></param>///<returns></returns> public int delchannel (int id) {
                try {_irepository.delete (ID);
            return 1;
            catch (Exception) {return 0; }///<summary>///paging get column///</summary>///<param name= "Showhi Dden "></param>///<param name=" PageIndex "></param>///<param name=" PageSize "> </param>///<returns></returns> Public ipagedresult<channeldto> Getchannelpagelis T (list<channeldto> channellist, bool ShowHidden = false, int pageIndex = 0, int pageSize = Int.
MaxValue) {            pagedresult<channeldto> Channel = new Pagedresult<channeldto> (channellist, PageIndex, pageSize);

        return Channel; ///<summary>///Get column Details///</summary>///<param name= "Gettourdein" &G
        t;</param>///<returns></returns> public channeldto getchannel (getchannelinput input)
            {Channeldto Channel = new Channeldto (); Channel = Mapper.map<channel, channeldto> (_irepository.get (input.
            ID));
        return Channel; } #endregion}

A custom paging implementation class is referenced in the

Code:

   <summary>///Paging Query implementation class///</summary>///<typeparam name= "T" >t object is an inheritance class for entity objects </type
        param> [Serializable] public class pagedresult<t>: ipagedresult<t> where T:class, new () { <summary>///instantiation///</summary>///<param name= "source" > Data source </para m>///<param name= "pageIndex" > Current page </param>///<param name= "pageSize" > Page size &LT;/PARAM&G
        T Public Pagedresult (iqueryable<t> source, int pageIndex, int pageSize) {Int. Total = source.
            Count (); This.
            TotalCount = total; This. Items = source. Skip (PageIndex * pageSize). Take (pageSize).
        ToList (); ///<summary>///instantiation///</summary>///<param name= "source" > Data source < /param>///<param name= "pageIndex" > Current page </param>///<param name= "pageSize" > Page size &LT;/PA RaM> public Pagedresult (ilist<t> source, int pageIndex, int pageSize) {Int. Total = Urce.
            Count (); This.
            TotalCount = total; This. Items = source. Skip (PageIndex * pageSize). Take (pageSize).
        ToList (); ///<summary>///Data set///</summary> public ireadonlylist<t> Items {g Et Set
    ///<summary>///Total number///</summary> public int TotalCount {get; set;} }

The creation of the application tier is probably that, followed by how the Web layer is implemented ~ ~

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.