Multiple database adapters and auditing

Posts   
 
    
hwing
User
Posts: 2
Joined: 06-Feb-2008
# Posted on: 06-Feb-2008 20:22:51   

LLBLGenPro v2.5 Final

I am using a DatabaseAuditor from the Auditing example provided. My AuditInfo table is on a separate database from the tables I want to audit and I have created a LLBLGenPro project for it using the Adapter method. This project contains the AuditInfo entity.

I have another database containing User + related tables. These are the tables I want to audit. I have created a LLBLGenPro project for this set of tables too using Adapter method. This project contains the UserEntity.

I am using the dependency injection mechanism to inject the DatabaseAuditor so that the UserEntity will use it as the "AuditorToUse"

When the following code, runs I get an error because I am using the DataAccessAdapter of the UserEntity. During the SaveEntity(user), it tries to also save the AuditInfo entity, but it will fail because the AuditInfo entity needs a different adapter.



//make some changes to the UserEntity user

using (DataAccessAdapter adapter = new DataAccessAdapter())
{
   adapter.StartTransaction(System.Data.IsolationLevel.RepeatableRead, updateLock);
   adapter.SaveEntity(user);  
   adapter.SaveEntity(log);
   adapter.Commit();
}

The motivation for doing this is that I want to put all Auditing related code as one separate dll, so that I can easily audit other databases.

Any suggestion on how I can save the AuditInfo entity, without having to put my audit tables in the same database? Thanks.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 07-Feb-2008 03:43:22   

Hi hwing,

I didn't worked like that before (using a different database for auditing). However I think you could achieve that in the way the SimpleTextFileAuditor do (please see the additional project in the same example):

  1. The AudiInfo wont participate in UserEntity (or other) entity's transaction.
public override bool RequiresTransactionForAuditEntities(SingleStatementQueryAction actionToStart)
{
     return false;
}
  1. At your Auditing project, reference the AuditingGeneric and AuditingDBSpecific dlls (or projects) that you generated for you Auditing DB.

  2. Persist your AuditInfo objects at TransactionCommitted method of your Auditor

public override void TransactionCommitted()
{

     using (Auditing.DBSpecific.DataAccessAdapter adapter = new Auditing.DBSpecific.DataAccessAdapter())
     {
          adapter.SaveEntityCollection(_auditInfos);
      }
}

The thing is: ¿What happens if step 3 fails?

hwing wrote:

The motivation for doing this is that I want to put all Auditing related code as one separate dll, so that I can easily audit other databases.

That's ok. But remember that at your Auditor you have to indicate what kind of entities the Auditor will audit (UserEntity, BlaBlaEntity, IEntity2) and the Auditor must to know what fields to audit, etc. So seems hard (IMHO) to do a super-general Auditor, or maybe I don't understand you 100% simple_smile

David Elizondo | LLBLGen Support Team