Castle ActiveRecord学习实践(3):映射基础

摘要:本文详细介绍了ActiveRecord中的基本映射,对于关联映射会在后续文章中通过一些具体的实例来说明。

主要内容

简单映射

1ActiveRecordAttribute

2. PrimaryKeyAttribute

3CompositeKeyAttribute

4PropertyAttribute

5FieldAttribute

 

一.ActiveRecordAttribute

每一个实体类都必须继承于基类ActiveRecordBase,并在实体类上设置特性ActiveRecordAttribute,示例代码

//指定数据库表名

[ActiveRecord(
"Blogs")]

public class Blog : ActiveRecordBase

{

    
//

}


//不指定数据库表名

[ActiveRecord]

public class Blog : ActiveRecordBase

{

    
//

}

 

 ActiveRecordAttribute说明

属性

说明

示例

Table

指定持久化类所对应的数据库表名,如果表名与类名相同,可以省略

[ActiveRecord("Blogs")]

[ActiveRecord(Table="Blogs")]

 

Schema

指定Schema的名字

Schema="ARDemo"

Proxy

指定一个接口,在延迟装载时作为代理使用

 

DiscriminatorColumn

识别器的字段名

DiscriminatorColumn="Blog"

DiscriminatorType

识别器的字段类型

DiscriminatorType="String"

DiscriminatorValue

识别器字段的值

 

Where

指定一个附加SQLWhere子句

Where="IsPost = 0"

Lazy

指定是否延迟加载

Lazy=true|false

 

二.PrimaryKeyAttribute

在实体类中,通过PrimaryKeyAttribute来指定表的主键,示例代码

//指定主键字段名

[ActiveRecord()]

public class Blog : ActiveRecordBase

{

    
private int id;

 

    [PrimaryKey(
"blog_id")]

    
public int Id

    
{

        
get return id; }

        
set { id = value; }

    }


}


//不指定主键字段名

[ActiveRecord()]

public class Blog : ActiveRecordBase

{

    
private int id;

 

    [PrimaryKey]

    
public int Id

    
{

        
get return id; }

        
set { id = value; }

    }


}


PrimaryKeyAttribute说明

属性

说明

示例

PrimaryKeyType

主键生成的方式,如果不指定,则默认的方式为PrimaryKeyType.Native

PrimaryKeyType.Native

Column

主键字段名称,如果跟属性名相同,可以不用指定

PrimaryKey("blog_id")

ColumnType

主键字段的类型

 

Generator

是一个.NET类的名字,用来为该持久化类的实例生成唯一的标识。

 

Params

Params来提供Generator所需要的配置参数或初始化参数

 

Length

主键字段的长度

Length=10

SequenceName

当指定主键的生成方式为Sequence时,序列的名称

PrimaryKey(PrimaryKeyType.Sequence, SequenceName="myseqname")

UnsavedValue

用来标志该实例是刚刚创建的,尚未保存。

 

 

主键的生成方式介绍

名称

说明

Identity

DB2,MySQL, MS SQL Server, SybaseHypersonicSQL的内置标识字段提供支持,生成自增的整型

Sequence

序列,对DB2,MySQL, PostgreSQL, Oracle的内置标识字段提供支持,生成自增的整型。

HiLo

高低位,使用一个高/低位算法来高效的生成Int64, Int32 或者 Int16类型的标识符。

SeqHiLo

使用序列的高低位,使用一个高/低位算法来高效的生成Int64, Int32 或者 Int16类型的标识符,给定一个数据库序列(sequence)的名字。

UuidHex

用一个System.Guid和它的ToString(string format)方法生成字符串类型的标识符。

UuidString

用一个新的System.Guid产生一个byte[] ,把它转换成字符串。

Guid

用一个新的System.Guid 作为标识符。

GuidComb

Jimmy Nilsso的一个算法产生一个新的System.Guid

Native

根据底层数据库的能力选择 identity, sequence 或者 hilo中的一个。默认值。

Assigned

让应用程序在自己为对象分配一个标示符。

Foreign

使用另外一个相关联的对象的标识符。

 

三.CompositeKeyAttribute

如果使用组合键,需要我们自定义一个类来作为主键属性的类型。示例代码

[PrimaryKey]

public MyCompositeKey ID

{

    
get return _key; }

    
set { _key = value; }

}


对于组合键类,除了需要加上CompositeKey特性之外,它还需要是可序列化的,并且要求实现EqualsGetHashCode方法。ActiveRecord官方网站上提供的一个组合键的示例程序如下:

[CompositeKey, Serializable]

public class MyCompositeKey

{

    
private string _keyA;

    
private string _keyB;

 

    [KeyProperty]

    
public virtual string KeyA

    
{

        
get return _keyA; }

        
set { _keyA = value; }

    }


 

    [KeyProperty]

    
public virtual string KeyB

    
{

        
get return _keyB; }

        
set { _keyB = value; }

    }


 

    
public override string ToString()

    
{

        
return string.Join( ":"new string[] { _keyA, _keyB } );

    }


 

    
public override bool Equals( object obj )

    
{

        
if( obj == this ) return true;

        
if( obj == null || obj.GetType() != this.GetType() ) return false;

        MyCompositeKey test 
= ( MyCompositeKey ) obj;

        
return ( _keyA == test.KeyA || (_keyA != null && _keyA.Equals( test.KeyA ) ) ) &&

            ( _keyB 
== test.KeyB || ( _keyB != null && _keyB.Equals( test.KeyB ) ) );

    }


 

    
public override int GetHashCode()

    
{

        
return _keyA.GetHashCode() ^ _keyB.GetHashCode();

    }


}

 

四.PropertyAttribute

ActiveRecord中通过PropertyAttribute来指定实体类属性与数据库中的字段映射。

[ActiveRecord()]

public class Blog : ActiveRecordBase

{

    
//不指定字段名

    [Property]

    
public int Name

    
{

        
get return _name; }

        
set { _name = value; }

    }


}


 

[ActiveRecord()]

public class Blog : ActiveRecordBase

{

    
//指定字段名

    [Property(
"blog_name")]

    
public int Name

    
{

        
get return _name; }

        
set { _name = value; }

    }


}


PropertyAttribute说明

属性

说明

示例

Column

对应的数据库字段名

Property("blog_name")

ColumnType

对应的字段类型

 

Formula

一个SQL表达式,定义了这个计算(computed) 属性的值。计算属性没有和它对应的数据库字段。

 

UnsavedValue

用来标志该实例是刚刚创建的,尚未保存。

 

Length

字段的长度

Length=10

NotNull

是否可以为空

NotNull=true|false

Unique

是否允许重复

Unique=true|false

Update

表明在用于UPDATE SQL语句中是否包含这个字段。默认为true

Update=true|false

Insert

表明在用于INSERTSQL语句中是否包含这个字段。默认为true

Insert=true|false

 

五.FieldAttribute

ActiveRecord中,允许我们直接对Field进行映射,使用FieldAttribute

[ActiveRecord()]

public class Blog : ActiveRecordBase

{

    
//不指定字段名称

    [Field]

    
string _name;

}


 

[ActiveRecord()]

public class Blog : ActiveRecordBase

{

    
//指定字段名称

    [Field(
"blog_name")]

    
string _name;

}


FieldAttribute说明

属性

说明

示例

Column

对应的数据库字段名

Property("blog_name")

ColumnType

对应的字段类型

 

Formula

一个SQL表达式,定义了这个计算(computed) 属性的值。计算属性没有和它对应的数据库字段。

 

UnsavedValue

用来标志该实例是刚刚创建的,尚未保存。

 

Length

字段的长度

Length=10

NotNull

是否可以为空

NotNull=true|false

Unique

是否允许重复

Unique=true|false

Update

表明在用于UPDATE SQL语句中是否包含这个字段。默认为true

Update=true|false

Insert

表明在用于INSERTSQL语句中是否包含这个字段。默认为true

Insert=true|false

 

六.NestedAttribute

在映射的时候我们也可以用子对象来映射数据库中的字段,示例代码

[ActiveRecord]

public class Company : ActiveRecordBase

{

    
private PostalAddress _address;

 

    [Nested]

    
public PostalAddress Address

    
{

        
get return _address; }

        
set { _address = value; }

    }


}


 

public class PostalAddress

{

    
private String _address;

    
private String _city;

    
private String _state;

    
private String _zipcode;

 

    
public PostalAddress()

    
{

    }


 

    
public PostalAddress(String address, String city,

        String state, String zipcode)

    
{

        _address 
= address;

        _city 
= city;

        _state 
= state;

        _zipcode 
= zipcode;

    }


 

    [Property]

    
public String Address

    
{

        
get return _address; }

        
set { _address = value; }

    }


 

    [Property]

    
public String City

    
{

        
get return _city; }

        
set { _city = value;}

    }


 

    [Property]

    
public String State

    
{

        
get return _state; }

        
set { _state = value; }

    }


 

    [Property]

    
public String ZipCode

    
{

        
get return _zipcode; }

        
set { _zipcode = value; }

    }


}

 

NestedAttribute说明

属性

说明

示例

Update

表明在用于UPDATE SQL语句中是否包含这个字段。默认为true

Update=true|false

Insert

表明在用于INSERTSQL语句中是否包含这个字段。默认为true

Insert=true|false

基本的映射就介绍这么多了,剩下的还有版本(VersionAttribute),时间戳(TimestampAttribute)等映射大家可以参考相关的文档。在下篇文章中我会通过一个具体的实例介绍实现One-Many/Many-One映射。

 

参考资料

Castle的官方网站http://www.castleproject.org

posted @ 2006-04-06 08:28  TerryLee  阅读(25163)  评论(32编辑  收藏  举报