自动映射器中一种类型到另一种类型的泛型类 [英] Generic class of one type to another type in auto mapper
问题描述
我有这个通用分页类:我想映射PagedList<Caste> to PagedList<CasteModel>
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);
}
以及模型和视图模型类
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.Map方法的任何调用,并且如果要使用ProjectTo或UseAsDataSource扩展方法,请确保您传入适当的IConfigurationProvider实例."
"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使用Asp.net核心和automapper 6.1.代码是根据以下链接编写的 自动映射的通用列表一个> 请建议我的解决方案尝试了很多都收到相同的消息
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
推荐答案
对于Mapper.Map<PagedList<CasteModel>>(result);
,您需要像下面的Startup.cs
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);
}
但是,建议使用Dependence Injection
来解析Mapper
.
But, it it recommended to use Dependence Injection
to resolve Mapper
.
-
安装软件包
AutoMapper.Extensions.Microsoft.DependencyInjection
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper(typeof(Startup));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
UseCase
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" };
}
}
这篇关于自动映射器中一种类型到另一种类型的泛型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!