zoukankan      html  css  js  c++  java
  • Castle ActiveRecord学习实践(2)映射

    本文将介绍Castle ActiveRecord 中的基本映射。

       1:      [ActiveRecord("Posts")]
       2:      public class Post:ActiveRecordBase<Post>
       3:      {
       4:          [PrimaryKey("PostId")]
       5:          public int Id { get; set; }
       6:   
       7:          [Property]
       8:          public string Subject { get; set; }
       9:   
      10:          [Property]
      11:          public string Text { get; set; }
      12:   
      13:          [Property]
      14:          public DateTime DateAdded { get; set; }
      15:   
      16:          [HasMany]
      17:          public IList<Comment> Comments { get; set; }
      18:          个o
      19:            
      20:      }

    上面的的代码中已经包含了部分特性(ActiveRecord、Property、PrimaryKey、HasMany),这些特性类在Castle.ActiveRecord 命名空间中。主要的有

    ActiveRecordAttribute

    PropertyAttribute

    PrimaryKeyAttribute

    CompositeKeyAttribute

    FieldAttribute

    ActiveRecordAttribute

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

    ActiveRecordAttribute的成员

    属性

    类型

    说明

    示例

    BatchSize int 为获取此类的实例标识符指定"批处理大小"  
    DiscriminatorColumn string 识别器的字段名 DiscriminatorColumn="Post"
    DiscriminatorLength string 识别器的长度  
    DiscriminatorType string 识别器的字段类型(int、string) DiscriminatorType=”string”
    DiscriminatorValue string 识别器字段的值  
    DynamicInsert bool From NHibernate documentation: Specifies that INSERT SQL should be generated at runtime and contain only the columns whose values are not null. 如果没有对成员变量赋值,直接调用SAVE方法的话,SQL字段中允许为NULL的字段就可以由sql赋予其默认值。因为如果不是可以为Null的话,nhibernate直接会给你个异常,告诉你有notnull字段没有初始化  
    DynamicUpdate bool From NHibernate documentation: Specifies that UPDATE SQL should be generated at runtime and contain only those columns whose values have changed.  
    Lazy bool Enable lazy loading for the type
    指定是否延迟加载
     
    LazySpecified bool Gets a value indicating whether explicit lazy behavior was specified. If explicit lazy behavior was not specified, it goes to the configuration to decide if the type should be lazy or not  
    Locking

    Castle.ActiveRecord.OptimisticLocking

    From NHibernate documentation: Determines the optimistic locking strategy. 确定乐观的锁定策略  
    Mutable bool From NHibernate documentation: Specifies that instances of the class are (not) mutable
    指定类的实例 (不) 是可变
     
    Persister System.Type From NHibernate documentation: Specifies a custom IEntityPersister.  
    Polymorphism

    Castle.ActiveRecord.Polymorphism

    From NHibernate documentation: Determines whether implicit or explicit query polymorphism is used.  
    Proxy System.Type From NHibernate documentation: Determines whether implicit or explicit query polymorphism is used.
    指定一个接口,在延迟装载时作为代理使用
     
    Schema string

    Gets or sets the schema name associated with the type
    设置概要的信息

     
    SchemaAction string    
    SelectBeforeUpdate bool From NHibernate documentation: Specifies that NHibernate should never perform an SQL UPDATE unless it is certain that an object is actually modified. In certain cases (actually, only when a transient object has been associated with a new session using update()), this means that NHibernate will perform an extra SQL SELECT to determine if an UPDATE is actually required.
    指定 NHibernate 应该永远不会执行 SQL 更新,除非它是某些对象实际修改。在某些情况下 (其实,只有当瞬态对象已经与使用 update()) 新会话相关联,这意味着 NHibernate 将执行额外 SQL SELECT 来确定是否确实需要更新。
     
    Table string

    Gets or sets the table name associated with the type
    表名,类名与表名相同可为空

    [ActiveRecord("Posts")]

    [ActiveRecord(Table="

    Posts

    ")]

    Tuplizer System.Type    
    UseAutoImport bool From NHibernate documentation: The auto-import attribute lets us use unqualified class names in the query language, by default. The assembly and namespace attributes specify the assembly where persistent classes are located and the namespace they are declared in.
    自动导入属性允许我们在查询语言中,默认情况下使用非限定的类名。程序集和命名空间的属性指定持久性类所在的程序集和命名空间中声明它们。
     
    Where string

    SQL condition to retrieve objects
    指定一个附加SQL的WHERE子句

     

    PropertyAttribute

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

    属性

    类型

    说明

    示例

    Check string

    From NHibernate documentation: create an SQL check constraint on either column or table
    约束

     
    Column string

    Gets or sets the column name
    设置字段名

    Property("PostId")
    ColumnType string

    Gets or sets the type of the column.
    设置字段类型

     
    Default string    
    Formula string

    Gets or sets the formula used to calculate this property
    一个SQL表达式,定义了这个计算(computed) 属性的值。计算属性没有和它对应的数据库字段。

     
    Index string From NHibernate documentation: specifies the name of a (multi-column) index
    指定 (多列) 的索引的名称
     
    Insert bool

    Set to false to ignore this property when inserting entities of this ActiveRecord class.

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

     
    IsOverride bool    
    Lazy bool    
    Length int

    Gets or sets the length of the property (for strings - nvarchar(50) )
    字段长度

     
    NotNull bool

    Gets or sets a value indicating whether this property allow null.
    是否允许为空

     
    SqlType string

    From NHibernate documentation: overrides the default column type
    重写默认的字段类型.

     
    Unique bool

    Gets or sets a value indicating whether this PropertyAttribute is unique.
    是否允许重复.

     
    UniqueKey string From NHibernate documentation: A unique-key attribute can be used to group columns in a single unit key constraint.  
    Update bool

    Set to false to ignore this property when updating entities of this ActiveRecord class.

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

     

    PrimaryKeyAttribute

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

    属性

    类型

    说明

    示例

    Column string

    Gets or sets the column name
    设置字段名

    PrimaryKey("PostId")
    ColumnType string

    Gets or sets the type of the column.
    设置字段类型

     
    CustomGenerator System.Type

    Gets or sets the custom generator. The generator must implement IIdentifierGenerator
    设置一个自定义的生成器.这个生成器必要实现IIdentifierGenerator接口

     
    Generator Castle.ActiveRecord.PrimaryKeyType

    Gets or sets the generator.
    设置一个生成器,是一个.NET类的名字,用来为该持久化类的实例生成惟一的标识.

     
    IsOverride bool    
    Length int

    Gets or sets the length of values in the column
    设置字段长度

     
    Params string

    Comma separated value of parameters to the generator
    用parameters来为生成器配置参数或者初始化参数.

     
    SequenceName string

    Gets or sets the name of the sequence.
    当指定主键的生成方式为Sequence时,序列的名称
    当指定主键的生成方式为Sequence时,Sequence的名称.

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

    Gets or sets the unsaved value.
    设置该实例未保存时的值

     

    PrimaryKeyType 主键生成方式的枚举

    名称

    说明

    Assigned

    The primary key value is always assigned. Note: using this you will lose the ability to call Save(), and will need to call Create() or Update() explicitly.
    让应用程序在自己为对象分配一个标示符。

    Counter

    Returns a Int64 constructed from the system time and a counter value.
    返回一个INT64类型的系统时间和一个记数器值.

    Custom

    A custom generator will be provided. See CustomGenerator

    Foreign

    This is a foreign key to another table
    外键

    Guid

    Generate a Guid for the primary key Note: You should prefer using GuidComb over this value.
    用一个新的System.Guid 作为标识符。

    GuidComb

    Generate a Guid in sequence, so it will have better insert performance in the DB.
    用Jimmy Nilsso的一个算法产生一个新的System.Guid。

    HiLo

    Use the HiLo algorithm to get the next value 
    高低位,使用一个高/低位算法来高效的生成Int64, Int32 或者 Int16类型的标识符。

    Identity

    Use Identity column (auto number) Note: This force an immediate call to the DB when Create() is called
    对DB2,MySQL, MS SQL Server, Sybase和HypersonicSQL的内置标识字段提供支持,生成自增的整型
    当Create() 方法执行时,便会触发数据库表自增类型字段自增.

    Increment

    Returns a Int64, constructed by counting from the maximum primary key value at startup.
    主键最大值加1

    Native

    Use an identity or sequence if supported by the database, otherwise, use the HiLo algorithm
    根据底层数据库的能力选择 identity, sequence 或者 hilo中的一个。默认值。

    SeqHiLo

    Use a sequence and a HiLo algorithm - better performance on Oracle
    使用序列的高低位,使用一个高/低位算法来高效的生成Int64, Int32 或者 Int16类型的标识符,给定一个数据库序列(sequence)的名字。

    Sequence

    Use a sequence
    序列

    UuidHex

    Use the hex representation of a unique identifier
    用一个System.Guid和它的ToString(string format)方法生成字符串类型的标识符。

    UuidString

    Use the string representation of a unique identifier
    用一个新的System.Guid产生一个byte[] ,把它转换成字符串。

    CompositeKeyAttribute

    组合键比较麻烦,这里还是准备了个demo

    和前面的博客差不多,新建表Tags TagPost

    BLENECTNS73D4QKJP_H2H35

    JZMNLSE~2Q$ZOL`IGHZME[H

    Post 类

       1:      [ActiveRecord("Posts")]
       2:      public class Post : ActiveRecordBase<Post>
       3:      {
       4:          [PrimaryKey("PostId")]
       5:          public int Id { get; set; }
       6:   
       7:          [Property]
       8:          public string Subject { get; set; }
       9:   
      10:          [Property]
      11:          public string Text { get; set; }
      12:   
      13:          [Property]
      14:          public DateTime DateAdded { get; set; }
      15:   
      16:          [BelongsTo("CategoryId")]
      17:          public Category Category { get; set; }
      18:   
      19:          [HasMany]
      20:          public IList<Comment> Comments { get; set; }
      21:   
      22:          [HasAndBelongsToMany(typeof(Tag), Table = "TagPost", ColumnKey = "PostId", ColumnRef = "TagId")]
      23:          public IList<Tag> Tag { get; set; }
      24:   
      25:      }

    主要的部分

    [HasAndBelongsToMany(typeof(Tag), Table = "TagPost", ColumnKey = "PostId", ColumnRef = "TagId")]
    public IList<Tag> Tag { get; set; }

    表达tag表与TagPost的关系

    Tag类

       1:      [ActiveRecord("Tags")]
       2:      public class Tag : ActiveRecordBase<Tag>
       3:      {
       4:          [PrimaryKey("TagId")]
       5:          public int Id { get; set; }
       6:   
       7:          [Property("Tag")]
       8:          public string TagName { get; set; }
       9:   
      10:          [HasAndBelongsToMany(typeof(Post), Table = "TagPost", ColumnKey = "TagId", ColumnRef = "PostId")]
      11:          public IList<Post> Post { get; set; }
      12:      }

    与上面的Post类相同 10-12 行代码表示的  Post表与TagPost的关系

    使用组合键作为主键,需要自定义一个类来作为主键属性的类型。

    新建类 TagPostKey

       1:      [Serializable]
       2:      public class TagPostKey
       3:      {
       4:          [KeyProperty]
       5:          public int PostId { get; set; }
       6:          [KeyProperty]
       7:          public int TagId { get; set; }
       8:   
       9:          public override int GetHashCode()
      10:          {
      11:              return TagId ^ PostId;
      12:          }
      13:   
      14:          public override string ToString()
      15:          {
      16:   
      17:              return string.Join(":", new string[] { PostId.ToString(), TagId.ToString() });
      18:          }
      19:          public override bool Equals(object obj)
      20:          {
      21:              if (this==obj)
      22:              {
      23:                  return true;
      24:              }
      25:              TagPostKey key = obj as TagPostKey;
      26:              if (key==null)
      27:              {
      28:                  return false;
      29:              }
      30:              if (PostId!=key.PostId||TagId!=key.TagId)
      31:              {
      32:                  return false;
      33:              }
      34:              return true;
      35:          }
      36:      }
    
    

    注意 该类必须能够序列化,还需重写Equals和GetHashCode方法

    TagPost类

       1:      [ActiveRecord]
       2:      public class TagPost:ActiveRecordBase<TagPost>
       3:      {
       4:          [CompositeKey]
       5:          public TagPostKey Id { get; set; }
       6:   
       7:      }

    FieldAttribute

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

       1:      [ActiveRecord]
       2:      public class TagPost:ActiveRecordBase<TagPost>
       3:      {
       4:          [CompositeKey]
       5:          public TagPostKey Id { get; set; }
       6:   
       7:   
       8:          [Field("ArbitraryValue")]
       9:          int _arbitraryValue;
      10:      }
  • 相关阅读:
    北京,第七天
    北京!北京!第五天
    关于模板中的动态取值 ---反射与javascript脚本编译
    InstallShield Limited Edition for Visual Studio 2013
    使用WebFrom来模拟一些MVC的MODEL与View的数据交互功能
    关于.net 对excel操作的方法
    关于Aspose对于Word操作的一些扩展及思考
    各种注释与取消注释的快捷键
    安装Vivado时遇到的问题及解决
    apache pdfbox简单读取内容
  • 原文地址:https://www.cnblogs.com/whx1973/p/2716566.html
Copyright © 2011-2022 走看看