Click here to Skip to main content
Click here to Skip to main content

An OO Persistence Approach using Reflection and Attributes in C#

, 9 Jun 2003
Rate this:
Please Sign up or sign in to vote.
Describes how to use C# attributes , ADO.NET and C# to persist objects to databases.

Introduction

Being a J2EE developer in the past and a very big fan of OOD , I started a few months ago to work in .NET. Now I have seen the .NET data persistence approach, the DataSets, the DataReaders and DataAdapters in ADO.NET, which I consider very powerful when working with more that one record in a table, but I still like using Business Objects in my design, so I wrote a small persistence framework for .NET.

Using JDO in the past , I considered making a XML description for each BO to map it to the database tables, but I saw that a better thing would be to use .NET attributes. I have found an article on the Internet with an example of using attributes for such a purpose.

Let’s say we have a BO called User:

public class User
{
   public User(){}
   private long _id;
   private string _name;
   private string _username;
   private string _password;

   //for each of these fields we have a Property
   public long UserID {
      set{_id = value;}
      get {return _id;}
   }

   //etc 
   …     
}

Now let’s map in to the database:

[DBTable("Users","dbo")]
[Serializable]
public class User
{
    public User(){}
    private long _id;
    private string _name;
    private string _username;
    private string _password;

    [DBPrimaryKeyField("ID_User",DbType.Int16)]
    [DBColumn("ID_User",DbType.Int16,false)]
    public long UserID 
    {
        set{_id = value;}
        get {return _id;}
    }

    [DBColumn("Name",DbType.String,true)]
    public string Name
    {
        //set, get
    }

    [DBColumn("Username",DbType.String,false)]
    public string Username
    {
        //set, get
    }

    [DBColumn("Password",DbType.String,false)]
    public string Password
    {
        //set, get
    }
}

We have 3 attribute classes DBTable, DBPrimaryKey, DBColumn. Let’s explain what each does.

  • DBTable(string tableName, string databaseOwner)

    maps the business object to a database table

  • DBPrimaryKey(string pkFieldName, DBType pkFieldType, bool isAutoIncrement)

    maps the Primary column to a BO property

  • DBColumn(string tableFieldName, DBType fieldType, bool allowsNulls)

    maps a table column to a BO property.

Now let’s look at some things that can be made with the BO. This is done using an IPersistenceService interface.

public interface IPersistenceService 
{
    object Create(Object o, Type objectType);
    void Update(Object o, Type objectType);
    void Delete(Object id, Type objectType);
    int Count(string condition, Type objectType);
    Object GetObject(Object id, Type objectType);
    void LoadDataSet(DataSet dataSet, String sqlQuery);
    void LoadDataSet(DataSet dataSet, String sqlQuery, String tableName);
    object Create(SqlConnection con, Object o, Type objectType);
    void Update(SqlConnection con,Object o, Type objectType);
    void Delete(SqlConnection con,Object id, Type objectType);
    int Count(SqlConnection con,string condition, Type objectType);
    Object GetObject(SqlConnection con,Object id, Type objectType);
    void LoadDataSet(SqlConnection con,DataSet dataSet, String sqlQuery);
    void LoadDataSet(SqlConnection con,DataSet dataSet, String sqlQuery,
                     String tableName);
    SqlConnection GetConnection();
    void CloseConnection(SqlConnection con);
}

We have a factory for this interface to get an implementation. In this case the implementation is made against SQL Server in PersistenceServiceImpl. We get an implementation:

String connectionString = “…”;
IPersistenceService ps = PersistenceServiceFactory.getPS(connectionString);

Now let’s create an User and persist it.

User u = new User();
u.Name = "Dan Bunea";
u.Password = "myusername";
u.Password = "mypassword";
//obtain id for the new user in the 
long id = (long)ps.Create(u,typeof(User));

or just: (if you don’t need the ID obtained for the object)

ps.Create(u,typeof(User));

Now that we have a user in the Users table , lets retrieve it to see how that’s done.

User U2 = (User)ps.GetObject(id,typeof(User));

Let’s update it and save the changes.

U2.Name = "Dan Bunea Updates";
U2.Password = "newusername";
U2.Password = "newpassword";
P2s.Update(U2,typeof(User));

Simple J , no? Now let’s delete it.

Ps.Delete(id,typeof(User));

l have also included some methods to load DataSet objects easily, but also if you have more operations that need to use the same connection. Let’s say we want to make all these operations on one connection:

SqlConnection con = ps.GetConnection();
User u = new User();
u.Name = "Dan Bunea";
u.Password = "myusername";
u.Password = "mypassword";
object id = (long)ps.Create(con,u,typeof(User));
User U2 = (User)ps.GetObject(con,id,typeof(User));
U2.Name = "Dan Bunea Updates";
U2.Password = "newusername";
U2.Password = "newpassword";
P2s.Update(con,U2,typeof(User));
Ps.Delete(con,id,typeof(User));
ps.CloseConnection(con);

Let’s talk a little about performance. For each BO the user must not be concerned about INSERT, UPDATE, DELETE and SELECT (for pk) queries. They are automatically generated by the code using the attributes specified in the object. Each statement for each BO is only generated once, and then cached with it’s parameters. The next time the user performs the same operation, the query is taken from cache, also its parameters and using reflection each parameter is given a value, the query is executed.

With special thanks for those that have written other articles that helped me write this, and in the hope that this was a pleasant and useful reading , I finish my small article here.

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

Dan Bunea
Web Developer
Romania Romania
No Biography provided

Comments and Discussions

 
GeneralAbout System.Type PinmemberJohn Gunnarsson20-Jun-03 3:01 
GeneralSome questions... (of who is wanting to understand some thing; -) PinsussMikeboo11-Jun-03 9:53 
GeneralRe: Some questions... (of who is wanting to understand some thing; -) Pinmemberdanbunea11-Jun-03 21:57 
GeneralNice idea but... Pinmemberworldspawn10-Jun-03 20:38 
GeneralRe: Nice idea but... PinmemberDan Bunea10-Jun-03 21:36 
GeneralRe: Nice idea but... Pinmemberworldspawn10-Jun-03 22:03 
GeneralRe: Nice idea but... Pinmemberdanbunea10-Jun-03 22:19 
GeneralRe: Nice idea but... PinmemberBen ThereDunThat17-Jun-03 5:11 
GeneralRe: Nice idea but... PinsussAnonymous18-Jun-03 16:03 
GeneralRe: Nice idea but... PinmemberBen ThereDunThat18-Jun-03 21:40 
GeneralRe: Nice idea but... Prevelance Pinmemberrj4516-Jan-04 18:24 
Generallink don't work Pinmemberpls10-Jun-03 17:28 
GeneralRe: link don't work PinmemberDan Bunea10-Jun-03 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
Web04 | 2.8.140821.2 | Last Updated 10 Jun 2003
Article Copyright 2003 by Dan Bunea
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid