📄 myormframework.cs
字号:
// 读取数据字段值
object v = reader.GetValue( field.FieldIndex );
v = field.FromDataBase( v );
// 设置对象属性值
field.Property.SetValue( ObjInstance , v , null );
FieldCount ++ ;
}//foreach
return FieldCount ;
}
/// <summary>
/// 检查若干个对象的数据库绑定状态,若检查不通过则抛出异常。
/// </summary>
/// <param name="Objects">对象列表</param>
/// <param name="CheckKey">是否检查定义了关键字段属性</param>
private void CheckBindInfo( System.Collections.IEnumerable Objects , bool CheckKey )
{
Type LastType = null ;
foreach( object obj in Objects )
{
if( obj == null )
{
throw new Exception("对象集合中出现空引用");
}
Type t = obj.GetType();
if( t.Equals( LastType ) == false )
{
LastType = t ;
CheckBindInfo( t , CheckKey );
}
}
}
/// <summary>
/// 检查一个对象类型的数据库绑定状态,若检查不通过则抛出异常。
/// </summary>
/// <param name="t">对象类型</param>
/// <param name="CheckKey">是否检查定义了关键字段属性</param>
private void CheckBindInfo( Type t , bool CheckKey )
{
TableBindInfo table = this.GetBindInfo( t );
if( table == null )
{
throw new Exception("类型 " + t.FullName + " 未映射到数据库");
}
if( CheckKey )
{
foreach( FieldBindInfo field in table.Fields )
{
if( field.Attribute.Key )
{
return ;
}
}
throw new Exception("类型 " + t.FullName + " 未定义关键字段");
}
}
/// <summary>
/// 在内部缓存的映射信息列表,此处为了提高速度。
/// </summary>
private static System.Collections.Hashtable myBufferedInfos = new System.Collections.Hashtable();
/// <summary>
/// 获得指定类型的数据表映射信息对象
/// </summary>
/// <param name="ObjectType">对象类型</param>
/// <returns>获得的映射信息对象</returns>
/// <remarks>
/// 本函数内部使用了 myBufferedInfos 来缓存信息,提高性能。
/// </remarks>
private TableBindInfo GetBindInfo( Type ObjectType )
{
if( ObjectType == null )
{
throw new ArgumentNullException("OjbectType");
}
// 查找已缓存的映射信息对象
TableBindInfo info = ( TableBindInfo ) myBufferedInfos[ ObjectType ] ;
if( info != null )
{
return info ;
}
// 若未找到则创建新的映射信息对象
BindTableAttribute ta = ( BindTableAttribute ) System.Attribute.GetCustomAttribute(
ObjectType , typeof( BindTableAttribute ));
if( ta == null )
{
return null;
}
TableBindInfo NewInfo = new TableBindInfo();
NewInfo.ObjectType = ObjectType ;
NewInfo.Attribute = ta ;
NewInfo.TableName = ta.Name ;
if( NewInfo.TableName == null || NewInfo.TableName.Trim().Length == 0 )
{
// 若在特性中没有指明绑定的表名则使用默认的对象类型名称
NewInfo.TableName = ObjectType.Name ;
}
System.Text.StringBuilder myFieldList = new System.Text.StringBuilder();
System.Collections.ArrayList fields = new System.Collections.ArrayList();
// 遍历所有的公开的实例属性来获得字段绑定信息
foreach( System.Reflection.PropertyInfo p in ObjectType.GetProperties(
System.Reflection.BindingFlags.Instance
| System.Reflection.BindingFlags.Public
))
{
BindFieldAttribute fa = ( BindFieldAttribute ) Attribute.GetCustomAttribute(
p , typeof( BindFieldAttribute ));
if( fa != null )
{
FieldBindInfo NewFieldInfo = new FieldBindInfo();
NewFieldInfo.Attribute = fa ;
NewFieldInfo.FieldName = fa.Name ;
if( NewFieldInfo.FieldName == null || NewFieldInfo.FieldName.Trim().Length == 0 )
{
// 若在特性中没有指明绑定的字段名则使用默认的属性名称
NewFieldInfo.FieldName = p.Name ;
}
if( myFieldList.Length > 0 )
{
myFieldList.Append(",");
}
myFieldList.Append( FixFieldName( NewFieldInfo.FieldName ) ) ;
NewFieldInfo.Property = p ;
NewFieldInfo.ValueType = p.PropertyType ;
NewFieldInfo.DefaultValue = GetDefaultValue( p );
fields.Add( NewFieldInfo );
}
}
NewInfo.Fields = ( FieldBindInfo[]) fields.ToArray( typeof( FieldBindInfo ));
NewInfo.FieldNameList = myFieldList.ToString();
// 缓存绑定信息对象
myBufferedInfos[ ObjectType ] = NewInfo ;
return NewInfo ;
}
/// <summary>
/// 判断两个字段名是否等价
/// </summary>
/// <param name="name1">字段名1</param>
/// <param name="name2">字段名2</param>
/// <returns>true:两个字段名等价 false:字段名不相同</returns>
private bool EqualsFieldName( string name1 , string name2 )
{
if( name1 == null || name2 == null )
{
throw new ArgumentNullException("name1 or name2");
}
name1 = name1.Trim();
name2 = name2.Trim();
// 进行不区分大小写的比较
if( string.Compare( name1 , name2 , true ) == 0 )
{
return true ;
}
int index = name1.IndexOf(".");
if( index > 0 )
{
name1 = name1.Substring( index + 1 ).Trim();
}
index = name2.IndexOf(".");
if( index > 0 )
{
name2 = name2.Substring( index + 1 ).Trim();
}
return string.Compare( name1 , name2 , true ) == 0 ;
}
/// <summary>
/// 获得指定属性的默认值
/// </summary>
/// <param name="p">属性对象</param>
/// <returns>获得的默认值</returns>
private object GetDefaultValue( System.Reflection.PropertyInfo p )
{
Type pt = p.PropertyType ;
System.ComponentModel.DefaultValueAttribute dva =
( System.ComponentModel.DefaultValueAttribute )
Attribute.GetCustomAttribute(
p , typeof( System.ComponentModel.DefaultValueAttribute ));
if( dva == null )
{
if( pt.Equals( typeof( byte )))
return ( byte ) 0 ;
else if( pt.Equals( typeof( short )))
return ( short ) 0 ;
else if( pt.Equals( typeof( int )))
return ( int ) 0 ;
else if( pt.Equals( typeof( uint )))
return ( uint ) 0 ;
else if( pt.Equals( typeof( long )))
return ( long ) 0 ;
else if( pt.Equals( typeof( float )))
return ( float ) 0 ;
else if( pt.Equals( typeof( double )))
return ( double ) 0 ;
else if( pt.Equals( typeof( DateTime )))
return DateTime.MinValue ;
else if( pt.Equals( typeof( char )))
return char.MinValue ;
else
return null;
}
else
{
System.ComponentModel.TypeConverter converter =
System.ComponentModel.TypeDescriptor.GetConverter( pt );
if( dva.Value != null )
{
Type t = dva.Value.GetType();
if( t.Equals( pt ) || t.IsSubclassOf( pt ))
{
return dva.Value ;
}
}
if( converter == null )
{
return dva.Value ;
}
else
{
return converter.ConvertFrom( dva.Value );
}
}
}
/// <summary>
/// 数据表绑定信息对象
/// </summary>
private class TableBindInfo
{
/// <summary>
/// 数据库表名
/// </summary>
public string TableName = null;
/// <summary>
/// 对象类型
/// </summary>
public Type ObjectType = null;
/// <summary>
/// 绑定信息对象
/// </summary>
public BindTableAttribute Attribute = null;
/// <summary>
/// 绑定的字段信息对象
/// </summary>
public FieldBindInfo[] Fields = null;
/// <summary>
/// 绑定的字段列表,格式为"字段1,字段2,字段3"
/// </summary>
public string FieldNameList = null;
}
/// <summary>
/// 数据字段绑定信息对象
/// </summary>
private class FieldBindInfo
{
/// <summary>
/// 绑定的字段名
/// </summary>
public string FieldName = null;
/// <summary>
/// 绑定的字段序号
/// </summary>
public int FieldIndex = - 1;
/// <summary>
/// 对象属性信息
/// </summary>
public System.Reflection.PropertyInfo Property = null;
/// <summary>
/// 数据类型
/// </summary>
public Type ValueType = null;
/// <summary>
/// 默认值
/// </summary>
public object DefaultValue = null;
/// <summary>
/// 绑定信息对象
/// </summary>
public BindFieldAttribute Attribute = null;
/// <summary>
/// 将对象数据转换为数据库中的数据
/// </summary>
/// <param name="v">对象数据</param>
/// <returns>数据库数据</returns>
public object ToDataBase( object v )
{
if( v == null || DBNull.Value.Equals( v ))
return DBNull.Value ;
string Format = Attribute.WriteFormat ;
if( Format != null && Format.Trim().Length > 0 )
{
if( v is System.IFormattable )
{
v = ( ( System.IFormattable ) v ).ToString( Format , null );
}
}
return v ;
}
/// <summary>
/// 将从数据库中获得的数据转换为对象数据
/// </summary>
/// <param name="v">从数据库获得的原始数据</param>
/// <returns>转化后的对象数据</returns>
public object FromDataBase( object v )
{
// 若数据为空则返回默认值
if( v == null || DBNull.Value.Equals( v ))
return DefaultValue ;
// 进行格式化解析
string Format = Attribute.ReadFormat ;
if( Format != null && Format.Trim().Length > 0 )
{
string Value = Convert.ToString( v );
if( ValueType.Equals( typeof( DateTime )))
{
if( Format == null )
return DateTime.Parse( Value );
else
return DateTime.ParseExact( Value , Format , null );
}
else if( ValueType.Equals( typeof(byte )))
{
return byte.Parse( Value );
}
else if( ValueType.Equals( typeof( short )))
{
return short.Parse( Value );
}
else if( ValueType.Equals( typeof( int )))
{
return int.Parse( Value );
}
else if( ValueType.Equals( typeof( float )))
{
return float.Parse( Value );
}
else if( ValueType.Equals( typeof( double )))
{
return double.Parse( Value );
}
return Convert.ChangeType( Value , ValueType );
}
if( v.GetType().Equals( ValueType ) || v.GetType().IsSubclassOf( ValueType ))
{
// 若数据类型匹配则直接返回数值
return v ;
}
else
{
// 若读取的值和对象数据的类型不匹配则进行数据类型转换
System.ComponentModel.TypeConverter converter =
System.ComponentModel.TypeDescriptor.GetConverter( ValueType );
if( converter != null && converter.CanConvertFrom( v.GetType()) )
{
return converter.ConvertFrom( v ) ;
}
return Convert.ChangeType( v , ValueType );
}
}//public object FromDataBase( object v )
}
#endregion
}//public class MyORMFramework
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -