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层如何实现了~~

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
ABP框架提供了通用的增删(CRUD)操作,如果需要自定义操作,可以按照以下步骤进行: 1. 定义自定义服务方法 在应用服务接口中定义自定义方法,例如: ```csharp public interface IStudentAppService : IApplicationService { Task<ListResultDto<StudentDto>> GetAllStudents(); Task<StudentDto> GetStudentById(int id); Task CreateStudent(CreateStudentInput input); Task UpdateStudent(UpdateStudentInput input); Task DeleteStudent(int id); Task MyCustomMethod(MyCustomMethodInput input); } ``` 其中,MyCustomMethod 为自定义方法。 2. 实现自定义服务方法 在应用服务实现类中实现自定义方法,例如: ```csharp public class StudentAppService : ApplicationService, IStudentAppService { private readonly IRepository<Student> _studentRepository; public StudentAppService(IRepository<Student> studentRepository) { _studentRepository = studentRepository; } public async Task<ListResultDto<StudentDto>> GetAllStudents() { var students = await _studentRepository.GetAllListAsync(); return new ListResultDto<StudentDto>(ObjectMapper.Map<List<StudentDto>>(students)); } public async Task<StudentDto> GetStudentById(int id) { var student = await _studentRepository.GetAsync(id); return ObjectMapper.Map<StudentDto>(student); } public async Task CreateStudent(CreateStudentInput input) { var student = ObjectMapper.Map<Student>(input); await _studentRepository.InsertAsync(student); } public async Task UpdateStudent(UpdateStudentInput input) { var student = await _studentRepository.GetAsync(input.Id); ObjectMapper.Map(input, student); } public async Task DeleteStudent(int id) { await _studentRepository.DeleteAsync(id); } public async Task MyCustomMethod(MyCustomMethodInput input) { // 实现自定义方法 } } ``` 其中,MyCustomMethod 实现自定义方法的逻辑。 3. 在前端代码中调用自定义服务方法 在前端代码中调用自定义服务方法,例如: ```javascript abp.services.app.student.myCustomMethod(input).done(function (result) { // 处理自定义方法的返回结果 }); ``` 其中,input 为传递给自定义方法的参数。 通过以上步骤,即可完成自定义增删操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值