国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

WPF實(shí)戰(zhàn)項(xiàng)目十二(API篇):配置AutoMapper

這篇具有很好參考價(jià)值的文章主要介紹了WPF實(shí)戰(zhàn)項(xiàng)目十二(API篇):配置AutoMapper。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

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

WPF實(shí)戰(zhàn)項(xiàng)目十二(API篇):配置AutoMapper,WPF,wpf,前端,webapi

?文章來(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)目

WPF實(shí)戰(zhàn)項(xiàng)目十二(API篇):配置AutoMapper,WPF,wpf,前端,webapi

WPF實(shí)戰(zhàn)項(xiàng)目十二(API篇):配置AutoMapper,WPF,wpf,前端,webapiWPF實(shí)戰(zhàn)項(xiàng)目十二(API篇):配置AutoMapper,WPF,wpf,前端,webapi

?

?

到了這里,關(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)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包