The first step: The interface is fixed
A standard service interface typically contains
- Add or update a single record: Serviceresult addorupdate (Entity model)
- Add or update a single record in bulk: Serviceresult addorupdate (ienumerable<entity> soucre)
- Get a single record: Entity Getsingle (long? id)
- Get list record:list<entity> GetList (expression<func<entity, bool>> predicate = null)
- Get Paging Records:pagedlist<entity> getpagedlist (querymodel query, expression<func<entity, bool>> predicate = Null
- 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:
- Add and update methods are uniformly written together, do not separate with addorupdate
- GetList method passed in Expression<func<bill, bool>> parameter
- The AddOrUpdate method does not need to be assigned a value, and defaults to the entity's default value
Standard Service Interface Sample code