If you find this article useful, consider making a small donation to show your support for this web site and its content.
Free app Developer Interview available here.

Available on the iPhone App Store
Available on the Google Play
AboutMe
About me:
Hi. My name is Farooq Kaiser and I'm a software developer from Toronto, Canada.



Data Access for high performance application

by Farooq Kaiser 13. June 2012 01:52

Updated :- To compare performance benchmarks, Here is an interesting article. Data Access Performance Comparison in .NET

Recently i was working on a project where application performance was a top priority. I started looking into ORM frameworks and found that ORMs are great for most applications, but when performance is a factor it�s best to write your own data access. So that's when I decided to write my own data access. This article will explain some of the design that i have used in my project. I have used SqlDataReader�s strongly typed Get methods to do a null check and then write the values. Here is my base class in VB.NET.

   1:  Public MustInherit Class BaseDAL(Of T As Class)
   2:   
   3:      Public fieldOrdinals As Dictionary(Of String, Integer) = Nothing
   4:      Private _Reader As IDataReader = Nothing
   5:   
   6:      Public Overridable Sub GetFieldOrdinalsFromReader()
   7:          fieldOrdinals = New Dictionary(Of String, Integer)()
   8:      End Sub
   9:   
  10:      Public Overridable Function FillFromReader(ByVal sqlReader As IDataReader, ByRef instance As T)
  11:          If (IsNothing(fieldOrdinals)) Then
  12:              _Reader = sqlReader
  13:              GetFieldOrdinalsFromReader()
  14:          End If
  15:          Return instance
  16:      End Function
  17:   
  18:      Public Function ParseInteger(ByVal column As String) As Integer
  19:          Dim result As Integer = 0
  20:   
  21:          Try
  22:              Dim ordinal As Integer = 0
  23:              If (fieldOrdinals.Keys.Contains(column)) Then
  24:                  ordinal = fieldOrdinals(column)
  25:                  result = If(_Reader.IsDBNull(ordinal), Nothing, _Reader.GetInt32(ordinal))
  26:              End If
  27:          Catch ex As Exception
  28:   
  29:          End Try
  30:   
  31:          Return result
  32:      End Function
  33:   
  34:      Public Function ParseString(ByVal column As String) As String
  35:          Dim result As String = Nothing
  36:   
  37:          Try
  38:              Dim ordinal As Integer = 0
  39:              If (fieldOrdinals.Keys.Contains(column)) Then
  40:                  ordinal = fieldOrdinals(column)
  41:                  result = If(_Reader.IsDBNull(ordinal), Nothing, _Reader.GetString(ordinal))
  42:              End If
  43:          Catch ex As Exception
  44:   
  45:          End Try
  46:   
  47:          Return result
  48:      End Function
  49:   
  50:      Public Function ParseDate(ByVal column As String) As DateTime
  51:          Dim result As DateTime = Nothing
  52:   
  53:          Try
  54:              Dim ordinal As Integer = 0
  55:              If (fieldOrdinals.Keys.Contains(column)) Then
  56:                  ordinal = fieldOrdinals(column)
  57:                  result = If(_Reader.IsDBNull(ordinal), Nothing, _Reader.GetDateTime(ordinal))
  58:              End If
  59:          Catch ex As Exception
  60:   
  61:          End Try
  62:   
  63:          Return result
  64:      End Function
  65:   
  66:      Public Function ParseBoolean(ByVal column As String) As Boolean
  67:          Dim result As Boolean = False
  68:   
  69:          Try
  70:              Dim ordinal As Integer = 0
  71:              If (fieldOrdinals.Keys.Contains(column)) Then
  72:                  ordinal = fieldOrdinals(column)
  73:                  result = If(_Reader.IsDBNull(ordinal), False, _Reader.GetBoolean(ordinal))
  74:              End If
  75:          Catch ex As Exception
  76:   
  77:          End Try
  78:   
  79:          Return result
  80:      End Function
  81:   
  82:      Public Function SetFieldOrdinalsFromReader(ByVal column As String) As Boolean
  83:          Dim ordinal As Integer = -1
  84:          Try
  85:              ordinal = _Reader.GetOrdinal(column)
  86:              fieldOrdinals.Add(column, ordinal)
  87:          Catch
  88:              ' Column not in reader, so don't track the field.
  89:          End Try
  90:          Return ordinal > -1
  91:      End Function
  92:   
  93:  End Class
Now i will build actual class that will inherit from base class, I'm also using Microsoft application access block. for data access.
   1:  Public Class PersonDB
   2:      Inherits BaseDAL(Of Person)
   3:   
   4:  Public Function GetAllPersons() As List(of Person)
   5:  dim myPersons as new List(of Person)
   6:  Dim defaultDB As Database = Helpers.GetDataBaseInstance()
   7:            Using cmdOrder As DbCommand = defaultDB.GetStoredProcCommand("SP_Name")
   8:               Using sqlReader As IDataReader = defaultDB.ExecuteReader(cmdOrder)
   9:                  While sqlReader.Read()
  10:                          Dim myPerson As New Person
  11:                          myPersons.Add(FillFromReader(sqlReader, myPerson))
  12:                      End While
  13:               End Using
  14:            End Using
  15:  Return myPersons
  16:  End Function
  17:   
  18:  Public Overrides Function FillFromReader(ByVal sqlReader As IDataReader, ByRef myPerson As Person)
  19:          MyBase.FillFromReader(sqlReader, myOrder)
  20:          myPerson.PersonId = ParseInteger("PersonId")
  21:          myPerson.FullName = ParseString("FullName")
  22:          myPerson.Address = ParseString("Address")
  23:          myPerson.CreatedDate = ParseDate("CreatedDate")
  24:          Return myPerson
  25:   End Function
  26:   
  27:  Public Overrides Sub GetFieldOrdinalsFromReader()
  28:         MyBase.GetFieldOrdinalsFromReader()
  29:         SetFieldOrdinalsFromReader("PersonId")
  30:         SetFieldOrdinalsFromReader("FullName")
  31:         SetFieldOrdinalsFromReader("Address")
  32:         SetFieldOrdinalsFromReader("CreatedDate")
  33:   End Sub
  34:  End Class

My base class has 2 methods SetFieldOrdinalsFromReader and FillFromReader that are used in a DAL class, SetFieldOrdinalsFromReader will assign database columns to fieldOrdinals which is a dictionary object and the FillFromReader will populate the person object with the help of helper methods that use SqlDataReader�s strongly typed Get methods. Here is my person class.

   1:  Public Class Person
   2:  Private _PersonId As Integer =0
   3:  Public Property PersonId() As Integer
   4:          Get
   5:              Return _PersonId
   6:          End Get
   7:          Set(ByVal value As Integer)
   8:              _PersonId = value
   9:          End Set
  10:  End Property
  11:   
  12:  Private _FullName As String 
  13:  Public Property FullName() As String
  14:          Get
  15:              Return _FullName
  16:          End Get
  17:          Set(ByVal value As String)
  18:              _FullName = value
  19:          End Set
  20:  End Property
  21:   
  22:  Private _Address As String 
  23:  Public Property Address() As String
  24:          Get
  25:              Return _Address
  26:          End Get
  27:          Set(ByVal value As String)
  28:              _Address = value
  29:          End Set
  30:  End Property
  31:   
  32:  Private _CreatedDate As Date 
  33:  Public Property CreatedDate() As Date
  34:          Get
  35:              Return _CreatedDate
  36:          End Get
  37:          Set(ByVal value As Date)
  38:              _CreatedDate = value
  39:          End Set
  40:  End Property
  41:  End Class

Please note, the above code is written for queries to read data only, and if you would like to insert/update a bulk data, then you can read my last article here. Please feel free to leave any comments and feedback.

Currently rated 1.8 by 5 people

  • Currently 1.8/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:


comments powered by Disqus
Jobs Autos Real estate Videos Power by Google