I'm using the Adapter scenario, and I want to complete some logging code I'm adding to my project, as well as add some security features. It seems like for a basic security implementation one can add security checking to an extended DataAccessAdapter and implement some checking to say (does this user in this role have the ability to save/fetch/delete entities of this type).
One problem I have is in the logging code. I want to log when various events happen such as a save, or delete. I also want to log in the entity itself when it was created it and who created it, as well as when it was last modified and who modified it. Since the notion of security principals in my applicaiton will not exist in the DB, I should implement this logic in my BL.
So here's what I need to do if I want to see if we're saving a new entity (this is simplified, since I don't want to perform these actions on the EventEntity that I use to actually save my log events).
public override void OnSaveEntity(IActionQuery saveQuery, IEntity2 entityToSave)
{
if (entityToSave.IsNew)
{
// Set the CreateDate and CreatedByUserId on this entity.
}
// Set the LastModifiedDate and LastModifiedByUserId on this entity.
base.OnSaveEntity(saveQuery, entityToSave);
}
The problem is that in order to set properties on the entityToSave I need to cast it as the proper type. The way that I'm doing this currently is repetitive:
switch ((EntityType)entityToSave.LLBLGenProEntityTypeValue)
{
case EntityType.DocumentEntity:
logevent.EntityId = ((DocumentEntity)entityToSave).Id;
logevent.EntityGuid = ((DocumentEntity)entityToSave).Guid;
break;
[and so on]
Is there any way to fetch the real type of the entity that's being passed around here, and then cast IEntity2 to that type, or otherwise access and set its properties? I can do this with big fat case statements, but that leads to a lot of repetitive code, and as I add/change entities in my project it gets unwieldy.