ABP基礎實踐訓練,一個簡易的博客(增刪改查)等功能 二:

個人認爲應用服務層的搭建是ABP框架的核心,掌握了這個就能基本上手工作了,在上一個博客中我們創建了一個分類表實體類,這次我們就圍繞這個實體類實現增刪改查的方法。


一:創建好相關的目錄結構
首先我們在應用層下建立相關的文件夾存放Dto(數據傳輸對象)以及接口、方法實現類等,使用Dto可以更好的做到表現層與模型層的解耦,也可以更方便序列化!
目錄結構


二:實現Dto類與實體的映射
創建一個基礎的Dto類
這裏寫圖片描述
如果Dto類與實體類,類型相同的情況下可以直接在Dto類的頭部實現自動映射(如圖所示),如果有類型不相同的情況那麼必須自定義映射規則!
我這裏舉一個簡單的例子吧!更詳細的用法可以自行上網百度AutoMapper的教程!
打開位於應用層中的***AbpApplicationModule類
這裏寫圖片描述

 //自定義映射規則
 cfg.CreateMap<Channels.Channel, Channels.Dto.ChannelDto().ForMember(dto => dto.state, map => map.MapFrom(m => m.state == 1 ? true : false));

到這裏基礎的Dto類就創建完成了!然後我們在創建一個類用於查詢需要的輸入參數:
這裏寫圖片描述


三:創建服務接口與實現類
服務接口和實現類的命名必須規範如:IChannelAppService、ChannelAppService,接口前面加I,首字母需要大寫,結尾必須爲***AppService!
這裏寫圖片描述
這裏寫圖片描述

接口代碼:

 public interface IChannelAppService: IApplicationService
    {
        /// <summary>
        /// 查找欄目列表
        /// </summary>
        /// <param name="getTourDeIn"></param>
        /// <returns></returns>
        List<ChannelDto> GetChannelList(GetChannelInput input);
        /// <summary>
        /// 插入欄目
        /// </summary>
        /// <param name="Channel"></param>
        /// <returns></returns>
        int InsertChannel(ChannelDto Channel);
        /// <summary>
        /// 更新欄目
        /// </summary>
        /// <param name="Channel"></param>
        /// <returns></returns>
        int UpdateChannel(ChannelDto Channel);
        /// <summary>
        /// 刪除欄目
        /// </summary>
        /// <param name="Channel"></param>
        /// <returns></returns>
        int DelChannel(int id);
        /// <summary>
        /// 分頁獲取欄目
        /// </summary>
        /// <param name="showHidden"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        IPagedResult<ChannelDto> GetChannelPageList(List<ChannelDto> ChannelList, bool showHidden = false, int pageIndex = 0, int pageSize = int.MaxValue);
        /// <summary>
        /// 獲取欄目詳細信息
        /// </summary>
        /// <param name="getTourDeIn"></param>
        /// <returns></returns>
        ChannelDto GetChannel(GetChannelInput input);
    }

實現代碼:

 public class ChannelAppService : BlogAbpAppServiceBase, IChannelAppService
    {
        #region 注入

        public IRepository<Channel> _iRepository;
        public ChannelAppService(IRepository<Channel> iRepository)
        {
            _iRepository = iRepository;

        }
        #endregion
        #region curd函數
        /// <summary>
        /// 查找欄目列表
        /// </summary>
        /// <param name="getTourDeIn"></param>
        /// <returns></returns>
        public List<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)) 可自主增加條件

             );
            }
            return Channel;
        }
        /// <summary>
        /// 插入欄目
        /// </summary>
        /// <param name="Channel"></param>
        /// <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>
        /// 更新欄目
        /// </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>
        /// 刪除欄目
        /// </summary>
        /// <param name="Channel"></param>
        /// <returns></returns>
        public int DelChannel(int id)
        {

            try
            {
                _iRepository.Delete(id);
                return 1;
            }
            catch (Exception)
            {

                return 0;
            }
        }
        /// <summary>
        /// 分頁獲取欄目
        /// </summary>
        /// <param name="showHidden"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IPagedResult<ChannelDto> GetChannelPageList(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>
        /// 獲取欄目詳細信息
        /// </summary>
        /// <param name="getTourDeIn"></param>
        /// <returns></returns>
        public ChannelDto GetChannel(GetChannelInput input)
        {
            ChannelDto Channel = new ChannelDto();
            Channel = Mapper.Map<Channel, ChannelDto>(_iRepository.Get(input.Id));
            return Channel;
        }
        #endregion
    }

代碼中引用了一個自定義的分頁實現類,代碼奉上:

   /// <summary>
     /// 分頁查詢實現類
     /// </summary>
     /// <typeparam name="T">T對象爲實體對象的繼承類</typeparam>
    [Serializable]
    public class PagedResult<T> : IPagedResult<T> where T : class, new()
    {
        /// <summary>
        /// 實例化
        /// </summary>
        /// <param name="source">數據源</param>
        /// <param name="pageIndex">當前頁</param>
        /// <param name="pageSize">頁大小</param>
        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>
        /// 實例化
        /// </summary>
        /// <param name="source">數據源</param>
        /// <param name="pageIndex">當前頁</param>
        /// <param name="pageSize">頁大小</param>

        public PagedResult(IList<T> source, int pageIndex, int pageSize)
        {
            int total = source.Count();
            this.TotalCount = total;
            this.Items = source.Skip(pageIndex * pageSize).Take(pageSize).ToList();
        }
        /// <summary>
        /// 數據集
        /// </summary>
        public IReadOnlyList<T> Items { get; set; }

        /// <summary>
        /// 總個數
        /// </summary>
        public int TotalCount { get; set; }
    }

應用層的創建大概就是這些,接下來就是web層如何實現了~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章