WPF实战项目十二(API篇):配置AutoMapper

1、新建类库WPFProjectShared,在类库下新建文件夹Dtos,新建BaseDto.cs,继承INotifyPropertyChanged,实现通知更新。

public class BaseDto : INotifyPropertyChanged
    {
        public int Id { get; set; }

        public event PropertyChangedEventHandler? PropertyChanged;

        /// 
        /// 实现通知更新
        /// 
        /// 
        public void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

 2、WPFProjectAPI添加引用WPFProjectShared

 3、新建待办事项传输实体TodoDto.cs,继承BaseDto.cs

/// 
    /// 待办事项传输实体
    /// 
    public class TodoDto : BaseDto
    {
        /// 
        /// 标题
        /// 
        private string title;

        public string Title
        {
            get { return title; }
            set { title = value; OnPropertyChanged(); }
        }

        /// 
        /// 内容
        /// 
        private string content;

        public string Content
        {
            get { return content; }
            set { content = value; OnPropertyChanged(); }
        }

        /// 
        /// 状态
        /// 
        private string status;

        public string Status
        {
            get { return status; }
            set { status = value; OnPropertyChanged(); }
        }

    }

4、在api层引入AutoMapper,并新建文件夹Extensions,新建帮助类AutoMapperProFile.cs,继承ProFile

WPF实战项目十二(API篇):配置AutoMapper_第1张图片

 

    public class AutoMapperProFile : Profile
    {
        public AutoMapperProFile()
        {
            CreateMap().ReverseMap();
        }
    }

5、在program.cs中注册AutoMapper的服务

//注册AutoMapper服务
builder.Services.AddAutoMapper(typeof(AutoMapperProFile));

 6、IToDoService.cs修改代码传入ToDoDto,相应的ToDoService.cs的代码也要修改

    public interface IToDoService : IBaseService
    {
        
    }
public class ToDoService : IToDoService
    {
        private readonly IUnitOfWork unitOfWork;
        private readonly IMapper mapper;

        public ToDoService(IUnitOfWork unitOfWork, IMapper mapper)
        {
            this.unitOfWork = unitOfWork;
            this.mapper = mapper;
        }

        /// 
        /// 新增待办事项
        /// 
        /// 
        /// 
        public async Task AddEntityAsync(TodoDto model)
        {
            try
            {
                var todo = mapper.Map(model);
                await unitOfWork.GetRepository().InsertAsync(todo);
                if(await unitOfWork.SaveChangesAsync() > 0)
                {
                    return new ApiResponse(true, model);
                }
                else
                {
                    return new ApiResponse(false, "添加数据失败!");
                }
            }
            catch (Exception ex)
            {

                return new ApiResponse(false, ex.Message);
            }
        }
        /// 
        /// 删除待办事项
        /// 
        /// 
        /// 
        public async Task DeleteEntityAsync(int id)
        {
            try
            {
                var repository = unitOfWork.GetRepository();
                var todo = await repository.GetFirstOrDefaultAsync(predicate: t => t.Id.Equals(id));
                repository.Delete(todo);
                if (await unitOfWork.SaveChangesAsync() > 0)
                {
                    return new ApiResponse(true, "删除数据成功!");
                }
                else
                {
                    return new ApiResponse(false, "删除数据失败!");
                }
            }
            catch (Exception ex)
            {

                return new ApiResponse(false, ex.Message);
            }
        }
       /// 
       /// 查询所有数据
       /// 
       /// 
        public async Task GetAllAsync()
        {
            try
            {
                var repository = unitOfWork.GetRepository();
                var todo = await repository.GetAllAsync();
                if (todo != null)
                {
                    return new ApiResponse(true, todo);
                }
                else
                {
                    return new ApiResponse(false, "查询数据失败!");
                }
            }
            catch (Exception ex)
            {

                return new ApiResponse(false, ex.Message);
            }
        }

        /// 
        /// 根据Id查询数据
        /// 
        /// 
        /// 
        public async Task GetSingleAsync(int id)
        {
            try
            {
                var repository = unitOfWork.GetRepository();
                var todo = await repository.GetFirstOrDefaultAsync(predicate: t => t.Id.Equals(id));
                if (todo != null)
                {
                    return new ApiResponse(true, todo);
                }
                else
                {
                    return new ApiResponse(false, $"未查询到Id={id}的数据!");
                }
            }
            catch (Exception ex)
            {

                return new ApiResponse(false, ex.Message);
            }
        }
        /// 
        /// 更新数据
        /// 
        /// 
        /// 
        /// 
        public async Task UpdateEntityAsync(TodoDto model)
        {
            try
            {
                var dbTodo = mapper.Map(model);
                var repository = unitOfWork.GetRepository();
                var todo = await repository.GetFirstOrDefaultAsync(predicate: t => t.Id.Equals(dbTodo.Id));
                if (todo != null)
                {
                    todo.Title = dbTodo.Title;
                    todo.Content = dbTodo.Content;
                    todo.Status = dbTodo.Status;
                    todo.UpdateDate = DateTime.Now;
                    repository.Update(todo);
                    if(await unitOfWork.SaveChangesAsync() > 0)
                    {
                        return new ApiResponse(true, "更新数据成功!");
                    }
                    else
                    {
                        return new ApiResponse(true, "更新数据失败!");
                    }
                }
                else
                {
                    return new ApiResponse(false, $"未查询到Id={model.Id}的数据!");
                }
            }
            catch (Exception ex)
            {

                return new ApiResponse(false, ex.Message);
            }
        }
    }

7、F5运行项目

WPF实战项目十二(API篇):配置AutoMapper_第2张图片

WPF实战项目十二(API篇):配置AutoMapper_第3张图片WPF实战项目十二(API篇):配置AutoMapper_第4张图片

 

 

你可能感兴趣的:(WPF,wpf,前端,webapi)