Introduction
Lately I have been looking into simplifying the server side data access code for a personal project of mine, and this led me to read up on some declarative programming principles. Explaining the principles of declarative programming is beyond the scope of this post, but in short it means to declare instead of writing tons of code.
Implementation
Instead of using a traditional O/R mapper, I build a class library containing attributes used to declare the relationship between domain objects and stored procedures. The domain objects should derive from a base class which implements two methods: 'Persist
' and 'Populate
'. These methods are used to parse the attributes and persist or populate the objects.
Example of an object which implements attributes:
[PersistableObject("SetObject", false, "GetObject")]
public class DataObject : PersistableObjectBase
{
private int mID;
private string mName;
private bool mActive;
public DataObject() {}
[PersistableProperty("UserID", PersistablePropertyDatatypes.Int,
PersistablePropertyTypes.Key)]
public int ID
{
get { return mID; }
set { mID = value; }
}
[PersistableProperty("UserName", PersistablePropertyDatatypes.NVarchar,
PersistablePropertyTypes.Field, 50)]
public string Name
{
get { return mName; }
set { mName = value; }
}
[PersistableProperty("Active", PersistablePropertyDatatypes.Bit,
PersistablePropertyTypes.Field)]
public bool Active
{
get { return mActive; }
set { mActive = value; }
}
}
This object derives from 'PersistableObjectBase
', thus you are now able to call the 'Persist
' method to persist the object in the database. When calling the 'Persist
' method, reflection will be used to parse the attributes and call the stored procedure stated in the top attribute 'SetObject
' via regular ADO.NET. All this will happen behind the scenes now. You do not have to write the ADO.NET code anymore for this object unless you have to do some really special operation. Please note that you still have to write stored procedures for INSERT
/UPDATE
and SELECT
. It is not yet supported to delete an object, but it will be implemented in a later version.
The assembly is provided as is, and any use of it is at your own risk, but please feel free to download it and play around with it. I need all the feedback I can get on this, so please get in touch with me if you have some ideas on how to improve it.
Next step
The following has to be implemented in the future in order to improve the usage of the assembly:
- Deletion of object in database.
- Benchmark performance test.