Auditing dependency injection using LLBLGenPro O/R engine

Posts   
 
    
greenstone
User
Posts: 132
Joined: 20-Jun-2007
# Posted on: 24-Aug-2011 17:21:15   

For my ASP.NET application, I need to get the userid/username, domainid/domainname(id of a company a user is in--a company shares common data they enter) and include each of these two as columns in an audit trail database table record.

I've found a post of how to access the ASP.NET session value (that contains the userid) on the llblgen forums, and that works well.

From a user id, (using llblgen entity fetching code), I can fetch the user name, domain id, domain name.

If I use the dependency-injection, I can put to put the code for auditing into a separate .dll from the llblgen "DatabaseGeneric" code (no compile time circular reference of the auditing code calling the DatabaseGeneric code...because injecting run-time).

However, if I don't use dependency injection, it appears that I need to put the auditing code into the DatabaseGeneric dll. Does this sound accurate?

A second related question: What I'm trying to accomplish is allowing users to select which entities to audit (based on a run-time UI for each user). So, I'm trying to figure out if I can use the dependency-injection way...as each user of the ASP.NET application might be auditing a different set (of the maximum set I allow) of entities. Would you suggest I create auditing for the "maximum" set, then in each auditor, to check if the user has requested the auditing of that entity?

Thanks

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 24-Aug-2011 18:38:51   

if I don't use dependency injection, it appears that I need to put the auditing code into the DatabaseGeneric dll. Does this sound accurate?

Nope, please check the following section copied from the docs:

Setting an entity's Auditor If you've decided to use Auditor classes to place your auditing logic in, you'll be confronted with the question: how to set the right Auditor in every entity object? You've three options:

1- Setting the AuditorToUse property of an entity object manually. This is straight forward, but error prone: if you forget to set an auditor, auditing isn't performed. Also, entities fetched in bulk into a collection are created using the factories so you have to alter these as well. You could opt for overriding OnInitializing in an entity to add the creation of the Auditor class.

2- By overriding the Entity method CreateAuditor. This is a protected virtual (Protected Overridable) method which by default returns null / Nothing. You can override this method in a partial class or user code region of the Entity class to create the Auditor to use for the entity. The LLBLGen Pro runtime framework will take care of calling the method. One way to create an override for most entities is by using a template. Please see the LLBLGen Pro SDK documentation for details about how to write templates to generate additional code into entities and in separate files. Also please see Adding Adding your own code to the generated classes for details.

3- By using Dependency Injection. Using the Dependency Injection mechanism build into LLBLGen Pro, the defined Auditors are injected into the entity objects for you. This option is the least amount of work.

Would you suggest I create auditing for the "maximum" set, then in each auditor, to check if the user has requested the auditing of that entity?

That's fine. Also you can have this logic inside the CreateAuditor override if you use option 2 from the above. There you should check for the condition and either return null or a new instance of the auditor.