The DbDataAdapter defines a standard interface to manage read, write and delete actions against a data source from a single class. The DbDataAdapter uses the DbConnection and DbCommand classes to access and manage the data. In today’s post we will create the AdDataAdapter so that we can more easily manage Active Directory data.
- First Post: Active Directory Data Access Layer
- Second Post: Active Directory Connection Strings
- Third Post: AdConnection: Enforcing Active Directory Communication Best Practices
- Fourth Post: AdCommandTextParser: Parsing SQL Statements
- Fifth Post: AdCommand: Running Active Directory Queries
- Sixth Post: AdDataReader: Providing Controlled Access to AD Values
- Seventh Post: AdDataAdapter: Managing Active Directory Data
- Eighth Post: AD Query: Putting it All Together
AdDataAdapter Class Diagram
The AdDataAdapter has four routines that accept an AdCommand each with a specific purpose. The CommandTextValidator will be used to ensure that each method supports the correct type of operation.
The implementation for today’s class diagram is based upon the MSDN article, Implementing a DataAdapter, which does an excellent job of explaining how to implement the DbDataAdapter.
AdRowUpdatingEventArgs
Namespace Data.ActiveDirectory Public Class AdRowUpdatingEventArgs Inherits System.Data.Common.RowUpdatingEventArgs ''' <summary> ''' Instantiates a new instance of AdRowUpdatingEventArgs class. ''' </summary> ''' <param name="row"></param> ''' <param name="command"></param> ''' <param name="statementType"></param> ''' <param name="tableMapping"></param> ''' <remarks></remarks> Public Sub New(ByVal row As DataRow, ByVal command As IDbCommand, ByVal statementType As StatementType, ByVal tableMapping As System.Data.Common.DataTableMapping) MyBase.New(row, command, statementType, tableMapping) End Sub Public Shadows Property Command As AdCommand Get Return CType(MyBase.Command, AdCommand) End Get Set(ByVal value As AdCommand) MyBase.Command = value End Set End Property End Class End Namespace
AdRowUpdatedEventArgs
Namespace Data.ActiveDirectory Public Class AdRowUpdatedEventArgs Inherits System.Data.Common.RowUpdatedEventArgs ''' <summary> ''' Instantiates a new instance of AdRowUpdatingEventArgs class. ''' </summary> ''' <param name="row"></param> ''' <param name="command"></param> ''' <param name="statementType"></param> ''' <param name="tableMapping"></param> ''' <remarks></remarks> Public Sub New(ByVal row As DataRow, ByVal command As IDbCommand, ByVal statementType As StatementType, ByVal tableMapping As System.Data.Common.DataTableMapping) MyBase.New(row, command, statementType, tableMapping) End Sub Public Shadows ReadOnly Property Command As AdCommand Get Return CType(MyBase.Command, AdCommand) End Get End Property End Class End Namespace
AdDataAdapter
Namespace Data.ActiveDirectory ''' <summary> ''' Represents a set of data commands and a database connection that are used to fill the DataSet and update an Active Directory domain. ''' </summary> ''' <remarks> ''' MSDN Implementing a DataAdapter: http://msdn.microsoft.com/en-us/library/08a1x80z(v=vs.71).aspx ''' MSDN TemplateDataAdapter.vb: http://msdn.microsoft.com/en-us/library/h15e2yde(v=vs.71).aspx ''' </remarks> Public Class AdDataAdapter Inherits System.Data.Common.DbDataAdapter Implements System.Data.IDbDataAdapter #Region "Event Section" ''' <summary> ''' Occurs during Update before a command is executed against the data source. The attempt to update is made, so the event fires. ''' </summary> ''' <remarks></remarks> Public Event RowUpdating As AdRowUpdatingEventHandler ''' <summary> ''' Occurs during Update after a command is executed against the data source. The attempt to update is made, so the event fires. ''' </summary> ''' <remarks></remarks> Public Event RowUpdated As AdRowUpdatedEventHandler Public Delegate Sub AdRowUpdatingEventHandler(ByVal sender As Object, ByVal e As AdRowUpdatingEventArgs) Public Delegate Sub AdRowUpdatedEventHandler(ByVal sender As Object, ByVal e As AdRowUpdatedEventArgs) '* Inherit from Component through DbDataAdapter. The event '* mechanism is designed to work with the Component.Events '* property. These variables are the keys used to find the '* events in the components list of events. Private Shared ReadOnly EventRowUpdated As Object = New Object() Private Shared ReadOnly EventRowUpdating As Object = New Object() #End Region #Region "Constructor Section" ''' <summary> ''' Instantiates a new instance of AdDataAdapter. ''' </summary> ''' <remarks></remarks> Public Sub New() End Sub ''' <summary> ''' Instantiates a new instance of AdDataAdapter. ''' </summary> ''' <param name="selectCommand"></param> ''' <remarks></remarks> Public Sub New(ByVal selectCommand As AdCommand) ThrowErrorOnInvalidStatementType(MyBase.SelectCommand, StatementTypes.Select) MyBase.SelectCommand = selectCommand End Sub ''' <summary> ''' Instantiates a new instance of AdDataAdapter. ''' </summary> ''' <param name="selectCommandText"></param> ''' <param name="selectConnectionString"></param> ''' <remarks></remarks> Public Sub New(ByVal selectCommandText As String, ByVal selectConnectionString As String) CommandTextParser.ValidateStatement(selectCommandText, StatementTypes.Select) Dim selectCommand As New AdCommand(selectCommandText, selectConnectionString) MyBase.SelectCommand = selectCommand End Sub ''' <summary> ''' Instantiates a new instance of AdDataAdapter. ''' </summary> ''' <param name="selectCommandText"></param> ''' <param name="selectConnection"></param> ''' <remarks></remarks> Public Sub New(ByVal selectCommandText As String, ByVal selectConnection As AdConnection) CommandTextParser.ValidateStatement(selectCommandText, StatementTypes.Select) Dim selectCommand As New AdCommand(selectCommandText, selectConnection) MyBase.SelectCommand = selectCommand End Sub #End Region #Region "Property Section" ''' <summary> ''' Gets or sets a Transact-SQL statement used to delete objects in Active Directory. ''' </summary> ''' <remarks></remarks> Public Overloads Property DeleteCommand As AdCommand Get ThrowErrorOnInvalidStatementType(MyBase.DeleteCommand, StatementTypes.Delete) Return DirectCast(MyBase.DeleteCommand, AdCommand) End Get Set(ByVal value As AdCommand) ThrowErrorOnInvalidStatementType(value, StatementTypes.Delete) MyBase.DeleteCommand = value End Set End Property ''' <summary> ''' Gets or sets a Transact-SQL statement used to insert objects into Active Directory. ''' </summary> ''' <remarks></remarks> Public Overloads Property InsertCommand As AdCommand Get ThrowErrorOnInvalidStatementType(MyBase.InsertCommand, StatementTypes.Insert) Return DirectCast(MyBase.InsertCommand, AdCommand) End Get Set(ByVal value As AdCommand) ThrowErrorOnInvalidStatementType(value, StatementTypes.Insert) MyBase.InsertCommand = value End Set End Property ''' <summary> ''' Gets or sets a Transact-SQL statement used to select objects in Active Directory. ''' </summary> ''' <remarks></remarks> Public Overloads Property SelectCommand As AdCommand Get ThrowErrorOnInvalidStatementType(MyBase.SelectCommand, StatementTypes.Select) Return DirectCast(MyBase.SelectCommand, AdCommand) End Get Set(ByVal value As AdCommand) ThrowErrorOnInvalidStatementType(value, StatementTypes.Select) MyBase.SelectCommand = value End Set End Property ''' <summary> ''' Gets or sets a Transact-SQL statement used to update objects in Active Directory. ''' </summary> ''' <remarks></remarks> Public Overloads Property UpdateCommand As AdCommand Get ThrowErrorOnInvalidStatementType(MyBase.UpdateCommand, StatementTypes.Update) Return DirectCast(MyBase.UpdateCommand, AdCommand) End Get Set(ByVal value As AdCommand) ThrowErrorOnInvalidStatementType(value, StatementTypes.Update) MyBase.UpdateCommand = value End Set End Property #End Region #Region "Behavior Section" ''' <summary> ''' Initializes a new instance of the RowUpdatedEventArgs class. ''' </summary> ''' <param name="dataRow"> ''' The DataRow used to update the data source. ''' </param> ''' <param name="command"> ''' The IDbCommand executed during the Update. ''' </param> ''' <param name="statementType"> ''' Whether the command is an UPDATE, INSERT, DELETE, or SELECT statement. ''' </param> ''' <param name="tableMapping"> ''' A DataTableMapping object. ''' </param> ''' <returns></returns> ''' <remarks></remarks> Protected Overrides Function CreateRowUpdatedEvent(ByVal dataRow As System.Data.DataRow, ByVal command As System.Data.IDbCommand, ByVal statementType As System.Data.StatementType, ByVal tableMapping As System.Data.Common.DataTableMapping) As System.Data.Common.RowUpdatedEventArgs Return New AdRowUpdatedEventArgs(dataRow, command, statementType, tableMapping) End Function ''' <summary> ''' Initializes a new instance of the RowUpdatingEventArgs class. ''' </summary> ''' <param name="dataRow"> ''' The DataRow used to update the data source. ''' </param> ''' <param name="command"> ''' The IDbCommand executed during the Update. ''' </param> ''' <param name="statementType"> ''' Whether the command is an UPDATE, INSERT, DELETE, or SELECT statement. ''' </param> ''' <param name="tableMapping"> ''' A DataTableMapping object. ''' </param> ''' <returns></returns> ''' <remarks></remarks> Protected Overrides Function CreateRowUpdatingEvent(ByVal dataRow As System.Data.DataRow, ByVal command As System.Data.IDbCommand, ByVal statementType As System.Data.StatementType, ByVal tableMapping As System.Data.Common.DataTableMapping) As System.Data.Common.RowUpdatingEventArgs Return New AdRowUpdatingEventArgs(dataRow, command, statementType, tableMapping) End Function ''' <summary> ''' Raises the RowUpdated event of a .NET Framework data provider. ''' </summary> ''' <param name="value"> ''' A RowUpdatedEventArgs that contains the event data. ''' </param> ''' <remarks></remarks> Protected Overrides Sub OnRowUpdated(ByVal value As System.Data.Common.RowUpdatedEventArgs) Dim handler As AdRowUpdatedEventHandler = CType(Events(EventRowUpdated), AdRowUpdatedEventHandler) If Not handler Is Nothing And value.GetType() Is Type.GetType("AdRowUpdatedEventArgs") Then handler(Me, CType(value, AdRowUpdatedEventArgs)) End If End Sub ''' <summary> ''' Raises the RowUpdating event of a .NET Framework data provider. ''' </summary> ''' <param name="value"> ''' An RowUpdatingEventArgs that contains the event data. ''' </param> ''' <remarks></remarks> Protected Overrides Sub OnRowUpdating(ByVal value As System.Data.Common.RowUpdatingEventArgs) Dim handler As AdRowUpdatingEventHandler = CType(Events(EventRowUpdating), AdRowUpdatingEventHandler) If Not handler Is Nothing And value.GetType() Is Type.GetType("AdRowUpdatingEventHandler") Then handler(Me, CType(value, AdRowUpdatingEventArgs)) End If End Sub ''' <summary> ''' Throws an error when the command is not of the specified statement type. ''' </summary> ''' <param name="command"> ''' The AdCommand to validate. ''' </param> ''' <param name="statementType"> ''' The type of statement expected. ''' </param> ''' <remarks></remarks> Protected Sub ThrowErrorOnInvalidStatementType(ByVal command As System.Data.Common.DbCommand, ByVal statementType As StatementTypes) If command IsNot Nothing AndAlso Not String.IsNullOrWhiteSpace(command.CommandText) Then CommandTextParser.ValidateStatement(command.CommandText, statementType) End If End Sub #End Region End Class End Namespace
Leave a Reply