Click here to Skip to main content
Click here to Skip to main content
Go to top

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.

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.

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

Share

About the Author

Thomas Lykke Petersen
Web Developer
Denmark Denmark
No Biography provided

Comments and Discussions

 
GeneralNewer Version PinmemberHormozShahi1-Apr-06 3:54 
GeneralRe: Newer Version PinmemberThomas Lykke Petersen2-Apr-06 8:47 
GeneralBeen done before Pinmemberworldspawn19-Jun-05 13:25 
GeneralRe: Been done before PinmemberThomas Lykke Petersen19-Jun-05 23:11 
GeneralTry XPO of DevExpress.XPO PinsussAnonymous16-Jun-05 8:51 
GeneralRe: Try XPO of DevExpress.XPO PinmemberThomas Lykke Petersen16-Jun-05 20:48 
GeneralRe: Try XPO of DevExpress.XPO Pinmembermorten Gade5-Dec-06 21:32 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 16 Jun 2005
Article Copyright 2005 by Thomas Lykke Petersen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid