如何递归映射实体查看模型Automapper函数调用? [英] How to recursively map entity to view model with Automapper function call?

查看:374
本文介绍了如何递归映射实体查看模型Automapper函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

分拆我刚才的问题 - > <一个href=\"http://stackoverflow.com/questions/20571157/how-to-map-net-function-call-to-property-automatically\">How映射.NET函数调用来自动财产?

我有我要地图查看模式一些相关的实体对象,每个映射到视图模型需要调用一个函数映射。

棘手的部分是该实体函数返回需要被映射到它的视图模型的另一个实体。该流程有点像这样。


  

CartEntity> CartViewModel> CartEntity.GetCartItems()>
  CartViewModel.CartItems> CartEntity.GetCartItems()的GetItem()>
  CartViewModel.CartItems.Item>
  CartEntity.GetCartItems()的GetItem()。GetProduct()>
  CartViewModel.CartItems.Item.Product


下面是类,在这里我想递归构建填充了每个对象一组嵌套。

实体:

 公共类CartEntity {
    众长标识{搞定;组;}
    公开名单&LT; CartItem&GT; GetCartItems(){
          返回Repository.GetAll&LT; CartItem&GT;();
    }
}公共类CartItem {
    众长标识{搞定;组;}
    众长ITEMID {搞定;组;}
    公共项目的GetItem(){
          返回Repository.Get&LT;项目&GT;(ITEMID);
    }
}公共类项目{
    众长标识{搞定;组;}
    众长{产品编号搞定;组;}
    公共项目GetProduct(){
          返回Repository.Get&LT;产品及GT;(产品编号);
    }
}
公共类产品{
    众长标识{搞定;组;}
}

查看模式:

 公共类CartViewModel {
    众长标识{搞定;组;}
    公开名单&LT; CartItemViewModel&GT; {得到;组; }
}公共类CartItemViewModel {
    众长标识{搞定;组;}
    公共ItemViewModel项{搞定;组; }
}公共类ItemViewModel {
    众长标识{搞定;组;}
    公共ProductViewModel产品{搞定;组; }
}公共类ProductViewModel {
    众长标识{搞定;组;}
}


解决方案

如果你愿意改变你的模型了一下,跑AutoMapper应该是pretty容易的(如果不是,请跳到下到谷底)。

首先,由于您的评论表明,你需要从你的模型打电话给你的资料库,我会建议他们改变为只读属性。例如:

 公共类CartItem {
    众长标识{搞定;组;}    众长ITEMID {搞定;组;}    公共项目项目{
        获得{
            返回的GetItem();
        }
    }    私人项目的GetItem(){
          返回Repository.Get&LT;项目&GT;(ITEMID);
    }
}

另外,如果你不想打了数据库的你叫每个的时间 .Item ,那么你可以添加一个延迟加载支持字段:

 公共类CartItem {
    私人项目_item;    众长标识{搞定;组;}    众长ITEMID {搞定;组;}    公共项目项目{
        获得{
            归还物品 ?? (_item =的GetItem());
        }
    }    私人项目的GetItem(){
          返回Repository.Get&LT;项目&GT;(ITEMID);
    }
}

从那里,你可以设置AutoMapper是这样的:

  //一次性的,静态配置:
Mapper.CreateMap&LT; CartEntity,CartViewModel&GT;();
Mapper.CreateMap&LT; CartItem,CartItemViewModel&GT;();
Mapper.CreateMap&LT;项目,ItemViewModel&GT;();
Mapper.CreateMap&LT;产品,ProductViewModel&GT;();//运行映射器:
VAR cartViewModel = Mapper.Map&LT; CartViewModel&GT;(车);

如果你不想改变你的模型使用属性(或者,如果你不能),那么你可以在地图上,添加使用 ForMember 配置指定如何为每个项目做的:

  Mapper.CreateMap&LT; CartItem,CartItemViewModel&GT;()
      .ForMember(目标=&GT; target.Item,X =&GT; x.ResolveUsing(来源= GT; source.GetItem()));

Spin off of my earlier question -> How to map .NET function call to property automatically?

I have some related entity objects that I want to map to view models, and each mapping to a view model needs to call a function for the mapping.

The tricky part is that the entity function returns another entity which needs to be mapped to it's view model. The flow is somewhat like this.

CartEntity > CartViewModel > CartEntity.GetCartItems() > CartViewModel.CartItems > CartEntity.GetCartItems().GetItem() > CartViewModel.CartItems.Item > CartEntity.GetCartItems().GetItem().GetProduct() > CartViewModel.CartItems.Item.Product

Here are the classes, where I am trying to recursively build a nested set that is populated with each object.

Entities:

public class CartEntity {
    public long Id {get; set;}
    public List<CartItem> GetCartItems() {
          return Repository.GetAll<CartItem>();
    }
}

public class CartItem {
    public long Id {get; set;}
    public long ItemId {get ; set;}
    public Item GetItem() {
          return Repository.Get<Item>(ItemId);
    }
}

public class Item {
    public long Id {get; set;}
    public long ProductId {get; set;}
    public Item GetProduct() {
          return Repository.Get<Product>(ProductId);
    }
}
public class Product {
    public long Id {get; set;}
}

View Models:

public class CartViewModel {
    public long Id {get; set;}
    public List<CartItemViewModel> {get; set; }
}

public class CartItemViewModel {
    public long Id {get; set;}
    public ItemViewModel Item {get; set; }
}

public class ItemViewModel {
    public long Id {get; set;}
    public ProductViewModel Product {get; set; }
}

public class ProductViewModel {
    public long Id {get; set;}
}

解决方案

If you're willing to change your models a bit, running AutoMapper should be pretty easy (if not, skip down to the bottom).

First, since your comments indicate that you need to call your repositories from your models, I'd suggest changing them to read-only properties. For example:

public class CartItem {
    public long Id {get; set;}

    public long ItemId {get ; set;}

    public Item Item {
        get {
            return GetItem();
        }
    }

    private Item GetItem() {
          return Repository.Get<Item>(ItemId);
    }
}

Also, if you didn't want to hit the database every time you called .Item, then you could add a lazy-loaded backing field:

public class CartItem {
    private Item _item;

    public long Id {get; set;}

    public long ItemId {get ; set;}

    public Item Item {
        get {
            return _item ?? (_item = GetItem());
        }
    }

    private Item GetItem() {
          return Repository.Get<Item>(ItemId);
    }
}

From there, you can set up AutoMapper like this:

// one-time, static configuration:
Mapper.CreateMap<CartEntity, CartViewModel>();
Mapper.CreateMap<CartItem, CartItemViewModel>();
Mapper.CreateMap<Item, ItemViewModel>();
Mapper.CreateMap<Product, ProductViewModel>();

// running the mapper:
var cartViewModel = Mapper.Map<CartViewModel>(cart);

If you don't want to change your models to use properties (or if you can't), then you can add use the ForMember configuration on your maps to specify what to do for each item:

Mapper.CreateMap<CartItem, CartItemViewModel>()
      .ForMember(target => target.Item, x => x.ResolveUsing(source => source.GetItem()));

这篇关于如何递归映射实体查看模型Automapper函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆