轻量级ORM开发系列:Attribute准备

     此系列文章记录我开发ORM的过程。

     开篇:Attribute准备

     我们需要利用Attribute来完成某些信息的配置,在nhibernate中我们使用的是XML文件来配置主键,字段映射,实体之间的关系,级联操作等等关系。这也带来了许多的问题,大部分的使用nhibernate开发的人员都会抱怨XML文件的管理,以及编写问题。笔者实践的这个ORM为了更好的提高用户体验,决定舍弃XML配置关系信息的方式,所有的相关信息都使用Attribute来实现。例如:

public class student{

 string name;

 string child_name;

}

 一个学生类,包含两个信息。姓名以及小时候的名字,如果我们的业务逻辑需要,把所有学生小时候的名字同意显示为 “小+name"  那么我们就根本不需要开辟child_name属性到数据库,自然也就不需要保存了。这是使用Attribute来实现这一功能我们可以这样写:

 [Ignore]

 string child_name;

 当然首先我们需要实现这些Attribute。

既然上面已经提到了这个  [Ignore] 那么我们就来先实现它吧。

 

[Serializable, AttributeUsage(AttributeTargets.Property)]
    
public   class  IgnoreAttribute : Attribute
    {
    }

 看出个究竟来了没有?为什么类里面是空的?这样做的意义是什么?

这里的 IgnoreAttribute仅仅只是起到一个标识作用,这就相当于一个bool类型的属性,它只有两种可能,不是true就是false,这也是同样的道理,用定义了 [Ignore]与没有定义 [Ignore]来模拟true和false。[Serializable]标识该类能够被序列化,方便以文件的方式持久保存。AttributeUsage(AttributeTargets.Property)表示[Ignore]这个Attribute只能用于属性,不能用于方法或类。强制定义它的使用范围。

本文既然是 Attribute准备 ,那么我们就要一次性实现所有的Attribute。

 

ExpandedBlockStart.gif ColumnAttribute
[AttributeUsage(AttributeTargets.Property)]
    
public   sealed   class  ColumnAttribute : Attribute
    {
        
private   string  _column;
        
private  DataType dataType;
        
private   int  length = 255 ;
        
private   bool  canNull = true ;
        
private   object  defaultValue;
        
private   bool  autoIncrement  =   false ;
        
private   bool  isUnique = false ;

        
public  ColumnAttribute()
        {
        }
        
public  ColumnAttribute( string  paraColumn,DataType dataType)
        {
            
this ._column  =  paraColumn;
            
this .dataType  =  dataType;
        }
        
public  ColumnAttribute( string  paraColumn,DataType dataType, int  length, bool  canNull, object  defaultValue, bool  autoIncrement, bool  isUnique)
        {
            
this ._column  =  paraColumn;
            
this .dataType  =  dataType;
            
this .length  =  length;
            
this .canNull  =  canNull;
            
this .defaultValue  =  defaultValue;
            
this .autoIncrement  =  autoIncrement;
            
this .isUnique  =  isUnique;
        }



        
///   <summary>
        
///  表字段的数据类型
        
///   </summary>
         public  DataType DataType
        {
            
get  {  return  dataType; }
            
set  { dataType  =  value; }
        }
        
///   <summary>
        
///  数据库中的列
        
///   </summary>
         public   string  Column
        {
            
get  {  return  _column; }
            
set  { _column  =  value; }
        }

        
///   <summary>
        
///  表字段的长度
        
///   </summary>
         public   int  Length
        {
            
get  {  return  length; }
            
set  { length  =  value; }
        }

        
///   <summary>
        
///  表字段是否可以为空
        
///   </summary>
         public   bool  CanNull
        {
            
get  {  return  canNull; }
            
set  { canNull  =  value; }
        }

        
///   <summary>
        
///  表字段的默认值
        
///   </summary>
         public   object  DefaultValue
        {
            
get  {  return  defaultValue; }
            
set  { defaultValue  =  value; }
        }
        
///   <summary>
        
///  表字段是否为自动增长列
        
///   </summary>
         public   bool  AutoIncrement
        {
            
get  {  return  autoIncrement; }
            
set  { autoIncrement  =  value; }
        }
        
///   <summary>
        
///  确定某个字段是否唯一
        
///   </summary>
         public   bool  IsUnique
        {
            
get  {  return  isUnique; }
            
set  { isUnique  =  value; }
        }
    }
ExpandedBlockStart.gif RelationAttribute
 [AttributeUsage(AttributeTargets.Property)]
    
public   class  RelationAttribute:Attribute
    {
        
public  RelationAttribute()
        {
        }
        
public  RelationAttribute(Cascade paracascade,Relation pararelation)
        {
            _cascade 
=  paracascade;
            _relation 
=  pararelation;
        }
        
private  Cascade _cascade  =  Cascade.None;
        
private  Relation _relation  =  Relation.None;
        
///   <summary>
        
///  表之间的关系
        
///   </summary>
         public  Relation Relation
        {
            
get  {  return  _relation; }
            
set  { _relation  =  value; }
        }
        
///   <summary>
        
///  级联操作
        
///   </summary>
         public  Cascade Cascade
        {
            
get  {  return  _cascade; }
            
set  { _cascade  =  value; }
        }

    }
ExpandedBlockStart.gif TableAttribute
 [AttributeUsage( AttributeTargets.Class )]
    
public   sealed   class  TableAttribute:Attribute
    {
        
private   string  _PrimaryKey;
        
private   string  name;
        
private   string  version  =   " V1.0 " ;
        
///   <summary>
        
///  无参数构造方法
        
///   </summary>
         public  TableAttribute()
        {

        }
        
public  TableAttribute( string  name) 
        {
            
this ._PrimaryKey  = " ID " ;
            
this .name  =  name;
        }
        
public  TableAttribute( string  name, string  version,  string  DataBaseName)
        {
            
this ._PrimaryKey  =   " ID " ;
            
this .name  =  name;
            
this .version  =  version;
        }

        
///   <summary>
        
///  数据表名
        
///   </summary>
         public   string  Name
        {
            
get  {  return  name; }
            
set  { name  =  value; }
        }
        
public   string  DataBaseName {  get set ; }
        
///   <summary>
        
///  表实体版本号
        
///  默认为 "V1.0"
        
///   </summary>
         public   string  Version
        {
            
get  {  return  version; }
            
set  { version  =  value; }
        }
        
///   <summary>
        
///  表字段是否为主键
        
///   </summary>
         public   string  PrimaryKey
        {
            
get  {  return  _PrimaryKey; }
            
set  { _PrimaryKey  =  value; }
        }
    }

以及上面已经提供的IgnoreAttribute一起。这四个Attribute将完成所有的配置信息关系的表示。

这里有必要进行一些简单的介绍

 ColumnAttribute : 定义每一列的相关信息(包括表字段的数据类型,数据库中的列名,表字段的长度,表字段是否可以为空,表字段的默认值等等信息,具体有哪些请参见这个类的注释。

 RelationAttribute: 定义每一列与其他Model的关系,包括many2many,many2one等关系,再后面的文章中将会一一讲述。

 TableAttribute    :  定义每个类所对应的表名,主键等信息。

 好了,今天就到这里,请听下回分解。

转载于:https://www.cnblogs.com/Creator/archive/2010/12/02/1861808.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值