如果是複雜Class要Mapper時
1 | public class Category |
Category要Mapping到CategoryViewModel
1 | public class CategoryViewModel |
有兩種做法,結果都會是一樣的
```csharp
Category category = new Category()
{
id = 1,
products = new List()
{
new Product()
{
id = 1,
name = “西遊戲”,
qantity =10,
},
new Product()
{
id = 2,
name = “三國志”,
qantity =30,
},
new Product()
{
id = 3,
name = “鹿鼎記”,
qantity =50,
}
}
};Mapper.CreateMap<Category, CategoryViewModel>()
.ForMember(d => d.id, o => o.MapFrom(s => s.id))
.ForMember(d => d.books, o => o.MapFrom(s => s.products));Mapper.CreateMap<Product, Book>()
.ForMember(d => d.title, o => o.MapFrom(s => s.name))
.ForMember(d => d.number, o => o.MapFrom(s => s.qantity));
var viewModel = Mapper.Map
viewModel.Dump();
1 |
|