Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

Trilobita ORMaper solution based on .NET 2005/2008

1.73/5 (6 votes)
3 May 2008CPOL2 min read 2   129  
ORMaper solution based on .NET (C#), add-in for VS IDE, and auto generating ORMaper code.

Image 1

Introduction

Trilobita is an ORMaper add-in for VS2005/2008.

After you successfully install Trilobita, you will see a welcome screen when you launch VS2005/2008. Trilobita will provide you the following functions (only supports SQL Server):

  1. Generate C# classes in terms of your database table.
  2. Automatically create data layer without writing any code.
  3. You are able to operate a database with C# code without paying any attention to the database.

Background

I designed the add-in based on of my understanding of the ORMaper concept. There are lots of ORMaper solutions based on the VS architecture. I can't guarantee you that my solution would the best. VS 2008 has been released for a while. I spent two years studying an MBA program both in China and U.S. I designed Trilobita about 3 years ago. At that time, I didn't think Visual Studio had a good solution for ORMaper. When I graduated from my MBA program, I realized that Microsoft had released huge numbers of new concepts including WCF, WPF, WFF... I opened up Trilobita to let C# fans who have a passion in technology come together to discuss ORMaper solutions. I'd like to contribute on it, and hopefully we can discuss it further.

Using the Code

After you successfully install Trilobita, you are able to output C# code to your project based on your database. Each class will connect one particular table in your database. It contains a few points to help Trilobita engine to run it properly:

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

TableAttribute's first parameter indicates a class teacher related to a table teacher in the relevant database. The second parameter tells Trilobita if this class will create an objects pool or not, which means all rows in this table will be translated into objects the very first time. You don't need to communicate with the database after the first connection, except when you need to modify the table data. (Note: if you want create an object pool based on a particular table, find the table in the left bar of the Trilobita Configuration, right click the table, and choose Create Object Pool).

Objectpool.JPG

TrilobitaRow contains a few methods which can simplify your coding pretty much.

C#
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 decorates the _ID field. It helps to connect the table field with the class field.

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

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

The first parameter tells Trilobita the related field in the table. The second parameter indicates this field is of string type (don't worry about the exact type, Trilobita will recognize it automatically). The third parameter is the length of this field which is almost useless now. The fourth parameter tells Trilobita if this field is a Primary Key or not. The fifth parameter indicates if this field is an incremental field or not.

Points of Interest

The sample TestTri demostrates how to use Trilobita.

  1. Create a new Teacher object.
  2. C#
    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. Update/delete Teacher:
  4. C#
    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. Get the students of a particular teacher:
  6. C#
    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. Here is how we implement a transaction:
  8. C#
    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";

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)