Data transfer objects

RahulSisodia

New member
Joined
Nov 25, 2009
Messages
3
Programming Experience
5-10
I have a query about implementing Data Transfer Objects.

I have a common interface but their implementation differs at the DAL level. Please consider below example.

Webpage calls a Service to get list of addresses by postcode. One service/interface implementation calls our Core database and generates the list of Address as business entity and passes back to the webpage.
Now another service implements the same interface and returns the data into datatable or say multiple byref string values; my requirement is I do not want to change my webpage implementation; want to write a wrapper which will convert datatable into Address business entity? I want to write a generic mechanism for this so that different implementations can be handled intelligently rather than mapping column to business properties.

Regards,
Rahul.
 
If I'm reading your description properly then you want to use a bit of generics and reflection:
VB.NET:
Private Function GetListFromTable(Of T As New)(ByVal table As DataTable) As List(Of T)
    'Get a Type object to represent the type of the items we're creating.
    Dim entityType = GetType(T)

    'Store the item type's properties against their corresponding column from the table.
    Dim properties As New Dictionary(Of DataColumn, PropertyInfo)

    'Get the item properties by column name.
    For Each column As DataColumn In table.Columns
        properties.Add(column, entityType.GetProperty(column.ColumnName))
    Next

    Dim items = New List(Of T)

    For Each row As DataRow In table.Rows
        'Create a new item for the current row.
        Dim item As New T

        'Populate the item's properties from the row's fields.
        For Each column As DataColumn In table.Columns
            properties(column).SetValue(item, _
                                        row(column), _
                                        Nothing)
        Next

        items.Add(item)
    Next

    Return items
End Function
That code assumes that each column in the DataTable has a name that matches a property of the DTO type you're creating. That may not always be the case, but you could work on that code to make it more flexible and robust. You might call that method like this:
VB.NET:
Dim products As List(Of Product) = GetListFromTable(Of Product)(myDataSet.Tables("Product"))
 
Hi jmcilhinney,

Thank you for your update. The only thing worries me is that I have no control over the datatable as this would be coming form 3rd party; in some instances they will send datatables, some instances there own entity objects ans in some instances byref string values; any idea how I can generalise this method, to cater for above 3 requirements.

Regards,
Rahul.
 
ok, I dont mind writing 3 separate routines...but could you please tell me what is the ebst way to map their entity objects with ours?

I mean lets say; their product object has 10 properties while mine has 7; how I am supposed to map these? Same with datatable; their table returns 8 column but I have only 5 properties in my object...well I can always map one to one properties....I am struggling to understand what would be the best way to tackle this problem?

Thanks & Regards
 

Latest posts

Back
Top