csharp/1448376744/Dapper.Linq/src/Dapper.Common/Providers/DbMetaInfoProvider.cs

DbMetaInfoProvider.cs
using Dapper.Attributes;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

namespace Dapper
{
    /// 
    /// 数据库元信息提供程序
    /// 
    public interface IDbMetaInfoProvider
    {
        /// 
        /// 获取表的元信息
        /// 
        /// 
        /// 
        DbTableMetaInfo GetTable(Type type);
        /// 
        /// 获取字段的元信息
        /// 
        /// 
        /// 
        List GetColumns(Type type);
    }
   
    /// 
    /// 注解方案数据库元信息
    /// 
    public clast AnnotationDbMetaInfoProvider: IDbMetaInfoProvider
    {
        private static readonly ConcurrentDictionary _tables
            = new ConcurrentDictionary();

        private static readonly ConcurrentDictionary _columns
            = new ConcurrentDictionary();

        public DbTableMetaInfo GetTable(Type type)
        {
            return _tables.GetOrAdd(type, t =>
            {
                var name = t.Name;
                if (t.GetCustomAttributes(typeof(TableAttribute), true).FirstOrDefault() != null)
                {
                    var attribute = t.GetCustomAttributes(typeof(TableAttribute), true)
                        .FirstOrDefault() as TableAttribute;
                    name = attribute.Name;
                }
                var table = new DbTableMetaInfo()
                {
                    TableName = name,
                    CsharpName = t.Name
                };
                return table;
            });
        }

        public List GetColumns(Type type)
        {
            return _columns.GetOrAdd(type, t =>
            {
                var list = new List();
                var properties = type.GetProperties();
                foreach (var item in properties)
                {
                    var columnName = item.Name;
                    var isPrimaryKey = false;
                    var isDefault = false;
                    var isIdensaty = false;
                    var isNotMapped = false;
                    var isConcurrencyCheck = false;
                    var isComplexType = false;
                    if (item.GetCustomAttributes(typeof(ColumnAttribute), true).FirstOrDefault() != null)
                    {
                        var attribute = item.GetCustomAttributes(typeof(ColumnAttribute), true)
                            .FirstOrDefault() as ColumnAttribute;
                        columnName = attribute.Name;
                    }
                    if (item.GetCustomAttributes(typeof(PrimaryKeyAttribute), true).FirstOrDefault() != null)
                    {
                        isPrimaryKey = true;
                    }
                    if (item.GetCustomAttributes(typeof(IdensatyAttribute), true).FirstOrDefault() != null)
                    {
                        isIdensaty = true;
                    }
                    if (item.GetCustomAttributes(typeof(DefaultAttribute), true).FirstOrDefault() != null)
                    {
                        isDefault = true;
                    }
                    if (item.GetCustomAttributes(typeof(ConcurrencyCheckAttribute), true).FirstOrDefault() != null)
                    {
                        isConcurrencyCheck = true;
                    }
                    if (item.GetCustomAttributes(typeof(NotMappedAttribute), true).FirstOrDefault() != null)
                    {
                        isNotMapped = true;
                    }
                    if (item.GetCustomAttributes(typeof(ComplexTypeAttribute), true).FirstOrDefault() != null)
                    {
                        isComplexType = true;
                    }
                    list.Add(new DbColumnMetaInfo()
                    {
                        CsharpType = item.PropertyType,
                        IsDefault = isDefault,
                        ColumnName = columnName,
                        CsharpName = item.Name,
                        IsPrimaryKey = isPrimaryKey,
                        IsIdensaty = isIdensaty,
                        IsNotMapped = isNotMapped,
                        IsConcurrencyCheck = isConcurrencyCheck,
                        IsComplexType = isComplexType
                    });
                }
                return list;
            });
        }

    }

    /// 
    /// 表信息
    /// 
    public clast DbTableMetaInfo
    {
        /// 
        /// 数据库表名称
        /// 
        public string TableName { get; set; }
        /// 
        /// Csharp表名称
        /// 
        public string CsharpName { get; set; }
    }

    /// 
    /// 字段信息
    /// 
    public clast DbColumnMetaInfo
    {
        /// 
        /// 是否并发检查
        /// 
        public bool IsConcurrencyCheck { get; set; }
        /// 
        /// 是否默认值约束
        /// 
        public bool IsDefault { get; set; }
        /// 
        /// 是否是数据库字段
        /// 
        public bool IsNotMapped { get; set; }
        /// 
        /// 数据库字段名
        /// 
        public string ColumnName { get; set; }
        /// 
        /// Csharp字段名
        /// 
        public string CsharpName { get; set; }
        /// 
        /// Csharp类型
        /// 
        public Type CsharpType { get; set; }
        /// 
        /// 是否主键约束
        /// 
        public bool IsPrimaryKey { get; set; }
        /// 
        /// 是否是自增列
        /// 
        public bool IsIdensaty { get; set; }
        /// 
        /// 是否为计算列
        /// 
        public bool IsComplexType { get; set; }
    }
}