2016-05-05 14 views
0

Ich habe viele POCO-Klassen, die jeweils mehrere virtuelle Eigenschaften enthalten. Etwas wie folgt aus:Wie können alle virtuellen Eigenschaften von Klassen ausgeschlossen werden, die standardmäßig vom ClassMapper <T> übernommen wurden?

public class Policy 
{ 
    public int Id { get; set; } 
    public int EntityId { get; set; } 
    public int ProgramId { get; set; } 

    public string PolicyNumber { get; set; } 
    public DateTime EffectiveDate { get; set; } 
    public DateTime ExpirationDate { get; set; } 

    public virtual Entity Entity{ get; set; } 
    public virtual Program Program { get; set; } 
    public virtual ICollection<Transaction> Transactions { get; set; } 
} 

Dapper.Extensions Arbeit zu machen, ich brauche eine Zuordnung für jede dieser Klassen zu schreiben, was in Ordnung ist. Mein Problem ist, wenn es virtual Eigenschaften in einer Klasse gibt, müssen sie explizit als ignoriert werden, die ich immer vergessen zu tun.

public sealed class PolicyMapper : BaseMapper<Policy> 
{ 
    public PolicyMapper() 
    { 
     Map(p => p.Entity).Ignore(); 
     Map(p => p.Program).Ignore(); 
     Map(p => p.Transactions).Ignore(); 
     AutoMap(); 
    } 
} 

Was für mich groß sein würde, wenn die Dapper.Extensions Bibliothek automatisch virtuelle Eigenschaften ausschließen, wenn überhaupt, wenn sie auf die POCO-Klasse zugeordnet. Es gibt eine Erweiterung für Automapper, die etwas ähnliches tut (link). Gibt es eine Möglichkeit, das für Dapper.Extensions Bibliothek zu tun? Möglicherweise etwas in der Art:

public sealed class PolicyMapper : BaseMapper<Policy> 
{ 
    public PolicyMapper() 
    { 
     IgnoreAllVirtual(); 
     AutoMap(); 
    } 
} 

Antwort

0

Ich fand meine eigene Lösung. Da alle meine Mapping-Klassen von BaseMapper Klasse ableiten, entschied ich mich AutoMap() -Methode außer Kraft zu setzen, die virtuelle Eigenschaften ausschließen:

public class BaseMapper<T> : ClassMapper<T> where T : BaseClass 
{ 
    public BaseMapper() 
    { 

    } 

    protected override void AutoMap() 
    { 
     CustomAutoMap(null); 
    } 

    private void CustomAutoMap(Func<Type, PropertyInfo, bool> canMap) 
    { 
     Type type = typeof(T); 
     bool hasDefinedKey = Properties.Any(p => p.KeyType != KeyType.NotAKey); 
     PropertyMap keyMap = null; 

     foreach (var propertyInfo in type.GetProperties()) 
     { 
      // Exclude virtual properties 
      if (propertyInfo.GetGetMethod().IsVirtual) 
      { 
       continue; 
      } 

      if (Properties.Any(p => p.Name.Equals(propertyInfo.Name, StringComparison.InvariantCultureIgnoreCase))) 
      { 
       continue; 
      } 

      if ((canMap != null && !canMap(type, propertyInfo))) 
      { 
       continue; 
      } 

      PropertyMap map = Map(propertyInfo); 
      if (!hasDefinedKey) 
      { 
       if (string.Equals(map.PropertyInfo.Name, "id", StringComparison.InvariantCultureIgnoreCase)) 
       { 
        keyMap = map; 
       } 

       if (keyMap == null && map.PropertyInfo.Name.EndsWith("id", true, CultureInfo.InvariantCulture)) 
       { 
        keyMap = map; 
       } 
      } 
     } 

     if (keyMap != null) 
     { 
      keyMap.Key(PropertyTypeKeyTypeMapping.ContainsKey(keyMap.PropertyInfo.PropertyType) 
       ? PropertyTypeKeyTypeMapping[keyMap.PropertyInfo.PropertyType] 
       : KeyType.Assigned); 
     } 
    } 
} 

}