如何在C#中使用AutoMapper

AutoMapper是一种流行的对象到对象映射库,可用于映射属于不同类型的对象。 例如,您可能需要将应用程序中的DTO(数据传输对象)映射到模型对象。 AutoMapper省去了手动映射此类不兼容类型的一个或多个属性的繁琐工作。

[ 同样在InfoWorld上:敏捷开发人员的7个关键编码实践 ]

要开始使用AutoMapper,您应该在Visual Studio中创建一个项目,然后安装AutoMapper。 您可以在NuGet软件包管理器控制台窗口中使用以下命令从NuGet安装AutoMapper:

PM> Install-Package AutoMapper

使用AutoMapper创建映射

诸如AutoMapper之类的对象到对象映射器将一种类型的输入对象转换为另一种类型的输出对象。 考虑以下两个类。

 public class AuthorModel
    {
        public int Id
        {
            get; set;
        }
        public string FirstName
        {
            get;set;
        }
        public string LastName
        {
            get; set;
        }
        public string Address
        {
            get; set;
        }
    }

 public class AuthorDTO
    {
        public int Id
        {
            get; set;
        }
        public string FirstName
        {
            get; set;
        }
        public string LastName
        {
            get; set;
        }
        public string Address
        {
            get; set;
        }
    }

下面的代码片段显示了如何在这两种类型AuthorModel和AuthorDTO之间创建映射。

var config = new MapperConfiguration(cfg => {
                cfg.CreateMap<AuthorModel, AuthorDTO>();
            });

然后,执行类型之间的映射就像下面的代码所示一样简单。

IMapper iMapper = config.CreateMapper();
var source = new AuthorModel();
var destination = iMapper.Map<AuthorModel, AuthorDTO>(source);

AutoMapper示例

现在让我们处理一些数据。 请参考以下代码,该代码将一些数据存储到源对象中,然后在完成映射后在目标对象中显示属性值。

var config = new MapperConfiguration(cfg => {
                cfg.CreateMap<AuthorModel, AuthorDTO>();
            });
IMapper iMapper = config.CreateMapper();
var source = new AuthorModel();
source.Id = 1;
source.FirstName = "Joydip";
source.LastName = "Kanjilal";
source.Address = "India";
var destination = iMapper.Map<AuthorModel, AuthorDTO>(source);
Console.WriteLine("Author Name: "+ destination.FirstName + " " + destination.LastName);

当您执行上述代码时,将显示存储在目标对象中的作者姓名。 但是,目标FirstName和目标LastName属性的值将与源对象相同,因为您已经使用AutoMapper成功映射了对象!

请注意,AutoMapper可以映射任何类集。 但是,AutoMapper遵循某些约定,其中之一是要映射的属性名称应具有相同的名称。 如果属性名称不同,则必须让AutoMapper知道应如何映射属性。 假设我们要映射两个属性Contact和ContactDetails,下面的示例说明了如何实现。

var config = new MapperConfiguration(cfg => {
                cfg.CreateMap<AuthorModel, AuthorDTO>()
                .ForMember(destination => destination.ContactDetails,
               opts => opts.MapFrom(source => source.Contact));
            });

请注意以下用于创建目标对象的语句。

var destination = iMapper.Map<AuthorModel, AuthorDTO>(source);

如果目标对象已经存在,则可以使用以下语句代替。

iMapper.Map(sourceObject, destinationObject);

本质上,以上代码片段可用于映射已经存在的两个对象。

在AutoMapper中使用投影

AutoMapper为投影提供了出色的支持。 投影用于将源值映射到与源结构不匹配的目标。 (通过对比,我们上面讨论的映射是一对一的映射。)

现在让我们看一个投影。 例如,考虑下面的类。

 public class Address
    {
        public string City { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
    }

让我们的AuthorModel类使用Address类来存储作者的地址信息。 这是更新后的AuthorModel类的外观。

 public class AuthorModel
    {
        public int Id
        {
            get; set;
        }
        public string FirstName
        {
            get;set;
        }
        public string LastName
        {
            get; set;
        }
        public Address Address
        {
            get; set;
        }
    }

这是更新的AuthorDTO类。

public class AuthorDTO
    {
        public int Id
        {
            get; set;
        }
        public string FirstName
        {
            get; set;
        }
        public string LastName
        {
            get; set;
        }
        public string City { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
    }

现在假设我们需要映射AuthorDTO和AuthorModel类。 以下代码段说明了如何实现此目的。

var config = new MapperConfiguration(cfg => {
                cfg.CreateMap<AuthorDTO, AuthorModel>()
                   .ForMember(destination => destination.Address,
              map => map.MapFrom(
                  source => new Address
                  {
                      City = source .City,
                      State = source .State,
                      Country = source.Country
                  }));

我将在以后的文章中讨论AutoMapper的更高级功能。 在此之前,您可以通过此链接了解有关AutoMapper的更多信息。

如何在C#中执行更多操作:

From: https://www.infoworld.com/article/3192900/how-to-work-with-automapper-in-csharp.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值