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):
- Generate C# classes in terms of your database table.
- Automatically create data layer without writing any code.
- 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).
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.
- Create a new
Teacher
object.
TestTri.Trilobita.Teacher teacher = new TestTri.Trilobita.Teacher();
teacher.Name = "ddddd";
teacher.DetailImage = this.picTeacher.Image;
this.teacher.Save();
- Update/delete
Teacher
:
TestTri.Trilobita.Teacher teacher = TestTri.Trilobita.Teacher.GetTeacher(int AID);
Teacher.Name = "New Name";
.....
teacher.Save();
teacher.Delete();
teacher. Save();
- Get the students of a particular teacher:
TestTri.Trilobita.Teacher teacher = TestTri.Trilobita.Teacher.GetTeacher(int AID);
TestTri.Trilobita.Student[] students = this.teacher.GetStudentList();
- Here is how we implement a transaction:
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";