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

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

主要内容

简单映射

1ActiveRecordAttribute

2. PrimaryKeyAttribute

3CompositeKeyAttribute

4PropertyAttribute

5FieldAttribute

 

一.ActiveRecordAttribute

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

// 指定数据库表名

[ActiveRecord(
" Blogs " )]

public   class  Blog : ActiveRecordBase

ExpandedBlockStart.gifContractedBlock.gif
{

    
//

}


// 不指定数据库表名

[ActiveRecord]

public   class  Blog : ActiveRecordBase

ExpandedBlockStart.gifContractedBlock.gif
{

    
//

}

 

 

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

ExpandedBlockStart.gifContractedBlock.gif
{

    
private int id;

 

    [PrimaryKey(
"blog_id")]

    
public int Id

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return id; }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
set { id = value; }

    }


}


// 不指定主键字段名

[ActiveRecord()]

public   class  Blog : ActiveRecordBase

ExpandedBlockStart.gifContractedBlock.gif
{

    
private int id;

 

    [PrimaryKey]

    
public int Id

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return id; }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
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

ExpandedBlockStart.gifContractedBlock.gif
{

ExpandedSubBlockStart.gifContractedSubBlock.gif    
get return _key; }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
set { _key = value; }

}


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

[CompositeKey, Serializable]

public   class  MyCompositeKey

ExpandedBlockStart.gifContractedBlock.gif
{

    
private string _keyA;

    
private string _keyB;

 

    [KeyProperty]

    
public virtual string KeyA

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return _keyA; }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
set { _keyA = value; }

    }


 

    [KeyProperty]

    
public virtual string KeyB

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return _keyB; }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
set { _keyB = value; }

    }


 

    
public override string ToString()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

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

    }


 

    
public override bool Equals( object obj )

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

        
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()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

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

    }


}

 

四.PropertyAttribute

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

[ActiveRecord()]

public   class  Blog : ActiveRecordBase

ExpandedBlockStart.gifContractedBlock.gif
{

    
//不指定字段名

    [Property]

    
public int Name

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return _name; }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
set { _name = value; }

    }


}


 

[ActiveRecord()]

public   class  Blog : ActiveRecordBase

ExpandedBlockStart.gifContractedBlock.gif
{

    
//指定字段名

    [Property(
"blog_name")]

    
public int Name

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return _name; }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
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

ExpandedBlockStart.gifContractedBlock.gif
{

    
//不指定字段名称

    [Field]

    
string _name;

}


 

[ActiveRecord()]

public   class  Blog : ActiveRecordBase

ExpandedBlockStart.gifContractedBlock.gif
{

    
//指定字段名称

    [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

ExpandedBlockStart.gifContractedBlock.gif
{

    
private PostalAddress _address;

 

    [Nested]

    
public PostalAddress Address

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return _address; }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
set { _address = value; }

    }


}


 

public   class  PostalAddress

ExpandedBlockStart.gifContractedBlock.gif
{

    
private String _address;

    
private String _city;

    
private String _state;

    
private String _zipcode;

 

    
public PostalAddress()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

    }


 

    
public PostalAddress(String address, String city,

        String state, String zipcode)

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

        _address 
= address;

        _city 
= city;

        _state 
= state;

        _zipcode 
= zipcode;

    }


 

    [Property]

    
public String Address

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return _address; }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
set { _address = value; }

    }


 

    [Property]

    
public String City

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return _city; }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
set { _city = value;}

    }


 

    [Property]

    
public String State

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return _state; }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
set { _state = value; }

    }


 

    [Property]

    
public String ZipCode

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return _zipcode; }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
set { _zipcode = value; }

    }


}

 

NestedAttribute说明

属性

说明

示例

Update

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

Update=true|false

Insert

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

Insert=true|false

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

转载于:https://www.cnblogs.com/Madream/articles/1680434.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值