在ASP.NET Core MVC 2.2 中使用AutoMapper

版权声明:版权所有,需要请联系作者 https://blog.csdn.net/weixin_42930928/article/details/86541865

使用vs 2017 创建一个新项目:Application.Web

使用nuget添加引用:AutoMapper.Extensions.Microsoft.DependencyInjection

依赖项右两个:

1、注册AutoMapper服务

在Startup.cs文件中,找到ConfigureServices方法,在方法的最后添加一行代码:

services.AddAutoMapper();

 添加完成后,如下图所示

2、映射配置文件

必须在项目中添加映射配置文件,以便AutoMapper能够正确的识别要映射的对象

映射配置类要继承Profile类。当程序第一次运行时,AutoMapper会查找从Profile类继承的类病加载配置文件

AutoMapper能够自动从源和目标映射相同的名称属性

3、添加映射文件类

映射文件类:

public class MappingProfiles : Profile
{
    public MappingProfiles()
    {
        CreateMap<Department, DepartmentDTO>().ReverseMap();
    }
}

源和目标类:

    //源
    public class Department
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Owner { get; set; }
        public string SecretProperty { get; set; }
    }

    //目标
    public class DepartmentDTO
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Owner { get; set; }
    }

4、添加Controller

在Controllers文件夹中添加一个MyMappingController类

namespace Application.Web.Controllers
{
    public class MyMappingController : Controller
    {
        private readonly IMapper _mapper;
        private readonly Department sampleDepartment;
        public MyMappingController(IMapper mapper)
        {
            _mapper = mapper;
            sampleDepartment = new Department
            {
                Name = "department1", Id = 1, Owner = "ABC", SecretProperty = "Very secret property"
            };
        }
        [HttpGet]
        public ActionResult<DepartmentDTO> Index()
        {
            return _mapper.Map<DepartmentDTO>(sampleDepartment);
        }
    }
}

5、运行项目看结果

运行项目,使用postman访问我们的项目,可以看到返回的json数据没有SecretProperty 属性的值,其他的数据都有值

6、深入使用

在上面的例子中,使用AutoMapper实现了简单的一对一的映射,但是AutoMapper也可用于对象扩展或从对象中包含对象的复杂情况。

下面我们将使用引入Person类和PersonDTO类,Person类中包含一个为Address的属性。而Address是一个类类型。在PersonDTO类中,只有一个City的属性。我们将使用AutoMapper实现从复杂对象到一个简单对象的映射。

实现类如下:

namespace Application.Web.Models
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Sex { get; set; }
        public int Age { get; set; }
        public Address Address { get; set; }
    }
    public class Address
    {
        public string HouseNumber { get; set; }
        public string Street { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
    }
    public class PersonDTO
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Sex { get; set; }
        public int Age { get; set; }
        public string City { get; set; }
    }
}

 7、修改MappingProfiles映射类

public class MappingProfiles : Profile
{
    public MappingProfiles()
    {
        CreateMap<Department, DepartmentDTO>().ReverseMap();
        CreateMap<Person, PersonDTO>()
            .ForMember(dest => dest.City, opts => opts.MapFrom(src => src.Address.City))
            .ReverseMap();
    }
}

8、在MyMappingController类中添加对Person的映射方法,修改后代码如下:

namespace Application.Web.Controllers
{
    public class MyMappingController : Controller
    {
        private readonly IMapper _mapper;
        private readonly Department sampleDepartment;
        private readonly Person samplePerson;
        public MyMappingController(IMapper mapper)
        {
            _mapper = mapper;
            sampleDepartment = new Department
            {
                Name = "department1", Id = 1, Owner = "ABC", SecretProperty = "Very secret property"
            };
            samplePerson = new Person()
            {
                FirstName = "John",
                LastName = "Doe",
                Age = 30,
                Sex = "Male",
                Address = new Address()
                {
                    City = "New York City",
                    HouseNumber = "10",
                    State = "NY",
                    ZipCode = "999999"
                }
            };
        }
        [HttpGet]
        public ActionResult<DepartmentDTO> Index()
        {
            return _mapper.Map<DepartmentDTO>(sampleDepartment);
        }
        public ActionResult<PersonDTO> GetPerson()
        {
            return _mapper.Map<PersonDTO>(samplePerson);
        }
    }
}

9、运行项目看结果

通过结果可以看到,City属性成功的使用了Address属性内的City属性值。


猜你喜欢

转载自blog.csdn.net/weixin_42930928/article/details/86541865