Dapper.net comment créer une carte

asp.net-mvc-4 dapper

Question

 public CodeCustomMapper()
  {

    Map(f => f.Name).Column("SName");
    Map(f => f.Name).Column("TName");
    Map(f => f.EmailID).Column("wmail");
    Map(f => f.EmailID).Column("Email");
   }

Je veux créer ce genre de carte pour la fonction dapper.query.
S'il vous plaît quelqu'un peut-il aider?
Je souhaite mapper les noms de colonnes personnalisés aux noms de propriétés d'entité.


public class EntityBase
{
    public string CreatedBy { get; set; }
    public DateTime CreatedOn { get; set; }
}

public class Person :EntityBase
{
    public string SecurityAlias{ get; set; }
    public DateTime Identifier{ get; set; }
}

public class EntityBaseMap
{
    public EntityBaseMap()
    {
        var map = new CustomTypeMap<EntityBase>();
        map.Map("CreatingPerson", "CreatedBy");
        map.Map("CreatedDate", "CreatedOn");
        SqlMapper.SetTypeMap(map.Type, map);
    }
}

public class PersonMap
{
    public PersonMap()
    {
        var map = new CustomTypeMap<Person>();
        map.Map("security_alias", "SecurityAlias");
        map.Map("Id", "Identifier");
        SqlMapper.SetTypeMap(map.Type, map);
    }
}

Quand j'appelle ceci:

EntityBaseMap entityBaseMap = new EntityBaseMap();
PersonMap personMap = new PersonMap();

maintenant, si j'utilise l'entité Personne, cela ne fait pas correspondre les propriétés EntityBase à l'intérieur de l'entité personne.

Réponse acceptée

Fondamentalement, vous devez écrire une ITypeMap et IMemberMap , qui en interne pourrait être basée sur les règles que vous choisissez (conventions de dénomination telles que la suppression des identifiants S etc, des traits de soulignement, etc., ou des cartes personnalisées).

Voici un exemple complet pouvant être utile:

using Dapper;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;

class SomeType
{
    public string Bar { get; set; }
    public int Foo { get; set; }
}

static class Program
{
    static Program()
    {
        // only need to do this ONCE
        var oldMap = SqlMapper.GetTypeMap(typeof(SomeType));
        var map = new CustomTypeMap(typeof(SomeType), oldMap);
        map.Map("IFoo", "Foo");
        map.Map("SBar", "Bar");
        SqlMapper.SetTypeMap(map.Type, map);
    }

    static void Main()
    {
        using (var conn = new SqlConnection("Server=.;Database=master;Trusted_Connection=true"))
        {
            conn.Open();

            var row = conn.Query<SomeType>("select 123 as [IFoo], 'abc' as [SBar]").Single();
            Console.WriteLine(row.Foo);
            Console.WriteLine(row.Bar);
        }
    }

    class CustomTypeMap : SqlMapper.ITypeMap
    {
        private readonly Dictionary<string, SqlMapper.IMemberMap> members
            = new Dictionary<string, SqlMapper.IMemberMap>(StringComparer.OrdinalIgnoreCase);
        public Type Type { get { return type; } }
        private readonly Type type;
        private readonly SqlMapper.ITypeMap tail;
        public void Map(string columnName, string memberName)
        {
            members[columnName] = new MemberMap(type.GetMember(memberName).Single(), columnName);
        }
        public CustomTypeMap(Type type, SqlMapper.ITypeMap tail)
        {
            this.type = type;
            this.tail = tail;
        }
        public System.Reflection.ConstructorInfo FindConstructor(string[] names, Type[] types)
        {
            return tail.FindConstructor(names, types);
        }

        public SqlMapper.IMemberMap GetConstructorParameter(
            System.Reflection.ConstructorInfo constructor, string columnName)
        {
            return tail.GetConstructorParameter(constructor, columnName);
        }

        public SqlMapper.IMemberMap GetMember(string columnName)
        {
            SqlMapper.IMemberMap map;
            if (!members.TryGetValue(columnName, out map))
            { // you might want to return null if you prefer not to fallback to the
              // default implementation
                map = tail.GetMember(columnName);
            }
            return map;
        }
    }
    class MemberMap : SqlMapper.IMemberMap
    {
        private readonly MemberInfo member;
        private readonly string columnName;
        public MemberMap(MemberInfo member, string columnName)
        {
            this.member = member;
            this.columnName = columnName;
        }
        public string ColumnName { get { return columnName; } }
        public System.Reflection.FieldInfo Field { get { return member as FieldInfo; } }
        public Type MemberType { get {
            switch (member.MemberType)
            {
                case MemberTypes.Field: return ((FieldInfo)member).FieldType;
                case MemberTypes.Property: return ((PropertyInfo)member).PropertyType;
                default: throw new NotSupportedException();
            }
        } }
        public System.Reflection.ParameterInfo Parameter { get { return null; } }
        public System.Reflection.PropertyInfo Property { get { return member as PropertyInfo; } }
    }
}



Sous licence: CC-BY-SA with attribution
Non affilié à Stack Overflow
Est-ce KB légal? Oui, apprenez pourquoi
Sous licence: CC-BY-SA with attribution
Non affilié à Stack Overflow
Est-ce KB légal? Oui, apprenez pourquoi