From 5a9b37ffa6d078fa3b5b818cbd87ab78e170c169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Sat, 25 Dec 2021 16:55:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0automapper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/AccountController.cs | 6 ++-- .../SwaggerDoc.xml | 4 +-- .../Yi.Framework.Common/Helper/HttpHelper.cs | 1 + .../Yi.Framework.DTOModel/loginDto.cs | 14 ++++++++ .../Mapper/MapperHelper.cs | 32 +++++++++++++++++++ .../Mapper/Profile/AutoMapperProfile.cs | 20 ++++++++++++ .../Mapper/Profile/RegisterMapProfile.cs | 25 +++++++++++++++ .../MiddlewareExtend/AutoMapperExtension.cs | 19 +++++++++++ .../Yi.Framework.WebCore.csproj | 1 + 9 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 Yi.Framework.Net6/Yi.Framework.DTOModel/loginDto.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.WebCore/Mapper/MapperHelper.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.WebCore/Mapper/Profile/AutoMapperProfile.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.WebCore/Mapper/Profile/RegisterMapProfile.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/AutoMapperExtension.cs diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs index 32d475a2..62ec149c 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs @@ -16,6 +16,7 @@ using Yi.Framework.DTOModel; using Yi.Framework.Interface; using Yi.Framework.Model.Models; using Yi.Framework.WebCore; +using Yi.Framework.WebCore.Mapper; namespace Yi.Framework.ApiMicroservice.Controllers { @@ -46,11 +47,12 @@ namespace Yi.Framework.ApiMicroservice.Controllers /// /// 登录方法,要返回data:{user,token} token /// - /// + /// /// [HttpPost] - public async Task Login(user _user) + public async Task Login(loginDto login) { + var _user= MapperHelper.Map(login); var user_data = await _userService.Login(_user); if (user_data == null) { diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/SwaggerDoc.xml b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/SwaggerDoc.xml index 3173de6a..2eed29be 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/SwaggerDoc.xml +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/SwaggerDoc.xml @@ -4,11 +4,11 @@ Yi.Framework.ApiMicroservice - + 登录方法,要返回data:{user,token} token - + diff --git a/Yi.Framework.Net6/Yi.Framework.Common/Helper/HttpHelper.cs b/Yi.Framework.Net6/Yi.Framework.Common/Helper/HttpHelper.cs index 96b7c4c0..afc9c4d7 100644 --- a/Yi.Framework.Net6/Yi.Framework.Common/Helper/HttpHelper.cs +++ b/Yi.Framework.Net6/Yi.Framework.Common/Helper/HttpHelper.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; +using System.Net.Http; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; diff --git a/Yi.Framework.Net6/Yi.Framework.DTOModel/loginDto.cs b/Yi.Framework.Net6/Yi.Framework.DTOModel/loginDto.cs new file mode 100644 index 00000000..456237b4 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.DTOModel/loginDto.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.DTOModel +{ + public class loginDto + { + public string username { get; set; } + public string password { get; set; } + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/Mapper/MapperHelper.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/Mapper/MapperHelper.cs new file mode 100644 index 00000000..d8b1b5f9 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/Mapper/MapperHelper.cs @@ -0,0 +1,32 @@ +using AutoMapper; +using AutoMapper.Configuration; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.WebCore.Mapper +{ + public class MapperHelper + { + public static IMapper Profile() + { + var cfg = new MapperConfigurationExpression(); + cfg.AddProfile(); + cfg.AddProfile(); + var config = new MapperConfiguration(cfg); + IMapper mapper = new AutoMapper.Mapper(config); + return mapper; + } + + public static Target Map(Source source) + { + var cfg = new MapperConfigurationExpression(); + cfg.CreateMap(); + var config = new MapperConfiguration(cfg); + IMapper mapper = new AutoMapper.Mapper(config); + return mapper.Map(source); + } + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/Mapper/Profile/AutoMapperProfile.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/Mapper/Profile/AutoMapperProfile.cs new file mode 100644 index 00000000..a8e2a78c --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/Mapper/Profile/AutoMapperProfile.cs @@ -0,0 +1,20 @@ +using AutoMapper; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.WebCore.Mapper +{ + public class AutoMapperProfile : Profile + { + // 添加你的实体映射关系. + public AutoMapperProfile() + { + //CreateMap(); + } + } + + +} diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/Mapper/Profile/RegisterMapProfile.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/Mapper/Profile/RegisterMapProfile.cs new file mode 100644 index 00000000..f72e2518 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/Mapper/Profile/RegisterMapProfile.cs @@ -0,0 +1,25 @@ +using AutoMapper; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.WebCore.Mapper +{ + public class RegisterMapProfile : Profile + { + //添加你的实体映射关系和出参字段 + //public RegisterMapProfile() + //{ + // #region 排班信息 + // CreateMap() + // .ForMember(dest => dest.HospitalId, + // options => options.MapFrom(c => c.SelectToken("Hospital_ID"))) + // .ForMember(dest => dest.BranchId, + // options => options.MapFrom(c => c.SelectToken("Branch_ID"))) + // .ForMember(dest => dest.SchedulId, + // options => options.MapFrom(c => c.SelectToken("ScheduHeadID"))); + //} + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/AutoMapperExtension.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/AutoMapperExtension.cs new file mode 100644 index 00000000..daefb162 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/AutoMapperExtension.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.IO; + +namespace Yi.Framework.WebCore.MiddlewareExtend +{ + /// + /// 通用autoMapper扩展 + /// + public static class AutoMapperExtension + { + public static IServiceCollection AddAutoMapperExtensionService(this IServiceCollection services,Type type) + { + services.AddAutoMapper(type); + return services; + } + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj b/Yi.Framework.Net6/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj index b0418029..95c39335 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj @@ -8,6 +8,7 @@ +