Click here to Skip to main content
Click here to Skip to main content

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.

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:

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

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.

[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. 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. 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. 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";
        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)

Share

About the Author

WillCaptain

China China
No Biography provided

Comments and Discussions

 
QuestionSource code? PinadminChris Maunder5-May-08 6:14 
AnswerRe: Source code? PinmemberWillCaptain5-May-08 16:30 
QuestionIs the source code in the distribution? PinmemberDewey4-May-08 15:11 
AnswerRe: Is the source code in the distribution? PinmemberWillCaptain4-May-08 16:29 

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

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

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