Gentle.NET is a free ORM framework. In this section let us
understand in how to use the same by creating a custom business objects.
Overview
Gentle.NET is an RDBMS independent object persistence
framework. It features automatic SQL generation and object construction, an SQL
factory for creating custom queries, Data View construction helpers and excellent
performance and reasonably complete docs. The gentle framework defines few
attributes that help the Business Objects relate with the tables in the
database.
Business objects may optionally inherit from the Persistent class,
which provides a set of standard methods for persisting and retrieving objects.
We can replicate these on a different base class if needed or on a manager
class if you prefer a more SOA-oriented design. Gentle provides support for
identity columns (where the database assigns a primary key value to the row
being inserted) to implementers of IPersistent (which includes the Persistent
class).
Business Objects
Let us take a simple Business object called “Employee.” It
has only two attributes called as “employeeID” and “employeeName.” We have a
table Employee which has two columns “employeeId” and “employeeName” where
“employeeId” is a primary key. The Code written below is in C# and shows a
simple business object. These changes to the code need to be observed with
regards to the Gentle .Net.
Listing 2
public class Employee
{
private int employeeId;
private string employeeName;
// Constructor of the Employee class.
public Employee( int employeeId, string employeeName )
{
this.employeeId = employeeId;
this.employeeName = employeeName;
}
public int Id
{
get{ return employeeId; }
set{ employeeId = value; }
}
public string Name
{
get{ return employeeName; }
set{ employeeName = value; }
}
}
Let us look at the changes need to be done to the “Employee”
class. We add the Meta data information to the employee class so that Gentle.NET
understands that Employee class corresponds to the Employee table in the
database. We also need to map the column names on One-One basis. The Code
written below is in C# and shows the internals of the Gentle.NET. We need to
have Gentle Framework to run the same.
Listing 3
[TableName("Employee")]
public class Employee
{
private int EmployeeId;
private string EmployeeName;
public Employee( int EmployeeId, string EmployeeName )
{
this.EmployeeId = EmployeeId;
this.EmployeeName = EmployeeName;
}
[TableColumn("EmployeeId"), PrimaryKey]
public int Id
{
get{ return EmployeeId; }
set{ EmployeeId = value; }
}
[TableColumn(NotNull=true)]
public string Name
{
get{ return EmployeeName; }
set{ EmployeeName = value; }
}
}
We have the following observations from the Listing 3.
·
An Attribute by the name “[TableName ()]” – This attribute helps
the Gentle framework make a relationship between the business object and Table
name. The name given in the attribute should match with the name of the Table
in the database.
·
An Attribute by the name “[TableColumn()]” – This attribute helps
the Gentle Framework identify the map the column name and the property in the
class. Apart from identifying the column names we can also give more
information like “Primary key” and “Not Null.”
Interaction with the Database – Using Broker class
Let us now understand the interaction with the database with
the same business object employee.
Listing 4
Employee employee = new Employee (42, "FirstName LastName");
Broker.Insert( employee );
// save the Employee to the database
Key key = new Key typeof(Employee), true, "Id", 42 );
// create a key //with a single selection criteria value
employee = Broker.RetrieveInstance( typeof(Employee), key ) as Employee;
// load the specified Employee from the database
We have the following observations from the Listing 4.
·
A class by name Broker acts like an interface that inserts the
new object employee. Broker class has method called as “Insert” which takes
the employee object.
·
A class by the name Key is used in the gentle framework that
helps to create a Key. This key is useful to retrieve the employee object.
·
Broker class has another method called as RetriveInstance. This
method takes 2 parameters: type information of business object and Key
information.
Interaction with the Database – Without Broker class
We can avoid using the Broker class for interacting with the
database. Gentle Framework has an interface called as IPersistent. Gentle has
a class Persistent that implements the interface IPersistent. Business classes
can inherit from Persistent class.
Listing 5
using Gentle.Framework;
[TableName("Employee")]
public class Employee : Persistent
{
private int EmployeeId;
private string EmployeeName;
// this is used by clients to construct new Employees
public Employee( string EmployeeName ) : this( 0, EmployeeName ) {}
// this is used by Gentle to reconstruct objects read from the //database
public Employee( int EmployeeId, string EmployeeName )
{
this.EmployeeId = EmployeeId;
this.EmployeeName = EmployeeName;
}
// this is used by client to fetch Employees from the database
static public Employee Retrieve( int EmployeeId )
{
Key key = new Key( typeof(Employee), true, "Id", EmployeeId );
return Broker.RetrieveInstance( typeof(Employee), key ) as Employee;
}
[TableColumn("EmployeeId"), PrimaryKey(AutoGenerated=true)]
public int Id
{
get{ return EmployeeId; }
set{ EmployeeId = value; }
}
[TableColumn(NotNull=true)]
public string Name
{
get{ return EmployeeName; }
set{ EmployeeName = value; }
}
}
We have the following observations from Listing 5.
Employee class is inherited from the Persistent class. The
Persistent class has a method “Retrieve” which returns the type Employee. It
internally uses Broker class.
Interaction with the Database using Transactions
We can handle Transactions with the Gentle framework. We
have a class by the name Transaction. Let us understand the usage of
Transactions with the help of a code snippet. The Code written below is in C#.
The code below explains the usage of the Transaction class in the Gentle
Framework.
Listing 6
try
{
Employee employee = new Employee();
Transaction transaction = new Transaction();
transaction.Persist( employee );
transaction.Commit();
}
catch( Exception e )
{
// rollback transaction and release database connection
transaction.Rollback();
// rethrow the exception
throw;
}
We have the following observations from Listing 6.
·
Transaction is a class in Gentle Framework which can be used for
the handling transactions with the usage of the Gentle framework.
·
Transaction has a method by the name Persist. This method stores
the values of the object passed in the parameter.
·
Transaction has a method called as commit which commits all the
data base operations.
·
Transactions can be rolled back. The method Rollback helps to
achieve the same.