asp.net-mvc-4 – Dapper.net如何创建地图

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-4 – Dapper.net如何创建地图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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");
   }

我想为dapper.query函数创建这种映射.
请有人帮忙吗?
我想将自定义名称映射到实体属性名称.

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);
    }
}

当我这样称呼时:

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

现在,如果我使用Person实体,它不会在person实体内映射EntityBase属性.

解决方法

基本上,您需要编写一个ITypeMap和IMemberMap实现,它在内部可以基于您选择的任何规则(命名约定,如删除S等标识符,下划线等;属性;或自定义映射,如您的情况).

这是一个完整的可运行示例,可能会有所帮助:

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; } }
    }
}

猜你在找的asp.Net相关文章