Generic DAL avoiding Reflection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Lindhe
    New Member
    • Oct 2010
    • 1

    #1

    Generic DAL avoiding Reflection

    I'm working on a new DAL for my latest project. As the performance is essential I'm avoiding late binding/reflection as much as possible.

    One central part is the DAL function for populating Business Objects. One class is called in order to connect to the database with the given SQL (stored procedures can't be used in this environment) and the given Business Object or List is populated. As different Business Object types will be populated by the same function I made an Interface that all of the Business Objects implements with the Sub called "Fill". The question is - will reflection be used in this scenario? My thought was as I'm "describing " the objects trought the interface the compiler doesn't need to use Reflection. Perhaps I'm all wrong here..

    Simplified code:

    Code:
    Object Handler:
    
    With New DAL.DataManager
         .Populate(Of T)(sSQL)
    End With
    
    Business Objects:
    
    Public Class Customer
         Implements DAL.EntityInterface
    
         Public Sub Fill(Reader as sqlClient.SQLDataReader) Implements DAL.EntityInterface.Fill
    
                CustomerID = Reader.Item("CustomerID"))
                CustomerName = Reader.Item("CustomerName"))
                ... and so on
         End Sub
    End Class
    
    Interface:
    
    Namespace DAL
        Public Interface EntityInterface
            Sub Fill(Reader as sqlClient.SQLDataReader)
        End Interface
    End Namespace
    
    DataManager:
    
    Public Class DataManager
         Public Function Populate(Of T As {DAL.EntityInterface, New})(ByVal sSQL As String) As T
    
         Reader = New DAL.GetReader(sSQL)
              If Not IsNothing(Reader) Then
                   Populate.Fill(Reader)
              End IF
         End Function
    
    End Class
    So with other words, I want to have many different Business Objects (Class definitions with Public Properties) and each Business Object Type has it's own Handler Class that will call DAL and with provided SQL DAL will create the corresponding Business OBject and fill it using the Sub defined in the Interface. And this without using Reflection (or at leats not any expensive Reflection). Any thought or alternative solutions? The important part is that I don't want to separate Business Object Handlers(SQL etc), Business Objects (Class definitions) from DAL but still get good performance.

    Thanks in advance!

    Regards
    Tom
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    You could use Reflection for populating the Business Objects with the data retrieved from the database; however, the Business Object properties would have to match the names of the columns in the table or else you would have to implement some way to map the column names to the property names.

    -Frinny

    Comment

    • TomLindhe

      #3
      Hi Frinavale

      Thanks. But the whole point is to avoid using reflection in order to speed up performance. So the question is more reagrding the use of Interface in order to accomplish that.

      //Tom

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        I guess your left with making your Data Access Layer simply returns the data retrieved to the Business Logic Class and leave it up to that class to know how to populate itself with the data (without using Reflection).

        -Frinny

        Comment

        Working...