Proetus
Overview
Proetus is a framework for .NET object persistence. Proetus is also a character of Greek mythology (see
Proetus at Wikipedia).
It's best to first describe Proetus by describing what it is not ... Proetus is
not an ORM framework. Proetus
is a framework for the persistence of an object's state to storage. The framework currently implements numerous adapters including those for Microsoft® SQL Server®, Microsoft® Message Queue (i.e. MSMQ), and XML.
Proetus adapters allow objects to be stored, retrieved, deleted, and queried. The syntax of Proetus queries is determined by the adapter used. For Microsoft® SQL Server® persistence Proetus queries are based on TransactSQL. For example ...
SELECT [Order].[PO] AS PO,
[Order].[Delivered] AS DeliveryDate
[Order].[IsDelivered] AS IsDelivered
FROM [Order:MyProject.Library.Order, MyProject]
WHERE [Order].[Customer#] = 'ABC4332'
The query above retrieves the value of properties
PO,
Delivered, and
IsDelivered from objects of type
MyProject.Library.Order where the value of property
Customer# is
ABC4332. The Proetus adapters transform this query into a TransactSQL query that can be executed by the database engine.
For more information on adapters refer to the
Adapters page.
Samples
Simple Object
Let's say you have a class called
Product with four properties ...
Name,
QuantityAvailable,
DateAvailable, and
Cost. In order to support Proetus persistence this class would be implemented as follows ...
public class Product : PersistentObject
{
public Product()
{
Name = null;
QuantityAvailable = null;
DateAvailable = null;
CostOfProduct = null;
}
public Product ( Guid aObjectId )
:base ( aObjectId )
{
}
public String Name
{
get { return Properties.GetProperty<StringProperty> ( "Name" ).Value; }
set { Properties.GetProperty<StringProperty> ( "Name" ).Value = value; }
}
public int? QuantityAvailable
{
get { return Properties.GetProperty<IntegerProperty> ( "QuantityAvailable" ).Value; }
set { Properties.GetProperty<IntegerProperty> ( "QuantityAvailable" ).Value = value; }
}
public DateTime? DateAvailable
{
get { return Properties.GetProperty<DateProperty> ( "DateAvailable" ).Value; }
set { Properties.GetProperty<DateProperty> ( "DateAvailable" ).Value = value; }
}
public decimal? Cost
{
get { return Properties.GetProperty<MoneyProperty> ( "CostOfProduct" ).Value; }
set { Properties.GetProperty<MoneyProperty> ( "CostOfProduct" ).Value = value; }
}
}
Note the following ...
- Classes that support persistence inherit from the PersistentObject class.
- The Properties collection defines the properties persisted and can be setup in the class constructor or the property accessors.
- To support retrieval of persisted objects, a class must implement a constructor that takes a single Guid parameter which it passes to the base class.
- The sample above uses String, int?, DateTime?, and decimal? properties. Other properties are also supported (see the Proetus.Properties namespace or the Properties page).
The following code would then create and save an instance of the class
Product ...
Product lProduct;
lProduct = new Product ();
lProduct.Name = "5 Bars Of Chocolate";
lProduct.QuantityAvailable = 12;
lProduct.DateAvailable = DateTime.Parse ( "5/28/2008" );
lProduct.Cost = ( decimal? )( 12.99 );
SqlObjectAdapter lObjectAdapter;
lObjectAdapter = new SqlObjectAdapter ();
try
{
lObjectAdapter.SaveObject ( lProduct );
lObjectAdapter.CommitChanges ();
}
catch
{
lObjectAdapter.RollbackChanges ();
throw;
}
To query and retrieve an instance of the class
Product the following code would be used ...
NOTE: The class methods of the
QueryAdapter and
ObjectAdapter classes encapsulate the
try-catch and
commit-rollback blocks seen in the code above.
String lQuery;
lQuery = @"
SELECT [Product]
FROM [Product:MyProject.Product, MyProject]
WHERE [Product].[Name] = '5 Bars Of Chocolate'";
DataSet lData;
lData = ( ( SqlQueryResults )( QueryAdapter.ExecuteQueryUsing ( new SqlQueryAdapter (), new SqlQueryParameters ( SqlAdapter.SqlAdapterContext.CreateCommand ( lQuery, CommandType.Text ) ) ) ) ).Data;
Guid lObjectId;
lObjectId = ( Guid )( lData.Tables [ 0 ].Rows [ 0 ][ "ObjectId" ] );
Product lProduct;
lProduct = ( Product )( ObjectAdapter.GetObjectUsing ( new SqlObjectAdapter (), lObjectId ) );
See Also