Click here to Skip to main content
15,867,308 members
Articles / Database Development / SQL Server
Article

Persist Objects with Attributes

Rate me:
Please Sign up or sign in to vote.
2.94/5 (7 votes)
16 Jun 20052 min read 44.5K   305   26   7
Enable persistance and population of objects without writing ADO.NET code.

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:

C#
[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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNewer Version Pin
HormozShahi1-Apr-06 3:54
HormozShahi1-Apr-06 3:54 
GeneralRe: Newer Version Pin
Thomas Lykke Petersen2-Apr-06 8:47
Thomas Lykke Petersen2-Apr-06 8:47 
GeneralBeen done before Pin
worldspawn19-Jun-05 13:25
worldspawn19-Jun-05 13:25 
GeneralRe: Been done before Pin
Thomas Lykke Petersen19-Jun-05 23:11
Thomas Lykke Petersen19-Jun-05 23:11 
GeneralTry XPO of DevExpress.XPO Pin
Anonymous16-Jun-05 8:51
Anonymous16-Jun-05 8:51 
Try XPO of DevExpress.XPO
GeneralRe: Try XPO of DevExpress.XPO Pin
Thomas Lykke Petersen16-Jun-05 20:48
Thomas Lykke Petersen16-Jun-05 20:48 
GeneralRe: Try XPO of DevExpress.XPO Pin
morten Gade5-Dec-06 21:32
morten Gade5-Dec-06 21:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.