zoukankan      html  css  js  c++  java
  • 标准服务接口示例代码

    第一步:定有接口

    标准的服务接口通常包含

    1. 添加或更新单条记录: ServiceResult AddOrUpdate(Entity model)
    2. 批量添加或更新单条记录: ServiceResult AddOrUpdate(IEnumerable<Entity> soucre)
    3. 获取单条记录: Entity GetSingle(long? id)
    4. 获取列表记录:List<Entity> GetList(Expression<Func<Entity, bool>> predicate = null)
    5. 获取分页记录:PagedList<Entity> GetPagedList(QueryModel query, Expression<Func<Entity, bool>> predicate = null)
    6. 删除记录:  ServiceResult Delete(long? id);

    已财务记录为例的示例代码:

     /// <summary>
        /// 财务记录
        /// </summary>
        public interface IBillService : IDynamicService {
            /// <summary>
            /// 添加或更新财务记录
            /// </summary>
            /// <param name="entity"></param>
            /// <returns></returns>
            void AddOrUpdate(Bill entity);
    
            /// <summary>
            /// 批量添加或更新财务记录
            /// </summary>
            /// <param name="soucre"></param>
            /// <returns></returns>
            void AddOrUpdate(IEnumerable<Bill> soucre);
    
            /// <summary>
            /// 获取一条财务记录
            /// </summary>
            /// <param name="id">c财务记录ID</param>
            /// <returns></returns>
            Bill GetSingle(long? id);
    
            /// <summary>
            /// 获取财务记录列表
            /// </summary>
            /// <param name="predicate">查询条件</param>
            /// <returns></returns>
            List<Bill> GetList(Expression<Func<Bill, bool>> predicate = null);
    
            /// <summary>
            /// 获取分页记录的财务记录
            /// </summary>
            /// <param name="query">查询条件</param>
            /// <param name="predicate">查询条件</param>
            /// <returns></returns>
            PagedList<Bill> GetPagedList(QueryModel query, Expression<Func<Bill, bool>> predicate = null);
    
            /// <summary>
            /// 删除财务记录
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            void Delete(long? id);
    
        }
    View Code

    第二步:完成服务接口

      public class BillService : ServiceBase, IBillService {
            public void AddOrUpdate(IEnumerable<Bill> soucre) {
                if (soucre == null)
                    throw new ArgumentNullException(nameof(soucre));
                foreach (var item in soucre) {
                    AddOrUpdate(item);
                }
            }
    
            public void AddOrUpdate(Bill entity) {
                if (entity.Id > 0) {
                    Repository<BillRepository>().UpdateSingle(entity);
                } else {
                    Repository<BillRepository>().AddSingle(entity);
                }
            }
    
            public void Delete(long? id) {
                Repository<BillRepository>().Delete(r=>r.Id==id);
            }
    
            public List<Bill> GetList(Expression<Func<Bill, bool>> predicate = null) {
                throw new NotImplementedException();
            }
    
            public PagedList<Bill> GetPagedList(QueryModel query, Expression<Func<Bill, bool>> predicate = null) {
                return Repository<BillRepository>().ReadMany(query, predicate);
            }
    
            public Bill GetSingle(long? id) {
                return Repository<BillRepository>().ReadSingle(e => e.Id == id);
            }
        }
    View Code

    注意实现:

    1. Add和Update方法 统一写到一起,不要分开用AddOrUpdate
    2. GetList方法传入Expression<Func<Bill, bool>>参数
    3. AddOrUpdate方法不需要再赋值了,默认会采用实体的默认值
  • 相关阅读:
    搭建 structs2 环境
    数据库基础
    好书推荐整理
    Java 完美判断中文字符
    中文在unicode中的编码范围
    [转载]爬虫的自我解剖(抓取网页HtmlUnit)
    web应用配置
    简单的Java Web服务器
    dos基本命令
    Unrecognized Windows Sockets error: 0: JVM_Bind
  • 原文地址:https://www.cnblogs.com/zkcloud/p/5575554.html
Copyright © 2011-2022 走看看