代码之家  ›  专栏  ›  技术社区  ›  Stian

如何在具有简单属性的实体模型和具有列表的viewmodel之间映射?

  •  0
  • Stian  · 技术社区  · 6 年前

    如何绘制地图 List<this entity model> :

    public class ProductCategory
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public int SortOrder { get; set; }
        public int? ParentId { get; set; }
    }
    

    。。。以及此viewmodel(实体类属性 Title 不应映射到viewmodel属性 标题 ,但要 Categories.Title ):

    public class ViewModelProductCategories
    {
        public List<ViewModelProductCategory> Categories { get; set; }
        public string Title { get; set; }// Used to create new category the Index view
        // User Interface parameter switches:
        public bool CanCreateCategory { get; set; }
        public bool CanCreateProduct { get; set; }
    }
    

    这是引用的viewmodel ViewModelProductCategories 。重要的是,我在绘制地图时也要获得导航属性:

    public class ViewModelProductCategory
    {
        public int Id { get; set; }
        public int? ParentId { get; set; }
        public string Title { get; set; }
        public int SortOrder { get; set; }
        // Nav.props:
        public List<ViewModelFrontPageProduct> FrontPageProducts { get; set; }
        public ViewModelProductCategory ParentCategory { get; set; }
        public ViewModelProductCategories Children { get; set; }
        public List<ViewModelProduct> Products { get; set; }
    }
    

    这是我以前的映射,它直接映射到 ViewModelProductCategory :

    CreateMap<ProductCategory, ViewModelProductCategory>()
                .ForMember(dst => dst.Products, opt => opt.MapFrom(
                    src => src.ProductInCategory.Select(pc => pc.Product)));
    

    这是我可以访问的 Products 通过 ProductInCategory 在每个类别中。我仍然需要它,但类别现在将在 List

    2 回复  |  直到 6 年前
        1
  •  2
  •   Georg Patscheider    6 年前

    如果你没有正确理解你的问题,你想通过一个 ProductCategory 自动映射并获取 ViewModelProductCategories Categories 收集

    1) 定义映射 ProductCategory产品类别 -&燃气轮机; ViewModelProductCategory

    CreateMap<ProductCategory, ViewModelProductCategory>();
    

    2) 使用此映射解析中的集合 ViewModelProductCategories视图模型产品类别

    CreateMap<ProductCategory, ViewModelProductCategories>()
        .ForMember(vm => vm.Categories, 
                   o => o.ResolveUsing(src => Mapper.Map<List<ViewModelProductCategory>>(src))
        );
    

    这假设映射 ProductCategory产品类别 -&燃气轮机; ViewModelProductCategory视图模型产品类别 为静态Automapper实例配置。请注意,如果Automapper知道如何映射元素,他将自动创建集合。

    用法:

    var cat = new ProductCategory();
    ViewModelProductCategories vm = Mapper.Map<ViewModelProductCategories>(cat);
    
        2
  •  1
  •   TheGeneral    6 年前

    您可以使用AutoMapper,也可以 Project / Select 进入您的 ViewModals

    var productCategories = context.Where(<my filter>)
                   .OrderBy(x => x.SortOrder)
                   .Select(x => new ViewModelProductCategory()
                     {
                        Id = x.Id,
                        Title = x.Title,
                        SortOrder = x.SortOrder, 
                        ParentId = x.ParentId
                     }); 
    
    var viewModelProductCategories  = new ViewModelProductCategories()
                    {
                       Categories  = productCategories 
                       ...
                       // other shenanigans here  
                    };