Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

Object Mapping Using Reflection

4.11/5 (8 votes)
28 Oct 20052 min read 1   410  
Object mapping using reflection, with C# and SQL Server.

Introduction

As a developer I have been plagued with mapping my domain objects to a persistent data source (or simply database). Throughout the development process many things can change based on the changing requirements. These changes force the developer to modify both the source code of the application as well as the database schema, and this is where I get frustrated. During development, I don't like my changes to have a great degree of impact on other developers. For instance, changing a table's structure may require modifications to stored procedures, embedded dynamic SQL, and code. In a utopian development environment, everything should be done for me when I change the table's structure. Hence OMUR was born.

Object Mapping Using Reflection (OMUR)

The premise behind OMUR is simple, map a C# object to a database table using Reflection and system tables. OMUR is built on top of SQL Server 2000, and uses C# (.NET 1.1). OMUR stores and maps a given object to a database table. In order for OMUR to work properly, your table names must match your class names, and your column names must match your class' properties. And you will need at least SELECT authority on the system tables within your desired database for OMUR to work properly. As OMUR builds a data map for a given domain object, it will loop through that object's properties mapping them to the columns in your table. If a column in your table does not exist in your object, it will be ignored.

How to use OMUR

Using OMUR is simple.

First, create your tables. For this example, we will create two tables, one for storing members, and one for storing when each member logs in.

SQL
CREATE TABLE dbo.Member
(
 memberId int not null identity(100000, 1),
 memberName varchar(255) not null,
 primary key (memberId)
)
GO

CREATE TABLE dbo.MemberLogin
(
 memberId int not null,
 memberLogin datetime not null,
 memberLoginUtc datetime not null,
 primary key (memberId, memberLogin)
)
GO

Next we need to create some objects that are going to use this data. Remember, your class / property names must match the table / column names exactly. Also, you'll notice some helper functions that come in handy when populating or saving these two objects, primarily the Get and Save functions.

C#
 public class Member
 {
  #region Properties

  public int memberId
  {
   get { return m_memberId; }
   set { m_memberId = value; }
  }
  private int m_memberId;

  public string memberName
  {
   get { return m_memberName; }
   set { m_memberName = value; }
  }
  private string m_memberName;

  #endregion

  #region Methods

  public void Save()
  {
   OMUR.DataFactory.Save(this);
  }

  public static Member Get(int id)
  {
   return (Member)OMUR.DataFactory.Get(typeof(Member), id);
  }

  public static Member GetByName(string name)
  {
   return (Member)OMUR.DataFactory.Find(typeof(Member), 
                     "memberName = @memberName", name);
  }

  #endregion
 }


 public class MemberLogin
 {
  #region Properties

  public int memberId
  {
   get { return m_memberId; }
   set { m_memberId = value; }
  }
  private int m_memberId;

  public DateTime memberLogin
  {
   get { return m_memberLogin; }
   set { m_memberLogin = value; }
  }
  private DateTime m_memberLogin;

  public DateTime memberLoginUtc
  {
   get { return m_memberLoginUtc; }
   set { m_memberLoginUtc = value; }
  }
  private DateTime m_memberLoginUtc;

  #endregion

  #region Methods

  public void Save()
  {
   OMUR.DataFactory.Save(this);
  }

  public static MemberLogin Get(int id, DateTime dt)
  {
   return (MemberLogin)OMUR.DataFactory.Get(typeof(MemberLogin), id, dt);
  }

  #endregion
 }

Finally, how do we use this code:

C#
class Program
 {
  [STAThread]
  static void Main(string[] args)
  {
   Member m = new Member();
   m.memberName = "Stu";
   m.Save();

   Console.WriteLine("New Member ID: {0}", m.memberId);

   MemberLogin ml = new MemberLogin();
   ml.memberId = m.memberId;
   ml.memberLogin = DateTime.Today;
   ml.memberLoginUtc = DateTime.UtcNow;
   ml.Save();

   ml = null;

   ml = MemberLogin.Get(m.memberId, DateTime.Today);

   Console.WriteLine("Login Today: {0:yyyy/MM/dd HH:mm}", 
                                         ml.memberLogin);
   Console.WriteLine("Login UTC  : {0:yyyy/MM/dd HH:mm}", 
                                      ml.memberLoginUtc);
  }
 }

Output:

New Member ID: 100000

Login Today: 2005/10/28 00:00

Login UTC: 2005/10/28 11:23

Conclusion: What OMUR needs, and What OMUR is not

OMUR needs a robust way to select a list from joined tables. Currently I have not taken the time to design or contemplate the issue. Also to make OMUR a little stronger, it should be database independent. Although currently, the only database I need to access is MS SQL Server.

OMUR is not a robust enterprise wide object mapping library. OMUR's is intended to be used on mid to small level projects.

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