using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Reflection; using OS.Common.Extention.Volidate; #region Copyright (C) 2014 OS系列开源项目 /*    文件功能描述:验证属性attribute    创建人:王超 创建人Email:1985088337@qq.com 创建日期:2014.08.25    修改描述: */ #endregion namespace OS.Common.Extention.DTO { public static class DTOExtetion { private static ConcurrentDictionary> propertyDirs =new ConcurrentDictionary>(); /// /// 从Rquest等NameValueCollection列表对象中获取实体信息 /// /// /// public static TModel SetToModel(this NameValueCollection valueCollection) where TModel : class, new() { TModel tModel = new TModel(); var properties = GetColumnAttributes( typeof (TModel) ); foreach (var propertyColumnInfo in properties) { var propertyInfo = propertyColumnInfo.Value.PropertyInfo; var value = valueCollection[propertyColumnInfo.Key]; if (!propertyColumnInfo.Value.IsIgnore &&!string.IsNullOrEmpty( value )) { propertyInfo.SetValue(tModel, value.ChangeToType(propertyInfo.PropertyType), null); } } return tModel; } ///// ///// 从DataReader中获取实体信息 ///// ///// ///// //public static TModel SetToModel(this IDataReader dr) where TModel : class ,new() //{ // TModel tModel=new TModel(); // var properties = GetColumnAttributes( typeof (TModel) ); // for (int i = 0; i < dr.FieldCount; i++) // { // string drFieldName = dr.GetName( i ); // if (properties.ContainsKey( drFieldName ) && !dr.IsDBNull( i )) // { // var propertyColumnInfo = properties[drFieldName]; // if (!propertyColumnInfo.IsIgnore) // { // var propertyInfo = propertyColumnInfo.PropertyInfo; // propertyInfo.SetValue(tModel, dr[i].ChangeToType(propertyInfo.PropertyType), null); // } // } // } // return tModel; //} #region 属性处理 /// /// 获取列附加属性列表 /// /// /// private static IDictionary GetColumnAttributes(Type type) { IDictionary propertyList; propertyDirs.TryGetValue(type.FullName, out propertyList); if (propertyList != null) return propertyList; var properties = TypeHelper.GetProperties(type); propertyList = new Dictionary(properties.Length); foreach (PropertyInfo fd in properties) { var properColumnInfo = new PropertyColumnInfo(); properColumnInfo.Name = fd.Name; properColumnInfo.PropertyInfo = fd; var attrs = TypeHelper.GetPropertiAttributes(type.FullName, fd, typeof(BaseClumnAttribute)) .Select(attr => attr as BaseClumnAttribute) .ToList(); attrs.ForEach(attr => { properColumnInfo.IsAuto = attr.IsAuto || properColumnInfo.IsAuto; properColumnInfo.IsIgnore = attr.IsIgnore || properColumnInfo.IsIgnore; if (!string.IsNullOrEmpty(attr.Alias)) properColumnInfo.Alias = attr.Alias; }); properColumnInfo.Alias = properColumnInfo.Alias ?? fd.Name; if (!propertyList.ContainsKey(properColumnInfo.Alias)) { propertyList.Add(properColumnInfo.Alias, properColumnInfo); } } propertyDirs.TryAdd(type.FullName, propertyList); return propertyList; } #endregion #region 数据转化部分 /// /// 转换到指定类型 /// /// /// /// public static Object ChangeToType(this String obj, Type type) { if (obj == null) { return default(Type); } return ((object)obj).ChangeToType(type); } /// /// 转换到指定类型 /// /// /// /// public static Object ChangeToType(this Object obj, Type type) { if (obj == null) { return default(Type); } object result; try { if (type == typeof (DateTime?)) { DateTime date; result = DateTime.TryParse(obj.ToString(), out date) ? date : default(DateTime?); } else if (type == typeof(int?)) { int i; result = int.TryParse(obj.ToString(), out i) ? i : default(int?); } else { result = type.IsEnum ? Enum.ToObject(type, Convert.ToInt64(obj)) : Convert.ChangeType(obj, type); } } catch (Exception) { result = default(Type); } return result; } #endregion #region 创建数据参数部分 ///// ///// 从实体中获取 所有数据库参数和值 ///// ///// ///// //public static DbParameters GetDbParameteres(this TModel tModel, DataBaseType type=DataBaseType.SqlServer) where TModel : class //{ // DbParameters parameters=new DbParameters(type); // var properties = GetColumnAttributes(typeof(TModel)); // foreach (var pro in properties) // { // var pcInfo = pro.Value; // if (!pcInfo.IsIgnore && !pcInfo.IsAuto) // { // parameters.AddInParameter(pcInfo.Alias, // GetPropertityValue(pcInfo.PropertyInfo, tModel)); // } // else if (pcInfo.IsAuto) // { // parameters.AddOutParameter(pcInfo.Alias,DbType.Int64); // } // } // return parameters; //} ///// ///// 设置SqlServer数据库插入参数和语句 ///// ///// ///// //public static void SetSqlServerInsertPara(this DbCommand com, TModel tModel, string tableName) //{ // DbParameters parameters = new DbParameters(DataBaseType.SqlServer); // var properties = GetColumnAttributes(typeof (TModel)); // StringBuilder strColumns = new StringBuilder(); // StringBuilder strValues = new StringBuilder(); // string strAuto = string.Empty; // foreach (var pro in properties) // { // var pcInfo = pro.Value; // if (!pcInfo.IsIgnore && !pcInfo.IsAuto) // { // var para = parameters.AddInParameter(pcInfo.Alias, // DbParameters.ConvertToDbType(pcInfo.PropertyInfo.PropertyType), // GetPropertityValue(pcInfo.PropertyInfo, tModel)); // if (strColumns.Length > 0) // strColumns.Append(","); // if (strValues.Length > 0) // strValues.Append(","); // strColumns.Append("[").Append(pcInfo.Alias).Append("]"); // strValues.Append(para.ParameterName); // } // else if (pcInfo.IsAuto) // { // var para = parameters.AddOutParameter(pcInfo.Alias, DbType.Int64); // strAuto = string.Format(" set {0}=@@identity", para.ParameterName); // } // } // com.CommandType = CommandType.Text; // com.CommandText = string.Format("insert into {0}({1}) values ({2}) {3}", tableName, strColumns, strValues, // strAuto); // com.Parameters.AddRange(parameters.ToArray()); //} private static object GetPropertityValue(PropertyInfo proper, TModel tModel) { object obj = proper.GetValue(tModel, null); if (proper.PropertyType.IsEnum) { obj = (int) obj; } return obj; } #endregion } #region 数据库参数定义 //public enum DataBaseType //{ // SqlServer, // Oracle //} //public static class DataBase //{ // public static DbProviderFactory GetDbProvider(DataBaseType type=DataBaseType.SqlServer) // { // DbProviderFactory factory = null; // switch (type) // { // case DataBaseType.SqlServer: // factory = DbProviderFactories.GetFactory("System.Data.SqlClient"); // break; // case DataBaseType.Oracle: // factory = DbProviderFactories.GetFactory("System.Data.OracleClient"); // break; // } // if (factory == null) // { // throw new ArgumentNullException("type", "DbProviderFactory do not support!"); // } // return factory; // } //} ///// ///// 数据库参数封装 ///// //public class DbParameters : Collection //{ // private readonly DataBaseType _dataBaseType; // /// // /// // /// // /// // public DbParameters(DataBaseType type) // { // _dataBaseType = type; // } // /// // /// 添加输入参数 // /// // /// // /// // /// // public DbParameter AddInParameter(string name, DbType dbType) // { // return AddParameter(name, dbType, ParameterDirection.Input, String.Empty, DataRowVersion.Default, null); // } // /// // /// 添加输入参数 // /// // /// // /// // /// // public DbParameter AddInParameter(string name, object value) // { // return AddParameter(name, ConvertToDbType(value==null?typeof(object):value.GetType()), ParameterDirection.Input, String.Empty, DataRowVersion.Default, value); // } // /// // /// 添加输入参数 // /// // /// // /// // /// // /// // public DbParameter AddInParameter(string name, DbType dbType, object value) // { // return AddParameter(name, dbType, ParameterDirection.Input, String.Empty, DataRowVersion.Default, value); // } // /// // /// 添加一个新的参数 // /// // /// 参数名称,不带前缀的,例如直接 “name” // /// 参数类型 // /// 参数大小 // /// 参数值 // public DbParameter AddInParameter(string name, DbType dbType, int size, object value) // { // return AddParameter(name, dbType, size, ParameterDirection.Input, true, 0, 0, String.Empty, DataRowVersion.Default, // value); // } // /// // /// 添加输入参数 // /// // /// // /// // /// // /// // /// // public DbParameter AddInParameter(string name, DbType dbType, string sourceColumn, DataRowVersion sourceVersion) // { // return AddParameter(name, dbType, 0, ParameterDirection.Input, true, 0, 0, sourceColumn, sourceVersion, null); // } // /// // /// 添加输出参数 // /// // /// // /// // public DbParameter AddOutParameter(string name, DbType dbType) // { // return AddParameter(name, dbType, 0, ParameterDirection.Output, true, 0, 0, String.Empty, DataRowVersion.Default, // DBNull.Value); // } // /// // /// 添加输出参数 // /// // /// // /// // /// // public DbParameter AddOutParameter(string name, DbType dbType, int size) // { // return AddParameter(name, dbType, size, ParameterDirection.Output, true, 0, 0, String.Empty, DataRowVersion.Default, // DBNull.Value); // } // /// // /// // /// // /// // /// // /// // /// // /// // /// // public DbParameter AddParameter(string name, DbType dbType, ParameterDirection direction, string sourceColumn, // DataRowVersion sourceVersion, object value) // { // return AddParameter(name, dbType, 0, direction, false, 0, 0, sourceColumn, sourceVersion, value); // } // /// // /// 添加参数 // /// // /// // /// // /// // /// // /// // /// // /// // /// // /// // /// // protected DbParameter AddParameter(string name, DbType dbType, int size, ParameterDirection direction, bool nullable, // byte precision, // byte scale, string sourceColumn, DataRowVersion sourceVersion, object value) // { // DbParameter parameter = CreateParameter(name, dbType, size, direction, nullable, precision, scale, // sourceColumn, sourceVersion, value); // Add(parameter); // return parameter; // } // /// // /// 创建参数 // /// // /// // /// // /// // /// // /// // /// // /// // /// // /// // /// // /// // protected internal DbParameter CreateParameter(string name, DbType dbType, int size, ParameterDirection direction, // bool nullable, byte precision, // byte scale, string sourceColumn, DataRowVersion sourceVersion, object value) // { // DbParameter para = DataBase.GetDbProvider(_dataBaseType).CreateParameter(); // para.ParameterName = string.Concat(PreParaToken, name); // ConfigureParameter(para, dbType, size, direction, nullable, precision, scale, sourceColumn, sourceVersion, // value); // return para; // } // /// // /// 配置参数信息 // /// // /// // /// // /// // /// // /// // /// // /// // /// // /// // /// // protected virtual void ConfigureParameter(DbParameter param, DbType dbType, int size, // ParameterDirection direction, bool nullable, byte precision, byte scale, string sourceColumn, // DataRowVersion sourceVersion, object value) // { // param.DbType = dbType; // param.Size = size; // param.Value = value ?? DBNull.Value; // param.Direction = direction; // param.IsNullable = nullable; // param.SourceColumn = sourceColumn; // param.SourceVersion = sourceVersion; // } // /// // /// 索引器 // /// // /// // /// // public DbParameter this[string name] // { // get // { // if (string.IsNullOrEmpty(name)) // { // throw new ArgumentNullException("ParameterName","查询的属性名称不能为空!"); // } // return Items.FirstOrDefault(dbp => dbp.ParameterName ==string.Concat(PreParaToken, name)); // } // } // private string _preParaToken; // /// // /// 当前数据的库操作符号 // /// // public String PreParaToken // { // get // { // if (string.IsNullOrEmpty(_preParaToken)) // { // switch (_dataBaseType) // { // case DataBaseType.SqlServer: // _preParaToken = "@"; // break; // //case DataBaseType.Oracle: // // _preParaToken = ":"; // // break; // default: // _preParaToken = string.Empty; // break; // } // } // return _preParaToken; // } // } // #region 数据库类型和.net类型映射 // /// // /// 数据库类型和.net类型映射 // /// // private static Dictionary typeMap=new Dictionary(); // /// // /// // /// // static DbParameters() // { // typeMap[typeof(byte)] = DbType.Byte; // typeMap[typeof(sbyte)] = DbType.SByte; // typeMap[typeof(short)] = DbType.Int16; // typeMap[typeof(ushort)] = DbType.UInt16; // typeMap[typeof(int)] = DbType.Int32; // typeMap[typeof(uint)] = DbType.UInt32; // typeMap[typeof(long)] = DbType.Int64; // typeMap[typeof(ulong)] = DbType.UInt64; // typeMap[typeof(float)] = DbType.Single; // typeMap[typeof(double)] = DbType.Double; // typeMap[typeof(decimal)] = DbType.Decimal; // typeMap[typeof(bool)] = DbType.Boolean; // typeMap[typeof(string)] = DbType.String; // typeMap[typeof(char)] = DbType.StringFixedLength; // typeMap[typeof(Guid)] = DbType.Guid; // typeMap[typeof(DateTime)] = DbType.DateTime; // typeMap[typeof(DateTimeOffset)] = DbType.DateTimeOffset; // typeMap[typeof(TimeSpan)] = DbType.Time; // typeMap[typeof(byte[])] = DbType.Binary; // typeMap[typeof(byte?)] = DbType.Byte; // typeMap[typeof(sbyte?)] = DbType.SByte; // typeMap[typeof(short?)] = DbType.Int16; // typeMap[typeof(ushort?)] = DbType.UInt16; // typeMap[typeof(int?)] = DbType.Int32; // typeMap[typeof(uint?)] = DbType.UInt32; // typeMap[typeof(long?)] = DbType.Int64; // typeMap[typeof(ulong?)] = DbType.UInt64; // typeMap[typeof(float?)] = DbType.Single; // typeMap[typeof(double?)] = DbType.Double; // typeMap[typeof(decimal?)] = DbType.Decimal; // typeMap[typeof(bool?)] = DbType.Boolean; // typeMap[typeof(char?)] = DbType.StringFixedLength; // typeMap[typeof(Guid?)] = DbType.Guid; // typeMap[typeof(DateTime?)] = DbType.DateTime; // typeMap[typeof(DateTimeOffset?)] = DbType.DateTimeOffset; // typeMap[typeof(TimeSpan?)] = DbType.Time; // } // #endregion // /// // /// 将.net类型转成数据库类型 // /// // /// // /// // internal static DbType ConvertToDbType(Type type) // { // if (type.IsEnum) // { // return DbType.Int32; // } // if (typeMap.ContainsKey(type)) // { // return typeMap[type]; // } // return DbType.String; // } //} #endregion }