开发者

Mapping lists in my Mapper class using using AutoMapper

I am using ASP.NET MVC 3 and AutoMapper.

In my category controller I return a list of categories and I want to map each category to a category view model which is used in my grid. I have my own mapper class method that accepts the source, source type and destination type and then does the mapping of single objects. How would I add and additional method so that I can map lists?

For example, if I want to map a开发者_运维知识库 single category to my category edit view model then the following mapping will be used:

Mapper.CreateMap<CategoryCreateViewModel, Category>();

In my controller I would map the 2 like this:

Category category = (Category)categoryMapper.Map(viewModel, typeof(CategoryCreateViewModel), typeof(Category));

This is what my mapping method looks like:

public class CategoryMapper : ICategoryMapper
{
     static CategoryMapper()
     {
          Mapper.CreateMap<Category, CategoryCreateViewModel>();
          Mapper.CreateMap<Category, CategoryViewModel>();
          Mapper.CreateMap<CategoryCreateViewModel, Category>();
     }

     public object Map(object source, Type sourceType, Type destinationType)
     {
          return Mapper.Map(source, sourceType, destinationType);
     }

     // I have been trying to get this right but not working
     //public object Map(object source, IEnumerable<Type> sourceType, IEnumerable<Type> destinationType)
     //{
     //     return Mapper.Map(sourceType, destinationType);
     //}
}

I want to add another method where I can map lists. How would I do this?


AutoMapper knows how to map lists, you don't need any extra code.

As long as you provide the mapping for the single object, it will work.

E.g:

Mapper.CreateMap<Source,Dest>();

Then:

var mappedCollection = Mapper.Map<IEnumerable<Source>,IEnumerable<Dest>>(items);

And it will work.


try

var maplist= Mapper.Map<IEnumerable<TSource>, IEnumerable<TDestination>>(IEnumerable<TSource>source);

i'm not sure but try something like

var category = CategoryMapper.Map(viewModel, CategoryCreateViewModel, Category);

public object Map(object source, IEnumerable<T> sourceType, IEnumerable<T> destinationType)
     {
         return Mapper.Map<sourceType,destinationType>(source);
     }


I wanted to use my IMapper methods like I mentioned above. With the help of Darin Dimitrov I got this to work. Here is my controller code:

public ActionResult JsonGetParentCategoryList()
{
     IEnumerable<Category> categoryList = categoryService.GetAll();

     // Mapping
     IEnumerable<CategoryViewModel> viewModelList = (IEnumerable<CategoryViewModel>)
          categoryMapper.Map(
               categoryList,
               typeof(IEnumerable<Category>),
               typeof(IEnumerable<CategoryViewModel>)
     );

     JsonEncapsulatorDto<CategoryViewModel> data = new JsonEncapsulatorDto<CategoryViewModel>
     {
          DataResultSet = viewModelList
     };

     return Json(data, JsonRequestBehavior.AllowGet);
}

It works with my current IMapper Map method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜