65.9K
CodeProject 正在变化。 阅读更多。
Home

Trilobita ORMaper 解决方案,基于 .NET 2005/2008

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.73/5 (6投票s)

2008年5月4日

CPOL

2分钟阅读

viewsIcon

23086

downloadIcon

165

基于 .NET (C#) 的 ORMaper 解决方案,VS IDE 插件,自动生成 ORMaper 代码。

引言

Trilobita 是一个用于 VS2005/2008 的 ORMaper 插件。

成功安装 Trilobita 后,启动 VS2005/2008 时会看到一个欢迎界面。 Trilobita 将为您提供以下功能(仅支持 SQL Server)

  1. 根据您的数据库表生成 C# 类。
  2. 自动创建数据层,无需编写任何代码。
  3. 您可以使用 C# 代码操作数据库,而无需关注数据库本身。

背景

我基于我对 ORMaper 概念的理解设计了这个插件。 有许多基于 VS 架构的 ORMaper 解决方案。 我无法保证我的解决方案是最好的。 VS 2008 已经发布一段时间了。 我花了两年时间在中国和美国学习 MBA 课程。 我大约在 3 年前设计了 Trilobita。 那时,我认为 Visual Studio 没有一个好的 ORMaper 解决方案。 当我从 MBA 课程毕业时,我意识到 Microsoft 已经发布了大量新概念,包括 WCF、WPF、WFF... 我开源了 Trilobita,让对技术充满热情的 C# 爱好者能够聚集在一起讨论 ORMaper 解决方案。 我想为它做出贡献,并希望我们可以进一步讨论它。

Using the Code

成功安装 Trilobita 后,您可以根据数据库将 C# 代码输出到您的项目。 每个类都会连接到数据库中的特定表。 它包含一些要点,可帮助 Trilobita 引擎正确运行它

[TableAttribute("Teacher",true)]
public partial class Teacher : TrilobitaRow

TableAttribute 的第一个参数指示一个类 teacher 与相关数据库中的表 teacher 相关。 第二个参数告诉 Trilobita 这个类是否会创建一个对象池,这意味着这个表中的所有行都将在第一次被转换为对象。 第一次连接后,您无需与数据库通信,除非您需要修改表数据。(注意:如果您想基于特定表创建对象池,请在 Trilobita 配置的左侧栏中找到该表,右键单击该表,然后选择“创建对象池”)。

Objectpool.JPG

TrilobitaRow 包含一些可以大大简化编码的方法。

public event TrilobitaRow.DeletedRow AfterDeleteRowEvent; 
public event TrilobitaRow.NewedRow AfterNewRowEvent; 
public event TrilobitaRow.UpdatedRow AfterUpdateRowEvent; 
public event TrilobitaRow.BeforeDeletedRow BeforeDeletedRowEvent; 
public event TrilobitaRow.BeforeNewRow BeforeNewRowEvent; 
public event TrilobitaRow.BeforeUpdateRow BeforeUpdateRowEvent; 
public void Delete(); 
public string GetTableName(); 
public void Save(); 
public static bool Save(List<TrilobitaRow> ARows); 
public static bool Save(TrilobitaRow[] ARows); 
protected void SetFlag();

Will.Trilobita.Engine.Field 修饰 _ID 字段。 它有助于将表字段与类字段连接起来。

[Will.Trilobita.Engine.Field("ID","N",-1,true,true)]

Int32 _ID;
Int32 OldID;//only primary key has old value.

第一个参数告诉 Trilobita 表中的相关字段。 第二个参数指示此字段是字符串类型(不必担心确切的类型,Trilobita 会自动识别它)。 第三个参数是该字段的长度,现在几乎没用。 第四个参数告诉 Trilobita 此字段是否是主键。 第五个参数指示此字段是否是增量字段。

关注点

示例 TestTri 演示了如何使用 Trilobita。

  1. 创建一个新的 Teacher 对象。
  2. TestTri.Trilobita.Teacher teacher = new TestTri.Trilobita.Teacher();
    
    teacher.Name = "ddddd";
    //Image field, the original field is Detail, you can add another partial 
    //teacher class to translate the Detail(byte[]) to System.Drawing.Image type
    teacher.DetailImage = this.picTeacher.Image;
    
    //this is the most important method to tell Trilobita that everything is ready 
    //to communicate with database.
    this.teacher.Save();
  3. 更新/删除 Teacher
  4. TestTri.Trilobita.Teacher teacher = TestTri.Trilobita.Teacher.GetTeacher(int AID);
    
    Teacher.Name = "New Name";
    
    .....
    
    teacher.Save();
    
    //delete teacher
    
    teacher.Delete();//this method tells Trilobita this record is going to be deleted
    
    teacher. Save();//save method carry out the database operation
  5. 获取特定教师的学生
  6. TestTri.Trilobita.Teacher teacher = TestTri.Trilobita.Teacher.GetTeacher(int AID);
    
    TestTri.Trilobita.Student[] students = this.teacher.GetStudentList();
    
    // GetStudentList has been created by Trilobita, you don't need to take care of it.
  7. 这是我们实现事务的方法
  8. static public void TransactionSuccess()
    {
        TestTri.Trilobita.Teacher[] teachers = new TestTri.Trilobita.Teacher[2];
        teachers[0] = new TestTri.Trilobita.Teacher();
        teachers[0].Name = "WillSaveRows";
        teachers[1] = new TestTri.Trilobita.Teacher();
        teachers[1].Name = "Will2";
        TestTri.Trilobita.Teacher.Save(teachers);
    }
    
    static public void TransactionFail()
    {
        TestTri.Trilobita.Teacher[] teachers = new TestTri.Trilobita.Teacher[2];
        teachers[0] = new TestTri.Trilobita.Teacher();
        teachers[0].Name = "WillTransaction";
© . All rights reserved.