1、新建類庫(kù)WPFProjectShared,在類庫(kù)下新建文件夾Dtos,新建BaseDto.cs,繼承INotifyPropertyChanged,實(shí)現(xiàn)通知更新。
public class BaseDto : INotifyPropertyChanged
{
public int Id { get; set; }
public event PropertyChangedEventHandler? PropertyChanged;
/// <summary>
/// 實(shí)現(xiàn)通知更新
/// </summary>
/// <param name="propertyName"></param>
public void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
?2、WPFProjectAPI添加引用WPFProjectShared
?3、新建待辦事項(xiàng)傳輸實(shí)體TodoDto.cs,繼承BaseDto.cs
/// <summary>
/// 待辦事項(xiàng)傳輸實(shí)體
/// </summary>
public class TodoDto : BaseDto
{
/// <summary>
/// 標(biāo)題
/// </summary>
private string title;
public string Title
{
get { return title; }
set { title = value; OnPropertyChanged(); }
}
/// <summary>
/// 內(nèi)容
/// </summary>
private string content;
public string Content
{
get { return content; }
set { content = value; OnPropertyChanged(); }
}
/// <summary>
/// 狀態(tài)
/// </summary>
private string status;
public string Status
{
get { return status; }
set { status = value; OnPropertyChanged(); }
}
}
4、在api層引入AutoMapper,并新建文件夾Extensions,新建幫助類AutoMapperProFile.cs,繼承ProFile
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-673232.html
public class AutoMapperProFile : Profile
{
public AutoMapperProFile()
{
CreateMap<ToDo, TodoDto>().ReverseMap();
}
}
5、在program.cs中注冊(cè)AutoMapper的服務(wù)
//注冊(cè)AutoMapper服務(wù)
builder.Services.AddAutoMapper(typeof(AutoMapperProFile));
?6、IToDoService.cs修改代碼傳入ToDoDto,相應(yīng)的ToDoService.cs的代碼也要修改
public interface IToDoService : IBaseService<TodoDto>
{
}
public class ToDoService : IToDoService
{
private readonly IUnitOfWork unitOfWork;
private readonly IMapper mapper;
public ToDoService(IUnitOfWork unitOfWork, IMapper mapper)
{
this.unitOfWork = unitOfWork;
this.mapper = mapper;
}
/// <summary>
/// 新增待辦事項(xiàng)
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public async Task<ApiResponse> AddEntityAsync(TodoDto model)
{
try
{
var todo = mapper.Map<ToDo>(model);
await unitOfWork.GetRepository<ToDo>().InsertAsync(todo);
if(await unitOfWork.SaveChangesAsync() > 0)
{
return new ApiResponse(true, model);
}
else
{
return new ApiResponse(false, "添加數(shù)據(jù)失?。?);
}
}
catch (Exception ex)
{
return new ApiResponse(false, ex.Message);
}
}
/// <summary>
/// 刪除待辦事項(xiàng)
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResponse> DeleteEntityAsync(int id)
{
try
{
var repository = unitOfWork.GetRepository<ToDo>();
var todo = await repository.GetFirstOrDefaultAsync(predicate: t => t.Id.Equals(id));
repository.Delete(todo);
if (await unitOfWork.SaveChangesAsync() > 0)
{
return new ApiResponse(true, "刪除數(shù)據(jù)成功!");
}
else
{
return new ApiResponse(false, "刪除數(shù)據(jù)失??!");
}
}
catch (Exception ex)
{
return new ApiResponse(false, ex.Message);
}
}
/// <summary>
/// 查詢所有數(shù)據(jù)
/// </summary>
/// <returns></returns>
public async Task<ApiResponse> GetAllAsync()
{
try
{
var repository = unitOfWork.GetRepository<ToDo>();
var todo = await repository.GetAllAsync();
if (todo != null)
{
return new ApiResponse(true, todo);
}
else
{
return new ApiResponse(false, "查詢數(shù)據(jù)失敗!");
}
}
catch (Exception ex)
{
return new ApiResponse(false, ex.Message);
}
}
/// <summary>
/// 根據(jù)Id查詢數(shù)據(jù)
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResponse> GetSingleAsync(int id)
{
try
{
var repository = unitOfWork.GetRepository<ToDo>();
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}的數(shù)據(jù)!");
}
}
catch (Exception ex)
{
return new ApiResponse(false, ex.Message);
}
}
/// <summary>
/// 更新數(shù)據(jù)
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task<ApiResponse> UpdateEntityAsync(TodoDto model)
{
try
{
var dbTodo = mapper.Map<ToDo>(model);
var repository = unitOfWork.GetRepository<ToDo>();
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, "更新數(shù)據(jù)成功!");
}
else
{
return new ApiResponse(true, "更新數(shù)據(jù)失?。?);
}
}
else
{
return new ApiResponse(false, $"未查詢到Id={model.Id}的數(shù)據(jù)!");
}
}
catch (Exception ex)
{
return new ApiResponse(false, ex.Message);
}
}
}
7、F5運(yùn)行項(xiàng)目
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-673232.html
?
到了這里,關(guān)于WPF實(shí)戰(zhàn)項(xiàng)目十二(API篇):配置AutoMapper的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!