I created this library because most of the times when I am designing a database and data layer, they end up looking almost identical. For example, I may have the following table in the database:
tblCustomer
------------
CustomerID int
FirstName varchar(50)
LastName varchar(50)
and the following class in the data layer (shortened for time):
public class Customer
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
So what this library does is to map a database table to a class. To use this library, you only need to do a few things.
- Inherit from HydrationBase (this probably could be done away with…just make changes to the HydrationFactory so that it accepts object types. In the initial version of this program, HydrationBase generated all the SQL with every constructor call…but that’s a waste of time when you really only need to generate it once)
- Add the HydratorParentTable attribute to your class
- Add the HydratorColumn attribute to your properties
- Make your PrimaryIndex property default to -1. They hydrator requires that brand new objects have their primary index property set to -1. It will check when you call .Save() whether it is -1 or something else, and depending on that, it will call an INSERT statement or an UPDATE statement.
Here is the “Customer” class with Hydrator support:
[HydratorParentTable("tblCustomer")]
public class Customer
{
[HydratorColumn("CustomerID", IsAutoNumber=true, PrimaryIndex=true)]
public int CustomerID { get; set; }
[HydratorColumn("FirstName")]
public string FirstName { get; set; }
[HydratorColumn("LastName")]
public string LastName { get; set; }
}
Now if we wanted to use this code, we could do this:
// load customerId 1
Customer customer = new Customer();
customer.CustomerID = 1;
customer.Load();
// to create a new customer
Customer customer = new Customer();
customer.FirstName = “Frank”;
customer.LastName = “Smith”;
customer.Save();
// to alter an existing one
Customer customer = new Customer();
customer.CustomerID = 1;
customer.Load();
customer.FirstName = “NewName”;
customer.Save();
// To delete
customer.Delete();
You can set your connection string in the app.config file (see the hydrator project to see how). You can also set it programatically by calling:
DB.RegisterConnection("DEFAULT", "my connectionstring here");
It will use the “DEFAULT” connection string if no other is provided. This does allow for supporting multiple connections though. I realize that most likely no one will ever have multiple databases with the exact same table structure, but there was this one place I worked that did it that way so that they could detach the database and make it mobile. I suppose if you were doing database copies you could do that too — but it would be faster just to write some SQL statements to do it. Regardless, here is the code for how to do that.
DB.RegisterConnection("conn1", "...");
DB.RegisterConnection("conn2", "...");
Customer cust = new Customer();
cust.CustomerID = 1;
cust.Load(”conn1″);
There are a few more things that may warrant discussion. In the HydratorParentTable attribute, you can set the “IsReadOnly” option to true to disallow Save and Delete. The Hydrator also support an expiration model. I don’t really like this, and should probably take it out, but here is the concept behind it. Some things in tables you never want to delete. You simply want to mark them as “deleted” and maybe put a “deleted timestamp.” That’s what this option is all about. Calling “Delete” on these will just mark it as deleted, set the expiration timestamp, and move on. I’m not even going to discuss it anymore, as I doubt it’s a completed idea.