ColumnMapping - Explicitly exclude a column

  • Thread starter Thread starter Jonathan Orgel
  • Start date Start date
J

Jonathan Orgel

In the middle tier I want to exclude some columns passed on to the client
layer. I would like to exclude a specific column from a datamap without
needing to specify all the column I do want. At design time I will not know
what the columns will be included in the select statement.

Any suggestion how to do this?

Thanks

Jonathan Orgel
 
Perhaps it would be best to leave the datatable alone and when binding data
to a grid or repeater with columns, handle user's request there. Certainly
it is not as easy as typing dbGrid.datasource = myfunctionresult(). You
will have to specify a template, but you can control that template via the
user's request.

It is the responsibility of the business logic to manage the relation of the
data and user, it is not the responsibility of the business logic to replace
the UI logic.
 
Fair point.

However I am trying to implement some auditing which I want to effectively
hide from the client.
The client does a select * from table_x and I do not want the 'hidden'
audting columns to appear in the client's datatables.
 
I can only assume what you have going on in your code, so I will throw out
an example or two and challenge you to think along those lines to find your
solution. I don't know of any articles off hand that will directly answer
your question, I do recommend that you searchh the code project, wikipedia
and the like.

First of all, this is assuming that you are using the traditional tiers as
they were intended.
Next, set your datagrid to not AutoGenerateColumns

I realize there is a lot of code here and apologize to all the modem users
out there.

In the example you will see that there is a Form, a UI Presenter, a Business
Layer, and a Data Manager.

The target data that we are acting against will be a Contacts table that has
ContactName, DateOfBirth and a LastModifiedBy data columns. LastModifiedBy
is the column we wish to exclude from the user's awareness.

The form passes the user requests to the UI Presenter.
The Presenter passes the request to the Business Layer
The Bussiness layer then requests data from the data manager
The bussiness layer then sends the result back to the UI Presenter
The UI Presenter decides what to do with the data
The Form then reflects the changes

There are two options for the user that net the same result for the user and
has different implications for the developer. The first is for the user to
populate the grid based on a view that excludes the audit column, the second
option retrieves all the columns from the table directly and then filters
the audit column.

I believe that this implementation is unit test ready for nUnit or the like.
Create test cases for the ContactBrowseUI and ContactLogic objects and you
are set.

As to not knowing what columns are in the tables at run time, I would have
to say that your business logic is too loosely coupled from the data layer
and you should reign in it a tad.

Public Class Form1

Private pobjCBUI As ContactBrowseUI = New ContactBrowseUI

Private Sub cmdRefreshContacts_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles cmdRefreshContacts.Click
pobjCBUI.RefreshContacts(dbgContacts)
End Sub

Private Sub cmdRefreshContacts1_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles cmdRefreshContacts1.Click
pobjCBUI.RefreshContacts1(dbgContacts)
End Sub

End Class

Public Class ContactBrowseUI

Private pobjContactLogic As ContactLogic = New ContactLogic

Public Sub RefreshContacts(ByVal dbgContacts As DataGridView)

'Clear the datagrid
dbgContacts.DataSource = Nothing

'Fetch the contacts
dbgContacts.DataSource = pobjContactLogic.FetchContacts

End Sub

Public Sub RefreshContacts1(ByVal dbgContacts As DataGridView)

Dim ContactNameDataGridViewTextBoxColumn As
System.Windows.Forms.DataGridViewTextBoxColumn
Dim DateOfBirthDataGridViewTextBoxColumn As
System.Windows.Forms.DataGridViewTextBoxColumn

'Initialize our cell objects
ContactNameDataGridViewTextBoxColumn =
CreateTextBoxColumn("ContactName", "ContactName",
"ContactNameDataGridViewTextBoxColumn")
DateOfBirthDataGridViewTextBoxColumn =
CreateTextBoxColumn("DateOfBirth", "DateOfBirth",
"DateOfBirthDataGridViewTextBoxColumn")

'Clear the datagrid
dbgContacts.DataSource = Nothing

'Fetch the contacts
With dbgContacts

'Do not allow the grid to auto generate it's members
dbgContacts.AutoGenerateColumns = False

'Clear all the cells
dbgContacts.Columns.Clear()

'Add the cells we want to use
dbgContacts.Columns.AddRange(New
System.Windows.Forms.DataGridViewColumn() _
{ContactNameDataGridViewTextBoxColumn, _
DateOfBirthDataGridViewTextBoxColumn})

'Fetch the data and bind it to the grid
dbgContacts.DataSource = pobjContactLogic.FetchContacts1

End With

End Sub

Private Function CreateTextBoxColumn(ByVal propertyName As String, ByVal
headerText As String, ByVal name As String) As DataGridViewTextBoxColumn

Dim objReturn As System.Windows.Forms.DataGridViewTextBoxColumn = New
System.Windows.Forms.DataGridViewTextBoxColumn

With objReturn
.DataPropertyName = propertyName
.HeaderText = headerText
.Name = name
End With

Return objReturn

End Function

End Class

Public Class ContactLogic


Public Function FetchContacts() As DataTable
'Return all rows from the contacts view

Dim sSQL As String = "Select * from ContactsView"

'Request our data manager to retrieve our data
' we will pass in the command text
' and not paramaters

' selecting against the view will not return
' any of the audit columns on the table

Return myGlobalDataManager.FetchData(sSQL, CommandType.Text, Nothing)

End Function

Public Function FetchContacts1() As DataTable

'Return all rows from the contacts table
Dim sSQL As String = "Select * from Contacts"

'Request our data manager to retrieve our data
' we will pass in the command text
' and not paramaters

' selecting against the table will return
' any of the audit columns on the table
Return myGlobalDataManager.FetchData(sSQL, CommandType.Text, Nothing)

End Function

End Class

Public Class myGlobalDataManager

Public Shared Function FetchData(ByVal sSQL As String, ByVal commandType
As Data.CommandType, ByVal params As ArrayList) As DataTable
Return Nothing
End Function

End Class
 
Back
Top