Standard Service Interface Sample code

Source: Internet
Author: User

The first step: The interface is fixed

A standard service interface typically contains

    1. Add or update a single record: Serviceresult addorupdate (Entity model)
    2. Add or update a single record in bulk: Serviceresult addorupdate (ienumerable<entity> soucre)
    3. Get a single record: Entity Getsingle (long? id)
    4. Get list record:list<entity> GetList (expression<func<entity, bool>> predicate = null)
    5. Get Paging Records:pagedlist<entity> getpagedlist (querymodel query, expression<func<entity, bool>> predicate = Null
    6. Delete Record: Serviceresult delete (long id);

Example code that has financial records as an example:

 /// <summary>    ///Financial Records/// </summary>     Public InterfaceIbillservice:idynamicservice {/// <summary>        ///Add or update financial records/// </summary>        /// <param name= "entity" ></param>        /// <returns></returns>        voidaddorupdate (Bill entity); /// <summary>        ///Bulk Add or update financial records/// </summary>        /// <param name= "Soucre" ></param>        /// <returns></returns>        voidAddOrUpdate (ienumerable<bill>soucre); /// <summary>        ///get a financial record/// </summary>        /// <param name= "id" >C Financial Record ID</param>        /// <returns></returns>Bill Getsingle (Long?ID); /// <summary>        ///get a list of financial records/// </summary>        /// <param name= "predicate" >Query Criteria</param>        /// <returns></returns>List<bill> GetList (Expression<func<bill,BOOL>> predicate =NULL); /// <summary>        ///get financial records for Paginated Records/// </summary>        /// <param name= "Query" >Query Criteria</param>        /// <param name= "predicate" >Query Criteria</param>        /// <returns></returns>Pagedlist<bill> getpagedlist (querymodel query, Expression<func<bill,BOOL>> predicate =NULL); /// <summary>        ///Delete financial records/// </summary>        /// <param name= "id" ></param>        /// <returns></returns>        voidDelete (Long?ID); }
View Code

Step Two: Complete the service interface
   Public classBillservice:servicebase, Ibillservice { Public voidAddOrUpdate (ienumerable<bill>soucre) {            if(Soucre = =NULL)                Throw NewArgumentNullException (nameof (soucre)); foreach(varIteminchsoucre)            {addorupdate (item); }        }         Public voidaddorupdate (Bill entity) {if(Entity. Id >0) {Repository<BillRepository>().            Updatesingle (entity); } Else{Repository<BillRepository>().            Addsingle (entity); }        }         Public voidDelete (Long?ID) {Repository<BillRepository> (). Delete (r=>r.id==ID); }         PublicList<bill> GetList (Expression<func<bill,BOOL>> predicate =NULL) {            Throw Newnotimplementedexception (); }         PublicPagedlist<bill> getpagedlist (querymodel query, Expression<func<bill,BOOL>> predicate =NULL) {            returnRepository<billrepository>().        Readmany (query, predicate); }         PublicBill Getsingle (Long?ID) {returnRepository<billrepository> (). Readsingle (E = E.id = =ID); }    }
View Code

Note implementation:

    1. Add and update methods are uniformly written together, do not separate with addorupdate
    2. GetList method passed in Expression<func<bill, bool>> parameter
    3. The AddOrUpdate method does not need to be assigned a value, and defaults to the entity's default value

Standard Service Interface Sample code

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.