Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

c# - Generic class of one type to another type in auto mapper

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...