1、DbEntry 介绍
EN&Download——[DbEntry Framework下载][Tutorials For Version 3.5]
CN&Summary:总体特性的介绍详见Elephant翻译的DbEntry Framework系列[Elephant‘s WorkBench]
2、object definition
///<authors>
/// SOPICH
/// </authors>
/// <email>
/// titan_song713@hotmail.com
/// </email>
/// <date>2009/03/06</date>
#region 数据对象类的实现 <Inherit from>DbObjectModel||DbObject||IDbObject||NamedDbObject
/// <summary>
/// 定义一个派生于DbObjectModel<T>的类User
/// </summary>
/// <methods>
/// DbObjectModel<T>的静态方法
/// Save()——当数据库无此记录则Insert(),有则Update()
/// Delete()
/// Find()
/// New()
/// FindById()
/// FindBySql()
/// FindOne()
/// FindRecent()
/// etc.
/// </methods>
/// <bookmarks>
/// 拥有特殊的PartialUpdate()
/// </bookmarks>
public abstract class User : DbObjectModel<User>
{
public abstract string Name { get; set; }
}
/// <summary>
/// 定义一个派生于DbObject的类User
/// </summary>
/// <methods>
/// 无静态方法
/// </methods>
/// <bookmark>
/// 默认添加一个[User].Id的column,并设置为long型的自增长标识符
/// 无静态方法PartialUpdate()
/// </bookmark>
public class User : DbObject
{
public string Name;
}
/// <summary>
/// 定义一个派生于System.object的类User
/// </summary>
/// <methods>
/// </methods>
/// <parameters>
/// bool IsDbGenerate——可定义改属性是否为自增长,默认为true
/// int Length——最大长度
/// </parameters>
/// <bookmark>
/// 无Id,但可自定义主键和多键位
/// </bookmark>
public class User : IDbObject
{
[DbKey(IsDbGenerate=false), Length(50)]//设置主键为Name字段,string.Length为50,非自增长类型
public string Name;
}
/// <summary>
/// 定义一个派生于NamedDbObject的类User
/// </summary>
/// <methods>
/// 同上
/// </methods>
public class User : NamedDbObject, IDbObject
{
//默认主键为Name,Name.Length=255
}
#endregion
#region [DbKey]——键位的设置
/// <summary>
/// [DbKey]
/// </summary>
/// <methods>
/// </methods>
/// <performance>
/// 实现该属性为一个主键,默认属性IsDbGenerate为true,会自增长
/// </performance>
///单主键primary key
public class User : IDbObject
{
[DbKey]///相当于IsDbGenerate=true
public long userID;
public string Name;
}
///多键位mutil-key
public class MKey : IDbObject
{
[DbKey(IsDbGenerate = false), Length(50)]
public string Name;
[DbKey(IsDbGenerate = false)]
public int Age;
public bool Gender;
}
#endregion
#region [DbTable]自定义Table对象的名称 [DbColumn]自定义Column的名称
/// <summary>
/// 提供数据库对象的属性定制
/// </summary>
/// <methods>
/// [DbTable("TableName")]——数据库Table对象的名称
/// 位于类声明的前面
/// </methods>
/// <methods>
/// [Length(int i), AllowNull,StringColumn(IsUnicode=true, Regular=CommonRegular.[EmailRegular||Url])]
/// 设置Column的长度,是否为空,字符列的格式(bool IsUnicode)用于区分nvar和var等,(Enum CommonRegular)提供了常用的email和url正则表达式验证
///位于字段名声明的前面
/// </methods>
/// <bookmark>
/// 实现这种映射还可以在config文件(App.config or Web.config)
/// <Lephone.Settings>
/// <add key="@数据库表名" value="自定义对象类" />
/// </Lephone.Settings>
/// </bookmark>
[DbTable("User")]
public class MyUser : IDbObject
{
[DbColumn("Name")]
public string theName;
}
public class User : IDbObject
{
[Length(50), AllowNull]
public string Name;
[StringColumn(IsUnicode=true, Regular=CommonRegular.EmailRegular||url)]
public string Email;
}
#endregion
#region [Exclude]
/// <summary>
/// [Exclude]——修饰数据对象中不需要实现数据库交互的字段
/// </summary>
/// <methods>
/// 除了使用[Exclude],私有字段和只读属性,都不会实现数据库存储交互
/// </methods>
public class User : IDbObject
{
public string Name;
[Exclude]
public bool isUserInput;
private int times; // exclude too
public int code { get { return 0; } } // exclude too
}
#endregion
#region 数据库索引
/// <summary>
/// [Index("Name_Age", ASC = false, UNIQUE = true)]
/// </summary>
/// <performance>
/// 建立索引
/// </performance>
class MyTest
{
[DbKey]
public long Id = 0;
[Index("Name_Age", ASC = false, UNIQUE = true)]
public string Name = null;
[Index("Name_Age", ASC = false, UNIQUE = true)]
public int Age = 0;
}
#endregion
#region Join 表的联合
/// <summary>
/// 实现两个或多个表的联合
/// </summary>
/// <methods>
/// [JoinOn(int Index,"first col","second col",Enum CompareOpration,Enum JoinMode)]
/// </methods>
//两个表的联合
[DbTable("SampleData.Id", "TheAge.Id")]
public class JoinTable1 : IDbObject
{
[DbColumn("SampleData.Id")] public long Id;
public string Name;
public UserRole Role;
public DateTime JoinDate;
public int Age;
}
//三个表的联合
[JoinOn(0, "SampleData.Id", "TheAge.Id", CompareOpration.Equal, JoinMode.Inner)]
[JoinOn(1, "SampleData.Id", "EnumTable.Id", CompareOpration.Equal, JoinMode.Inner)]
public class JoinTable2 : IDbObject
{
[DbColumn("SampleData.Id")] public long Id;
[DbColumn("SampleData.Name")] public string Name;
public UserRole Role;
public DateTime JoinDate;
public int Age;
[DbColumn("EnumTable.Name")] public string EnumName;
}
#endregion
#region Object Relationship[HasOne, HasMany, BelongsTo, HasAndBelongsToMany]
/// <summary>
/// 实现一对一、一对多、多对一、多对多的对象关系
/// </summary>
[DbTable("People")]
public abstract class Person : DbObjectModel<Person>
{
public abstract string Name { get; set; }
[HasOne(OrderBy = "Id DESC")]
public abstract PersonalComputer PC { get; set; }
}
public abstract class PersonalComputer : DbObjectModel<PersonalComputer>
{
public abstract string Name { get; set; }
[BelongsTo, DbColumn("Person_Id")]
public abstract Person Owner { get; set; }
}
#endregion
#region Just Example
/// <summary>
/// 如何定义枚举型的字段,实现数据对象的域
/// </summary>
public enum UserRole
{
Manager,
Worker,
Client
}
public abstract class SampleData : DbObjectModel<SampleData>
{
[Length(50)] public abstract string Name { get; set; }
public abstract UserRole Role { get; set; }
public abstract DateTime JoinDate { get; set; }
public abstract bool Enabled { get; set; }
public abstract int? NullInt { get; set; }
public SampleData() {}
public SampleData(string Name, UserRole Role, DateTime JoinDate, bool Enabled)
: this(Name, Role, JoinDate, Enabled, null) { }
public SampleData(string Name, UserRole Role, DateTime JoinDate, bool Enabled,
int? NullInt)
{
this.Name = Name;
this.Role = Role;
this.JoinDate = JoinDate;
this.Enabled = Enabled;
this.NullInt = NullInt;
}
}
#endregion