DeleteEntitiesDirectly and Auditor

Posts   
 
    
LukeO
User
Posts: 58
Joined: 23-Jul-2007
# Posted on: 24-Jan-2008 23:22:39   

I'm working on auditing a call to the DeleteEntitiesDirectory method. I've been assigning my auditor directly to the entity like this

entityToTest.AuditorToUse = myAuditor;

Which has worked great for single entity updates or deletes.

Now for DeleteEntitiesDirectly how do I specify the auditor to use?

I was thinking the solution might involve UnitOfWork2 like this:

UnitOfWork2 uow = new UnitOfWork2(); uow.AddDeleteEntitiesDirectlyCall(typeof(CustodianEntity),filter); uow.Commit(_adapter,true);

But.... I can't seem to figure out where to specify an auditor.

Thanks, -Luke

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-Jan-2008 00:51:52   

Hi Luke,

Internally, LLBLGenPro creates a dummy entity with the type you specified, so you can assign the AuditorToUse property at an overload of the OnInitialized() method (at a partial class or CUSTOM_CODE_REGIONS):

protected override void OnInitialized()
{
    this.AuthorizerToUse = new yourAuditor();

    base.OnInitialized();
}

You also can opt to use DependencyInjection so you don't have to worry about the assignment of the property. (See LLBLGenPro Help - Generated code - Setting up and using Dependency Injection).

David Elizondo | LLBLGen Support Team
LukeO
User
Posts: 58
Joined: 23-Jul-2007
# Posted on: 25-Jan-2008 01:07:26   

Thanks. I will use Dependency Injection for real code but when I execute this with MBUNIT I need to specify something outide the .config files.

Are you saying that if I delete 100 or so entities a new entity instance is created? And for each of those entities the entity auditor is accessed? I know what you're talking about will work for single entities but I'm specifically talking about DeleteEntitiesDirectly which takes just the type and a filter.

-Luke

daelmo wrote:

Hi Luke,

Internally, LLBLGenPro creates a dummy entity with the type you specified, so you can assign the AuditorToUse property at an overload of the OnInitialized() method (at a partial class or CUSTOM_CODE_REGIONS):

protected override void OnInitialized()
{
    this.AuthorizerToUse = new yourAuditor();

    base.OnInitialized();
}

You also can opt to use DependencyInjection so you don't have to worry about the assignment of the property. (See LLBLGenPro Help - Generated code - Setting up and using Dependency Injection).

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-Jan-2008 01:32:11   

No. Just one dummy entity for the whole DeleteEntitiesDirectly call. Just intended to make a call to Auditors and other things. The Auditor is only accessed once for the OnAuditOfDeleteEntitiesDirectly. wink

The above code should works OK for testing. Also I have tested DependencyInjection on NUnit and VSNet-UnitTest and works OK. I didn't try with MBUNIT. simple_smile

David Elizondo | LLBLGen Support Team
LukeO
User
Posts: 58
Joined: 23-Jul-2007
# Posted on: 25-Jan-2008 01:45:48   

Thanks...

Ok one more question.

If I want to assign the auditor in code then do I have to use the overload?

-Luke

daelmo wrote:

No. Just one dummy entity for the whole DeleteEntitiesDirectly call. Just intended to make a call to Auditors and other things. The Auditor is only accessed once for the OnAuditOfDeleteEntitiesDirectly. wink

The above code should works OK for testing. Also I have tested DependencyInjection on NUnit and VSNet-UnitTest and works OK. I didn't try with MBUNIT. simple_smile

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-Jan-2008 01:59:48   

Ok Luke,

As far as I know there're only two overloads for DeleteEntitiesDirectly:

A._DeleteEntitiesDirectly(Type typeOfEntity, IRelationPredicateBucket filterBucket)_

B._DeleteEntitiesDirectly(string entityName, IRelationPredicateBucket filterBucket) _

However **B **doesn't support Authorization or Auditing, so you have to use A if you want Auditing. Now, to set the Auditor I see to options for you:

 I. Setting the auditor when the Entity class is initiated (see above posts).
 II. Using Dependency Injection. (see docs).

Hope helpful wink

David Elizondo | LLBLGen Support Team
LukeO
User
Posts: 58
Joined: 23-Jul-2007
# Posted on: 25-Jan-2008 08:37:40   

You're right. I change a few things around and was able to both configure Dependency Injection via App.config and call it from MBUnit. However...

I use TypeMock to test the calls to various methods. For example take the following.

[Test] public void TestAuditDirectDeleteOfEntities() { // Intercept calls to the RAMAuditor object Mock ramAuditorMock = MockManager.Mock(typeof(RAMAuditor), Constructor.NotMocked);

 // This auditor will now be mocked
 RAMAuditor ramAuditor = new RAMAuditor();

 // Set up the predicate filter
 IRelationPredicateBucket filter = new RelationPredicateBucket((CustodianFields.Name == "Test"));

 // When we directly delete the entities then AddAuditEntryToList should be called with these parameters
 ramAuditorMock.ExpectCall("AddAuditEntryToList").Args("CustodianEntity", RAMAuditor.AuditType.DirectDeleteOfEntities, Check.IsAny());

 _adapter.DeleteEntitiesDirectly(typeof(CustodianEntity), filter);


 MockManager.Verify();

}

This had worked perfectly with single entity calls. But now if I use dependency injection I cannot mock the auditor class since the auditor now gets set in the constructor of the entity. Is there no way to set the auditor in code that would be used for multi-entity calls like DeleteEntitiesDirectly?

Thanks, -Luke

daelmo wrote:

Ok Luke,

As far as I know there're only two overloads for DeleteEntitiesDirectly:

A._DeleteEntitiesDirectly(Type typeOfEntity, IRelationPredicateBucket filterBucket)_

B._DeleteEntitiesDirectly(string entityName, IRelationPredicateBucket filterBucket) _

However **B **doesn't support Authorization or Auditing, so you have to use A if you want Auditing. Now, to set the Auditor I see to options for you:

 I. Setting the auditor when the Entity class is initiated (see above posts).
 II. Using Dependency Injection. (see docs).

Hope helpful wink

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 25-Jan-2008 16:23:00   

Either use the DI or set it from within the entity class.

The following is copied from the manual (I've omitted what doesn't suit your requirements):

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.