⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectmapper.cs

📁 smark.data是基于C#开发的轻量级数据访问组件。 提供以下功能封: 1)提供了跨数据库类型操作能力 2)基于程线存储的数据访问上下文对象
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace Smark.Data.Mappings
{
    class ObjectMapper
    {
        public ObjectMapper(Type objtype)
        {
            Connection = new ConnectionAttribute(ConnectionType.Context);
            ObjectType = objtype;
            OnInit(objtype);
        }
        private void OnInit(Type type)
        {
            TableAttribute[] ta = NClay.CoreFun.GetTypeAttributes<TableAttribute>(type, false);
            IDAttribute[] id;
            ColumnAttribute[] col;
            AggregationAttribute[] aggr;
            PropertyCastAttribute[] pca;
            PropertyMapper pm;
            ValueAttribute[] val;
            if (ta.Length == 0)
                throw new SmarkException("对象不存在TableAttribute!");
            Table = string.IsNullOrEmpty(ta[0].Name) ? ObjectType.Name : ta[0].Name;
            foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                id = NClay.CoreFun.GetPropertyAttributes<IDAttribute>(pi, false);
                col = NClay.CoreFun.GetPropertyAttributes<ColumnAttribute>(pi, false);
                aggr = NClay.CoreFun.GetPropertyAttributes<AggregationAttribute>(pi, false);
                pca = NClay.CoreFun.GetPropertyAttributes<PropertyCastAttribute>(pi, false);
                val = NClay.CoreFun.GetPropertyAttributes<ValueAttribute>(pi, false);
                if (id.Length > 0)
                {
                    if (ID != null)
                        throw new SmarkException("重复描述IDAttribute!");
                    ID = new PropertyMapper();
                    ID.ColumnName = string.IsNullOrEmpty(id[0].Name) ? pi.Name : id[0].Name;
                    ID.Handler = new NClay.PropertyHandler(pi);
                    if (pca.Length > 0)
                        ID.Cast = pca[0];
                    if (val.Length > 0)
                        ID.Value = val[0];
                }
                if (col.Length > 0)
                {
                    pm = new PropertyMapper();
                    pm.ColumnName = string.IsNullOrEmpty(col[0].Name) ? pi.Name : col[0].Name;
                    pm.Handler = new NClay.PropertyHandler(pi);
                    if (aggr.Length > 0)
                        pm.Aggregation = aggr[0];
                    if (pca.Length > 0)
                        pm.Cast = pca[0];
                    if (val.Length > 0)
                        pm.Value = val[0];
                    if (Properties.Contains(pm))
                        throw new SmarkException("重复描述字段描述:" + pm.ColumnName + "!");
                    Properties.Add(pm);
                }
            }
            CreateSql();
        }
        private void CreateSql()
        {
            bool isgroup = false; ;
            StringBuilder select = new StringBuilder();
            StringBuilder group = new StringBuilder();
            if (ID != null)
            {
                select.Append("(" + ID.ColumnName + ") _" + ID.Handler.Property.Name);
                group.Append(ID.ColumnName);
            }
            foreach (PropertyMapper pm in Properties)
            {
                if (select.Length > 0)
                {
                    select.Append(",");
                }
                if (group.Length > 0)
                    group.Append(",");
                if (pm.Aggregation != null)
                {
                    if (pm.Aggregation.DISTINCT)
                    {
                        select.Append("(" + pm.Aggregation.Type + "(DISTINCT " + pm.ColumnName + ")) _" + pm.Handler.Property.Name);
                    }
                    else
                    {
                        select.Append("(" + pm.Aggregation.Type + "(" + pm.ColumnName + ")) _" + pm.Handler.Property.Name);
                    }
                    isgroup = true;
                }
                else
                {
                    select.Append("(" + pm.ColumnName + ") _" + pm.Handler.Property.Name);
                    group.Append(pm.ColumnName);
                }
                
            }
            Select ="Select "+ select.ToString()+" from " + Table;
            if (isgroup)
                GroupBy = group.ToString();
        }
        public Type ObjectType
        {
            get;
            set;
        }
        public string Select
        {
            get;
            set;
        }
        
        public string GroupBy
        {
            get;
            set;
        }
        public string Table
        {
            get;
            set;
        }
        public PropertyMapper ID
        {
            get;
            set;

        }
        public ConnectionAttribute Connection
        {
            get;
            set;

        }
        private IList<PropertyMapper> mProperties = new List<PropertyMapper>();
        public IList<PropertyMapper> Properties
        {
            get
            {
                return mProperties;
            }

        }
        private bool mLoadColumnIndex = false;
        public void ReaderToObject(System.Data.IDataReader reader, object obj)
        {
            if (!mLoadColumnIndex)
                SetColumnIndex(reader);
            object dbvalue;
            foreach (PropertyMapper pm in Properties)
            {
                if (reader[pm.ColumnIndex] != DBNull.Value)
                {
                    if (pm.Cast != null)
                    {
                        dbvalue = pm.Cast.ToProperty(reader[pm.ColumnIndex], pm.Handler.Property.PropertyType);
                    }
                    else
                    {
                        dbvalue= reader[pm.ColumnIndex];
                    }
                    pm.Handler.Set(obj, Convert.ChangeType(dbvalue, pm.Handler.Property.PropertyType));
                }
            }
            if (ID != null)
            {
                ID.Handler.Set(obj, Convert.ChangeType(reader[ID.ColumnIndex], ID.Handler.Property.PropertyType));
            }
        }
        
        private void SetColumnIndex(System.Data.IDataReader reader)
        {
            lock (this)
            {
                if (!mLoadColumnIndex)
                {
                    if(ID !=null)
                        ID.ColumnIndex = reader.GetOrdinal("_" + ID.Handler.Property.Name);
                    foreach (PropertyMapper pm in Properties)
                    {
                        pm.ColumnIndex = reader.GetOrdinal("_"+pm.Handler.Property.Name);
                    }
                    mLoadColumnIndex = true;
                }
            }
        }

        private static Dictionary<Type, ObjectMapper> mMapperTable = new Dictionary<Type, ObjectMapper>();
        public static ObjectMapper GetOM(Type type)
        {
           
                if (!mMapperTable.ContainsKey(type))
                {
                    lock (mMapperTable)
                    {
                        if (!mMapperTable.ContainsKey(type))
                        {
                            mMapperTable.Add(type, new ObjectMapper(type));
                        }
                    }
                }
                return mMapperTable[type];
           
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -