自动映射器中一种类型到另一种类型的泛型类

时间:2019-06-23 11:20:36

标签: c# generics asp.net-core automapper

我有这个通用分页类:我想映射PagedList<Caste> to PagedList<CasteModel>

  public class PagedList<T>
        {
            public PagedList()
            {
            }
            public PagedList(IList<T> source, int pageNumber, int pageSize)
            {
                this.TotalItems = source.Count;
                this.PageNumber = pageNumber;
                this.PageSize = pageSize;
                this.Items = source;
            }

            public int TotalItems { get; set; }
            public int PageNumber { get; set; }
            public int PageSize { get; set; }
            public IEnumerable<T> Items { get; set; }
            public int TotalPages => (int)Math.Ceiling(this.TotalItems / (double)this.PageSize);


        }

以及模型和视图模型类

    public class Caste
    {
        public int Id { get; set; }
        public string CasteCode { get; set; }
        public string CasteDesc { get; set; }
        public bool IsActive { get; set; }
        public int? CasteParentId { get; set; }
        public virtual Caste CasteParent { get; set; }
        public virtual ICollection<Caste> CasteChildren { get; set; }

        public virtual ICollection<Customer> Customers { get; set; }

    }

public class CasteModel
    {
        public int Id { get; set; }
        public string CasteCode { get; set; }
        public string CasteDesc { get; set; }
        public bool IsActive { get; set; }
        public int? CasteParentId { get; set; }

    }

以下是我的自动映射器配置

 public class AppProfile : Profile
    {
        public AppProfile()
        {

            //Masters
            CreateMap<CasteModel, Caste>();
            CreateMap<Caste, CasteModel>();

            CreateMap(typeof(PagedList<>), typeof(PagedList<>));
         // CreateMap<PagedList<Caste>, PagedList<CasteModel>>(); ---This also checked
        }

这是控制器中映射的代码

 PagedList<Caste> result = new PagedList<Caste>
                {
                     Items = new List<Caste> { new Caste { Id = 7, CasteCode="" } },
                     TotalItems = 1
                };

                var pagedListOfDtos = Mapper.Map<PagedList<CasteModel>>(result);

执行以下错误时,发生以下异常

“映射器未初始化。以适当的配置调用初始化。如果您试图通过容器或其他方式使用映射器实例,请确保您没有对静态Mapper.Map方法的任何调用,并且如果使用的是ProjectTo或UseAsDataSource扩展方法,请确保您传入适当的IConfigurationProvider实例。“

Am使用Asp.net核心和automapper 6.1。根据以下链接编写代码 generic list to automapper 请建议我的解决方案尝试了很多都收到相同的消息

1 个答案:

答案 0 :(得分:1)

对于Mapper.Map<PagedList<CasteModel>>(result);,您需要像Mapper一样初始化Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        Mapper.Initialize(cfg =>
        {
            cfg.AddProfile<AppProfile>();
        });
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

但是,它建议使用Dependence Injection来解决Mapper

  1. 安装软件包AutoMapper.Extensions.Microsoft.DependencyInjection

  2. Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAutoMapper(typeof(Startup));
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }
    
  3. UseCase

    public class ValuesController : ControllerBase
    {
        private readonly IMapper _mapper;
        public ValuesController(IMapper mapper)
        {
            _mapper = mapper;
        }
        // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            PagedList<Caste> result = new PagedList<Caste>
            {
                Items = new List<Caste> { new Caste { Id = 7, CasteCode = "" } },
                TotalItems = 1
            };
    
            var pagedListOfDtos = _mapper.Map<PagedList<CasteModel>>(result);
            return new string[] { "value1", "value2" };
        }       
    }