首页 > 其他分享 >使用AutoMapper

使用AutoMapper

时间:2023-12-04 11:46:33浏览次数:29  
标签:mapper builder app public 使用 AutoMapper class

1、在控制台中

namespace StudyAutoMapper
{
    public class Foo
    {
        public int ID { get; set; }

        public string Name { get; set; }
    }

    public class FooDto
    {
        public int ID { get; set; }

        public string Name { get; set; }
    }

    public class AutoMapperProfile : Profile
    {
        public AutoMapperProfile() //配置AutoMapper
        {
            CreateMap<Foo, FooDto>();
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile<AutoMapperProfile>();
            });

            var mapper = config.CreateMapper();

            Foo foo = new Foo { ID = 1, Name = "Tom" };

            FooDto dto = mapper.Map<FooDto>(foo);

        }
    }
}
在控制台中使用AutoMapper

2、在ASP.NET Core WebAPI中

public class AutoMapperProfile : Profile
    {
        public AutoMapperProfile() 
        {
            CreateMap<Foo,FooDto>();
        }
    }
创建AutoMapperConfig.cs文件,继承自Profile
 public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();
            builder.Services.AddAutoMapper(typeof(AutoMapperProfile));//注册AutoMapper
            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }
            
            app.UseHttpsRedirection();

            app.UseAuthorization();


            app.MapControllers();

            app.Run();
        }
在Main中注册AutoMapper
 [ApiController]
    [Route("[controller]")]
    public class testFroController : ControllerBase
    {
        private readonly IMapper mapper;

        public testFroController(IMapper mapper)
        {
            this.mapper = mapper;
        }
       
        [HttpGet(Name = "GetTestForFooDto")]
        public ActionResult<FooDto> Get()
        {
            Foo foo = new Foo { ID = 1, Name = "Tom" };

            FooDto dto = mapper.Map<FooDto>(foo);
            return Ok(dto);
        }
    }
在Controller中使用AutoMapper

 

标签:mapper,builder,app,public,使用,AutoMapper,class
From: https://www.cnblogs.com/kjgagaga/p/17874564.html

相关文章

  • 【实用+干货】如何使用Clickhouse搭建百亿级用户画像平台看这一篇就够了
    背景如果你是用户,当你使用抖音、小红书的时候,假如平台能根据你的属性、偏好、行为推荐给你感兴趣的内容,那就能够为你节省大量获取内容的时间。如果你是商家,当你要进行广告投放的时候,假如平台推送的用户都是你潜在的买家,那你就可以花更少的钱,带来更大的收益。这两者背后都有一项......
  • git使用小结
    一、提交代码到master仓库1、克隆新建仓库地址gitclone"xxx"2、查看仓库状态gitstatus3、拷贝上传的代码到目录并上传gitadd.4、加备注,加说明gitcommit-m"firstcommit"5、输入用户名和邮箱gitconfig--globaluser.name"wanggangtao"gitconfig--globalus......
  • Golang使用kcp
    安装goget-ugithub.com/xtaci/kcp-goimport("fmt""github.com/xtaci/kcp-go""golang.org/x/net/ipv4""golang.org/x/net/ipv6""net")//KCP服务器funcserver(){//创建一个UDP连接u......
  • notepad++中的正则表示式使用方法
    当使用Notepad+中的正则表达式时,可以利用以下常用的元字符和语法规则来构建你的表达式:正则表达式知识准备字符匹配:\d:匹配任意数字。\w:匹配任意字母、数字或下划线。\s:匹配任意空白字符(空格、制表符等)。.:匹配除换行符外的任意字符。重复次数:*:匹配前一个元素零次或......
  • 【让AI女友跟我表白】大白话说Python+Flask入门(四)Flask Sijax的使用
    写在前面先吐槽两句,搞个mysql安装配置弄了4个小时,怎么都是外网无法访问,我靠,我特么也是服了。当然,后来我投降了,明天再说,学什么不是学,娘的,换个方向,状态依然在!Sijax是什么?代表SimpleAjax,它是一个Python/jQuery库,使用jQuery.ajax来进行AJAX请求用的。安装依赖pipinstallflas......
  • hibernate使用原生sql查询Hibernate原生SQL多表查询字段名重复问题以及解决方法
    解决方案通过将别名.*换成{别名.*}hibernate会自动为我们生成别名,具体修改如下图: ......
  • Windows驱动中使用数字签名验证控制设备访问权限
    1.背景  在一般的驱动开发时,创建了符号链接后在应用层就可以访问打开我们的设备并进行通讯。  但我们有时候不希望非自己的进程访问我们的设备并进行交互,虽然可以使用IoCreateDeviceSecure来创建有安全描述符的设备,但大数的用户账户为了方便都是管理员,因此该方法不太完整......
  • .NET 7(C#)配置使用log4net日志框架的方法
    .NET7(C#)中配置和使用log4net日志框架是一个涉及多步骤的过程。log4net是一个高度灵活且强大的日志记录库,可用于记录应用程序的运行时信息。它支持各种日志输出格式和目的地,如文件、数据库、控制台等。以下是在.NET7(C#)项目中配置和使用log4net的基本步骤:1.安装log4net......
  • ubuntu22.04使用esp_idf
    因为需要安装这个开发工具,所以这里记录一下,主要是参考的这个网页:https://docs.espressif.com/projects/esp-idf/zh_CN/release-v4.4/esp32/get-started/index.html#esp-idf这里特别注意:这个过程需要下载很多东西,建议使用梯子,不然很容易失败,我本人是前两次没用梯子,都失败了,第三次......
  • C++中 enum 的使用
    C++中的枚举enum使用如果没有对应的范围限定,会很容易出现重定义的错误。如下的重定义错误,在一个文件中有如下两个枚举,编译时会出现重定义错误enumType{MIN_TYPE,MAX_TYPE,INPUT_TYPE};enumMode{CITY,INPUT_TYPE};此时虽然是不同的枚举类型,......