Click here to Skip to main content
12,897,457 members (50,988 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 3 May 2008

Trilobita ORMaper solution based on .NET 2005/2008

, 3 May 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
ORMaper solution based on .NET (C#), add-in for VS IDE, and auto generating ORMaper code.


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.


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:

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


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

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.


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. 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.
  3. Update/delete Teacher:
  4. TestTri.Trilobita.Teacher teacher = TestTri.Trilobita.Teacher.GetTeacher(int AID);
    Teacher.Name = "New Name";
    //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. 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. 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";
    static public void TransactionFail()
        TestTri.Trilobita.Teacher[] teachers = new TestTri.Trilobita.Teacher[2];
        teachers[0] = new TestTri.Trilobita.Teacher();
        teachers[0].Name = "WillTransaction";


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


About the Author

China China
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionSource code? Pin
Chris Maunder5-May-08 5:14
adminChris Maunder5-May-08 5:14 
AnswerRe: Source code? Pin
WillCaptain5-May-08 15:30
memberWillCaptain5-May-08 15:30 
QuestionIs the source code in the distribution? Pin
Dewey4-May-08 14:11
memberDewey4-May-08 14:11 
AnswerRe: Is the source code in the distribution? Pin
WillCaptain4-May-08 15:29
memberWillCaptain4-May-08 15:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 4 May 2008
Article Copyright 2008 by WillCaptain
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid