2016-07-27 62 views
0
public class Trimmer<TModel> 
    { 
     public Trimmer() 
     { 
      Mapper.Initialize(c => 
      { 
       c.CreateMap<string, string>().ConvertUsing(s => string.IsNullOrEmpty(s) ? s : s.Trim()); 
       c.CreateMap<TModel, TModel>(); 
      }); 
     } 

     /// <summary> 
     /// Function take List of object of type TModel what supplied during initalization and applied trim on every property which is string. 
     /// </summary> 
     /// <param name="models">An model object of type TModel</param> 
     /// <returns>List of objects of type TModel with string properties that are trimmed (leading and trailing spaces removed)</returns> 
     public List<TModel> StringTrimmer(List<TModel> models) 
     { 
      if (models == null) 
      { 
       return null;    
      } 
      var modelList = models.Select(StringTrimmer).ToList(); 
      return modelList; 
     } 

     /// <summary> 
     /// Function take object of type T which one supply during Initalization and applied trim on every property which is string. 
     /// </summary> 
     /// <param name="model">An model object of Type TModel</param> 
     /// <returns>Object of type TModel with string properties that are trimmed (leading and trailing spaces removed)</returns> 
     public TModel StringTrimmer(TModel model) 
     { 
      Mapper.AssertConfigurationIsValid();`enter code here` 
      var mappedObj = Mapper.Map<TModel,TModel>(model); 
      return mappedObj; 
     } 

我创建了一个称为Trimmer的泛型类,它带有称为StringTrimmer的重载方法。这些方法的意图是使用Automapper修剪Tmodel对象属性的任何空间。它的工作很好,但有时则这些方法我得到了以下错误:使用自动映射器修剪对象属性期间的零星错误

Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type.

当不应该发生,因为我同一个对象类型转换为同一对象类型。

+0

什么是TModel的是造成这个错误? –

+0

TModel只是泛型类型。在我的情况下,它只是我想要修剪的对象属性的类名。例如,如果我有一个“甲板”类,那么我会做“var obj = new Trimmer ()”。 –

+0

@TimothyGhanem我确实发现了问题并回答了问题,因为我偶然发现错误。这是代码会给我错误的许多可能的测试案例之一。解决方案是移动Mapper.AssertConfigurationIsValid();在Mapper.Initialize中 –

回答

0
Code 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 
using Excel = Microsoft.Office.Interop.Excel; 
using AutoMapper; 

namespace ConsoleApplication2 
{ 
public class Data 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 
public class DataDto 
{ 
    public string FirstName { get; set; } 
    public string No { get; set; } 
} 
public class ObjectModifier<TModel> 
{ 
    public ObjectModifier() 
    { 
     Mapper.Initialize(c => 
     { 
      c.CreateMap<string, string>().ConvertUsing(s => string.IsNullOrEmpty(s) ? s : s.Trim()); 
      c.CreateMap<TModel, TModel>(); 
      Mapper.AssertConfigurationIsValid(); 
     }); 
    } 

    public TModel StringTrimmer(TModel model) 
    { 
     Mapper.AssertConfigurationIsValid(); 
     var mappedObj = Mapper.Map<TModel, TModel>(model); 
     return mappedObj; 
    } 
} 

public static class ObjectConverter 
{ 
    public static T2 ToObject<T1, T2>(T1 val) 
    { 
     return Mapper.DynamicMap<T1, T2>(val); 
    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     var doc = new Data() 
     { 
      FirstName = "Foo ", 
      LastName = "ooF" 
     }; 
     ObjectModifier<Data> objMod = new ObjectModifier<Data>(); 
     var result = objMod.StringTrimmer(doc); //Good 
     var result2 = ObjectConverter.ToObject<Data, DataDto>(doc); //Good 
     var result3 = objMod.StringTrimmer(doc); //Error 
    } 
} 

}

@TimothyGhanem我发现这个问题了,因为我使用Mapper.AssertConfigurationIsValid();在StringTrimmer开始时,它应该位于Mapper.Initialize中的CreateMap之后。根据为什么它的零星工作

  1. 案例 按照下面的代码。当main方法调用ObjectModifier类来修剪空间时,数据类属性Mapper.AssertConfigurationIsValid()将查找将数据映射到Data,这意味着Map的源和目标中的相同属性因此会通过Validation。
  2. 案例 现在在下一行我们使用ObjectConverter中提到的一个新的Map,并将Data转换为Datadto工作正常。
  3. 现在在下一行再次尝试使用StringTrimmer进行修剪,但现在当执行到Mapper.AssertConfigurationIsValid()时,它将无效,因为Mapper保存了来自ObjectConverter的前一个Map的上下文,现在Data和Datadto有一些属性与Datadto中的“no”中的“lastName”不同。因此失败。这是我提出的一个案例,它适合我寻求偶发性失败的一些解释。

感谢