Anyways what I want is to set up and use the auditing functionality provided by your cool DAL generator.
I have read the articles on Generated code – ‘Setting up and using Auditing’ and the article relating to Dependency Injection (our preferred method for setting the entity’s auditor).
The composition of my vs2008 solution is
2 auto generated llblgenpro projects providing an adapter dal in vb.net
1 application project with UI and business logic together.
It’s a windows application written in vb.net.
What I’ve done is
• Added the sample GeneralAuditor class below to the application project – commenting out one or two of the methods not relevant for me.
• Added the line <add key="autoDependencyInjectionDiscovery" value="true"/> to the appsettings section in the config file in the application project.
• Created an AuditInfo table and generated the AuditInfoEntity using LLBLGenPro
I then used an adapter to do an insert of an entity (see code below) and the insert is working fine – see the AddTheTransaction function below (I commented out the transaction thinking it might be an issue here but no). What I thought would happen is that the override method for AuditInsertOfNewEntity in an instance of GeneralAuditor would be called each time I did an insert using oadapter.SaveEntity(oCDTran, True). I am obviously missing something. Any help you might be able to provide would be greatly appreciated?
Sample Code
Imports SD.LLBLGen.Pro.ORMSupportClasses
Imports FAS.EntityClasses
Imports System.Collections.Generic
' VB.NET
' <summary>Example Auditor class which is usable on all entities in a project.</summary>
<DependencyInjectionInfo(GetType(IEntity2), "AuditorToUse"), Serializable()> _
Public Class GeneralAuditor
Inherits AuditorBase
Private Enum AuditType
DeleteOfEntity = 1
DirectDeleteOfEntities = 2
DirectUpdateOfEntities = 3
DereferenceOfRelatedEntity = 4
ReferenceOfRelatedEntity = 5
EntityFieldSet = 6
InsertOfNewEntity = 7
UpdateOfExistingEntity = 8
End Enum
Private _auditInfoEntities As List(Of AuditInfoEntity)
''' <summary>CTor </summary>
Public Sub New()
_auditInfoEntities = New List(Of AuditInfoEntity)()
End Sub
''' <summary>Audits the successful delete of an entity from the database</summary>
''' <param name="entity">The entity which was deleted.</param>
''' <remarks>As the entity passed in was deleted succesfully, reading values from the
''' passed in entity is only possible in this routine. After this call, the
''' state of the entity will be reset to Deleted again and reading the fields
''' will result in an exception. It's also recommended not to reference
''' the passed in entity in any audit entity you might want to persist as the entity
''' doesn't exist anymore in the database.</remarks>
Public Overrides Sub AuditDeleteOfEntity(ByVal entity As IEntityCore)
Dim auditInfo As New AuditInfoEntity()
auditInfo.AffectedEntityName = entity.LLBLGenProEntityName
auditInfo.ActionDateTime = DateTime.Now
auditInfo.ActionType = CInt(AuditType.DeleteOfEntity)
_auditInfoEntities.Add(auditInfo)
End Sub
''' <summary>Audits the successful dereference of related entity from the entity passed in.</summary>
''' <param name="entity">The entity of which the related entity was dereferenced from.</param>
''' <param name="relatedEntity">The related entity which was dereferenced from entity</param>
''' <param name="mappedFieldName">Name of the mapped field onto the relation from entity to related
''' entity for which the related entity was dereferenced.</param>
'Public Overrides Sub AuditDereferenceOfRelatedEntity(ByVal entity As IEntityCore, ByVal relatedEntity As IEntityCore, _
' ByVal mappedFieldName As String)
' Dim auditInfo As New AuditInfoEntity()
' auditInfo.AffectedEntityName = entity.LLBLGenProEntityName
' auditInfo.ActionDateTime = DateTime.Now
' auditInfo.ActionType = CInt(AuditType.DereferenceOfRelatedEntity)
' auditInfo.ActionData = String.Format("RelatedEntityName: {0}. MappedFieldName: {1}", _
' relatedEntity.LLBLGenProEntityName, mappedFieldName)
' _auditInfoEntities.Add(auditInfo)
'End Sub
''' <summary>Audits the successful insert of a new entity into the database.</summary>
''' <param name="entity">The entity saved successfully into the database.</param>
Public Overrides Sub AuditInsertOfNewEntity(ByVal entity As IEntityCore)
Dim auditInfo As New AuditInfoEntity()
auditInfo.AffectedEntityName = entity.LLBLGenProEntityName
auditInfo.ActionDateTime = DateTime.Now
auditInfo.ActionType = CInt(AuditType.InsertOfNewEntity)
_auditInfoEntities.Add(auditInfo)
End Sub
''' <summary>
''' Audits the successful reference of related entity from the entity passed in.
''' </summary>
''' <param name="entity">The entity of which the related entity was dereferenced from.</param>
''' <param name="relatedEntity">The related entity which was dereferenced from entity</param>
''' <param name="mappedFieldName">Name of the mapped field onto the relation from entity to related
''' entity for which the related entity was referenced.</param>
'Public Overrides Sub AuditReferenceOfRelatedEntity(entity As IEntityCore, relatedEntity As IEntityCore,
' mappedFieldName As String)
' Dim auditInfo As New AuditInfoEntity()
' auditInfo.AffectedEntityName = entity.LLBLGenProEntityName
' auditInfo.ActionDateTime = DateTime.Now
' auditInfo.ActionType = CInt(AuditType.ReferenceOfRelatedEntity)
' auditInfo.ActionData = String.Format("RelatedEntityName: {0}. MappedFieldName: {1}", _
' relatedEntity.LLBLGenProEntityName, mappedFieldName)
' _auditInfoEntities.Add(auditInfo)
'End Sub
''' <summary>
''' Audits the successful update of an existing entity in the database
''' </summary>
''' <param name="entity">The entity updated successfully in the database.</param>
Public Overrides Sub AuditUpdateOfExistingEntity(ByVal entity As IEntityCore)
Dim auditInfo As New AuditInfoEntity()
auditInfo.AffectedEntityName = entity.LLBLGenProEntityName
auditInfo.ActionDateTime = DateTime.Now
auditInfo.ActionType = CInt(AuditType.UpdateOfExistingEntity)
_auditInfoEntities.Add(auditInfo)
End Sub
''' <summary>
''' Gets the audit entities to save. Audit entities contain the audit information stored
''' inside this auditor.
''' </summary>
''' <returns>The list of audit entities to save, or null if there are no audit entities to save</returns>
''' <remarks>Do not remove the audit entities and audit information from this auditor when this method is
''' called, as the transaction in which the save takes place can fail and retried which will result in
''' another call to this method</remarks>
Public Overrides Function GetAuditEntitiesToSave() As IList
Return _auditInfoEntities
End Function
''' <summary>
''' The transaction with which the audit entities requested from GetAuditEntitiesToSave were saved.
''' Use this method to clear any audit data in this auditor as all audit information is persisted
''' successfully.
''' </summary>
Public Overrides Sub TransactionCommitted()
_auditInfoEntities.Clear()
End Sub
End Class
Function AddTheTransaction() As Boolean
Dim oCDTran As New FAS.EntityClasses.CallDepositTransactionsEntity
Dim oCDFundTran As New FAS.EntityClasses.CallDepositFundTransactionsEntity
Dim ocolCDFundTran As New FAS.HelperClasses.EntityCollection(Of FAS.EntityClasses.CallDepositFundTransactionsEntity)
AddTheTransaction = False
With oCDTran
.BankId = CInt(Me.LookUpEditBankAccounts.EditValue)
.CdaccountId = CInt(Me.LookUpEditBankAccounts.EditValue)
.TradeDate = CType(dtTransactionDate.Text, Date)
.TransTypeId = CType(Me.LookUpEditTransactionType.EditValue, Integer)
.CurrencyId = CType(Me.LookUpEditCurrency.EditValue, Integer)
.Narrative = Me.LookUpEditNarrative.Text
.Description = Me.txtDescription.Text & " - Manual CD"
.ProcessedDate = Now
.SourceId = enumPostingAreaID.ePostAreaCallDeposit
.CurrentRec = -1
End With
For Each drowview As System.Data.DataRowView In _mbsCDJournals
With oCDFundTran
.IfundNo = CType(drowview("FundID"), Integer)
.Amount = CType(drowview("Amount"), Decimal)
End With
ocolCDFundTran.Add(oCDFundTran)
Next
Dim oadapter As New DataAccessAdapter(FASDatabase.ClientDBConnectionString)
Using oadapter
'oadapter.StartTransaction(IsolationLevel.Serializable, "Add CD Transaction")
oadapter.SaveEntity(oCDTran, True)
'Dim GenAudit As New GeneralAuditor
'oCDTran.AuditorToUse = GenAudit
'oCDFundTran.AuditorToUse.AuditInsertOfNewEntity(oCDTran)
For Each oCDFundTran In ocolCDFundTran
oCDFundTran.TransId = oCDTran.RecordId
Next
Dim nNoSaved As Integer = oadapter.SaveEntityCollection(ocolCDFundTran)
If _mbsCDJournals.Count <> nNoSaved Then
MsgBox("There was a problem in saving the funds and we are rolling back.", MsgBoxStyle.Critical)
'oadapter.Rollback()
Else
'oadapter.Commit()
AddTheTransaction = True
End If
End Using
End Function