Click here to Skip to main content
13,004,203 members (113,027 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 16 Jun 2005

Persist Objects with Attributes

, 16 Jun 2005
Rate this:
Please Sign up or sign in to vote.
Enable persistance and population of objects without writing ADO.NET code.


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.


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, 
    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, 
    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.


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


About the Author

Thomas Lykke Petersen
Web Developer
Denmark Denmark
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralNewer Version Pin
HormozShahi1-Apr-06 3:54
memberHormozShahi1-Apr-06 3:54 
GeneralRe: Newer Version Pin
Thomas Lykke Petersen2-Apr-06 8:47
memberThomas Lykke Petersen2-Apr-06 8:47 
GeneralBeen done before Pin
worldspawn19-Jun-05 13:25
memberworldspawn19-Jun-05 13:25 
GeneralRe: Been done before Pin
Thomas Lykke Petersen19-Jun-05 23:11
memberThomas Lykke Petersen19-Jun-05 23:11 
GeneralTry XPO of DevExpress.XPO Pin
Anonymous16-Jun-05 8:51
sussAnonymous16-Jun-05 8:51 
GeneralRe: Try XPO of DevExpress.XPO Pin
Thomas Lykke Petersen16-Jun-05 20:48
memberThomas Lykke Petersen16-Jun-05 20:48 
GeneralRe: Try XPO of DevExpress.XPO Pin
morten Gade5-Dec-06 21:32
membermorten 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170627.1 | Last Updated 16 Jun 2005
Article Copyright 2005 by Thomas Lykke Petersen
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid