Dependency Injection: How do I access the "host" object?

Posts   
 
    
LukeO
User
Posts: 58
Joined: 23-Jul-2007
# Posted on: 18-Jan-2008 02:05:56   

I'm using LLBLGEN 2.5 Final

I'm using code like this:

RIA ria = new RIA(); RIAAuditor riaAuditor = new RIAAuditor(_adapter); ria.AuditorToUse = riaAuditor;

No problems. I even got the basic auditor to work.

What I'm trying to do is concerns sub-types like the Order example from your website.

Is there a way to access the host object from the auditor?

I have something like this: protected internal override void AddAuditEntryToList(string affectedEntityName, AuditType actionType, string actionData) { AuditInfoRIAEntity auditInfo = new AuditInfoRIAEntity(); auditInfo.AffectedEntityName = affectedEntityName; auditInfo.ActionDateTime = DateTime.Now; auditInfo.AuditActionTypeId = (int)actionType; auditInfo.UserId = GetCurrentUserID(); auditInfo.RIAId = 1; auditInfo.ActionData = actionData;

        base.AddAuditEntryToList(auditInfo);
    }

As you can see the RIAId is hard coded for testing. This works. I would obviously like to get the applicable ID.

Thanks, -Luke

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-Jan-2008 09:03:22   

If I understand you well, implementing the IAuditor -> most of the methods that you should implement receives the host entity as a parameter as follows:

public override void AuditUpdateOfExistingEntity(IEntityCore entity)
    {
        AuditInfoEntity auditInfo = new AuditInfoEntity();
        auditInfo.AffectedEntityName = entity.LLBLGenProEntityName;
        auditInfo.ActionDateTime = DateTime.Now;
        auditInfo.ActionType = (int)AuditType.UpdateOfExistingEntity;
        _auditInfoEntities.Add(auditInfo);
    }

From where did you get the AddAuditEntryToList() method?

LukeO
User
Posts: 58
Joined: 23-Jul-2007
# Posted on: 18-Jan-2008 11:15:02   

I combined the two examples from your website. Basically I'm doing this:

public abstract class RAMAuditorBase : AuditorBase { private List<EntityBase2> _auditInfoEntities; private DataAccessAdapter _dataAdapter;

... code from example on website changed slightly to fit. The method you asked about is duplicated here. It pretty much follows the example.

   protected virtual internal void AddAuditEntryToList(string affectedEntityName, AuditType actionType, string actionData)
    {
        AuditInfoEntity auditInfo = new AuditInfoEntity();
        auditInfo.AffectedEntityName = affectedEntityName;
        auditInfo.ActionDateTime = DateTime.Now;
        auditInfo.AuditActionTypeId = (int)actionType;
        auditInfo.UserId = GetCurrentUserID();
        auditInfo.ActionData = actionData;

        _auditInfoEntities.Add(auditInfo);
    }

...

}

The above pretty much covers anything I want saved to just to the AuditInfo table.

In order to do anything I little more custom I did this:

public class RIAAuditor : RAMAuditorBase { public RIAAuditor(DataAccessAdapter dataAdapter):base(dataAdapter) { }

    protected internal override void AddAuditEntryToList(string affectedEntityName, AuditType actionType, string actionData)
    {
        AuditInfoRIAEntity auditInfo = new AuditInfoRIAEntity();
        auditInfo.AffectedEntityName = affectedEntityName;
        auditInfo.ActionDateTime = DateTime.Now;
        auditInfo.AuditActionTypeId = (int)actionType;
        auditInfo.UserId = GetCurrentUserID();
        auditInfo.RIAId = 1;
        auditInfo.ActionData = actionData;

        base.AddAuditEntryToList(auditInfo);
    }
}

So now I'm thinking I should just pass in the ENTITY into this method and not "string affectedEntityName".

-Luke

Walaa wrote:

If I understand you well, implementing the IAuditor -> most of the methods that you should implement receives the host entity as a parameter as follows:

public override void AuditUpdateOfExistingEntity(IEntityCore entity)
    {
        AuditInfoEntity auditInfo = new AuditInfoEntity();
        auditInfo.AffectedEntityName = entity.LLBLGenProEntityName;
        auditInfo.ActionDateTime = DateTime.Now;
        auditInfo.ActionType = (int)AuditType.UpdateOfExistingEntity;
        _auditInfoEntities.Add(auditInfo);
    }

From where did you get the AddAuditEntryToList() method?