2010-10-13 23 views
3

Dans mon application MVC 2, j'ai une méthode typique qui appelle un service Web, construit un objet de données JSON et le renvoie à la vue.Comment mapper poco à JSON en utilisant Automapper

Tout fonctionne bien, mais je me demandais s'il y avait un moyen de faire le mappage avec Automapper afin que je puisse supprimer le code moche de mon contrôleur. Merci à l'avance

Voici ma méthode d'action

public virtual ActionResult AllErrors(string sidx, string sord, 
             int page=1, int rows=10) 
    { 
     var pageSize = rows; 
     var pageNumber = page; 
     var orderBy = string.Format("{0} {1}", sidx, sord); 

     var result = errorService.GetPagedOpenErrors(pageSize, page, orderBy); 
     var errors = new List<IngestionErrorDataContract>(result.IngestionErrors); 
     var totalPages = (int) Math.Ceiling(result.TotalRows/(float) pageSize); 

     int index = 0; 
     var list = new List<object>(); 
     errors.ForEach(e => list.Add(
           new { 
             i = index++, 
             cell = new[] 
             { 
              e.IngestionErrorId.ToString(), 
              e.RunId.ToString(), 
              e.ProcessDate.ToShortDateString(), 
              e.Status, 
              e.ErrorDetails 
             } 
            })); 

     var jsonData = new 
          { 
           total = totalPages, 
           page = pageNumber, 
           records = result.TotalRows, 
           rows = list.ToArray() 
          }; 

     return Json(jsonData, JsonRequestBehavior.AllowGet); 
    } 
+0

extraire le code de mappage dans une classe séparée et u peut-être se retrouver avec quelque chose de générique – Omu

Répondre

2

Je l'ai résolu en utilisant la méthode ConstructUsing de AutoMapper. Voici ma carte

public void CreateMap() 
    { 
     Mapper.CreateMap<List<IngestionErrorDataContract>, object[]>() 
      .ConvertUsing(
       errors => 
        { 
         int index = 0; 
         var list = new List<object>(); 
         errors.ForEach(e => list.Add(
          new 
           { 
            i = index++, 
            cell = new[] 
               { 
                e.IngestionErrorId.ToString(), 
                e.RunId.ToString(), 
                e.ProcessDate.ToShortDateString(), 
                e.Status, 
                e.ErrorDetails 
               } 
           })); 
         return list.ToArray(); 
        }); 
    } 

et est ma méthode d'action ici maintenant

public virtual ActionResult AllErrors(string sidx, string sord, int page=1, int rows=10) 
    { 
     var pageSize = rows; 
     var pageNumber = page; 
     var orderBy = string.Format("{0} {1}", sidx, sord); 

     var result = errorService.GetPagedOpenErrors(pageSize, page, orderBy); 
     var errors = new List<IngestionErrorDataContract>(result.IngestionErrors); 
     var totalPages = (int) Math.Ceiling(result.TotalRows/(float) pageSize); 

     var jsonData = new 
          { 
           total = totalPages, 
           page = pageNumber, 
           records = result.TotalRows, 
           rows = mapper.Map<List<IngestionErrorDataContract>,object[]>(errors) 
          }; 

     return Json(jsonData, JsonRequestBehavior.AllowGet); 
    } 

beaucoup mieux, je pense que