Generic class of one type to another type in auto

2019-09-21 07:09发布

问题:

I have this Generic Pagination class: i want to map 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);


        }

And Model and View Model Classes

    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; }

    }

and below is my auto mapper configuration

 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
        }

This is the code for mapping in controller

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

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

When executing below error am getting below exception

"Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance."

Am using Asp.net core and automapper 6.1. Code is written based on below link generic list to automapper Please suggest a me solution tried a lot all getting same message

回答1:

For Mapper.Map<PagedList<CasteModel>>(result);, you need to initialize Mapper like below in Startup.cs

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

But, it it recommended to use Dependence Injection to resolve Mapper.

  1. Install Package 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" };
        }       
    }