5,550,131 members and growing! (19,069 online)
Email Password   helpLost your password?
Database » Database » Databases     Intermediate

An OO Persistence Approach using Reflection and Attributes in C#

By Dan Bunea

Describes how to use C# attributes , ADO.NET and C# to persist objects to databases.
C++, C#, .NET, Win2K, WinXP, Win2003, Windows, Visual Studio, MFC, Dev

Posted: 9 Jun 2003
Updated: 9 Jun 2003
Views: 76,818
Bookmarked: 38 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
36 votes for this Article.
Popularity: 5.31 Rating: 3.41 out of 5
6 votes, 16.7%
1
2 votes, 5.6%
2
6 votes, 16.7%
3
9 votes, 25.0%
4
13 votes, 36.1%
5

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

About the Author

Dan Bunea



Occupation: Web Developer
Location: Romania Romania

Other popular Database articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 13 of 13 (Total in Forum: 13) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralAbout System.TypememberJohn Gunnarsson4:01 20 Jun '03  
GeneralSome questions... (of who is wanting to understand some thing; -)sussMikeboo10:53 11 Jun '03  
GeneralRe: Some questions... (of who is wanting to understand some thing; -)memberdanbunea22:57 11 Jun '03  
GeneralNice idea but...memberworldspawn21:38 10 Jun '03  
GeneralRe: Nice idea but...memberDan Bunea22:36 10 Jun '03  
GeneralRe: Nice idea but...memberworldspawn23:03 10 Jun '03  
GeneralRe: Nice idea but...memberdanbunea23:19 10 Jun '03  
GeneralRe: Nice idea but...memberBen ThereDunThat6:11 17 Jun '03  
GeneralRe: Nice idea but...sussAnonymous17:03 18 Jun '03  
GeneralRe: Nice idea but...memberBen ThereDunThat22:40 18 Jun '03  
GeneralRe: Nice idea but... Prevelancememberrj4519:24 16 Jan '04  
Generallink don't workmemberpls18:28 10 Jun '03  
GeneralRe: link don't workmemberDan Bunea22:32 10 Jun '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Jun 2003
Editor: Smitha Vijayan
Copyright 2003 by Dan Bunea
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project