r/dotnet•
SequesterMe

Looking for a better way of doing this auditing thing

So, we've got this BusinessRules class that has hundreds of methods like this:

public Module GetModuleById(int pModuleId, LegacyApplicationUser pApplicationUser)
{
  SaveAdminAudit(pApplicationUser, AdminAuditCode.Module_GetModuleById, AdminAuditLevel.RetrievedData, false,
        string.Format("Get Module by Module Id {0}.", pModuleId.ToString()), typeof(string));
  return MyDataManager.ModuleManager.GetModuleById(pModuleId);
}

It ends up being 6000+ lines of repetitious code.

It's offensive to me to have a wrapper put around each and every method just to track who made the call. I admit that many of these methods actually have business rules in them like checking if it's okay for this user to modify/delete an object or if the object is in a state where it shouldn't be modified or deleted but these audit code lines are repeated throughout this class like a valley girl says "like".

Does anyone have suggestions on how to get away from this?

What I'm thinking is that we could inherit from a class that would use reflection to get the methods name and the user information as it is called and log that for us.

Also, I think that what is a parameter in all these method calls, "LegacyApplicationUser pApplicationUser" should instead be part of the BusinessRules constructor. Yes, it's on every method I checked.

Note that this is used as a web service. There's a web service .asmx class that calls the BusinessRules class that calls the ModuleManager class.

This code is in an older SOAP .asmx web service application that we're working on updating to WCF (I don't know why we're not using WebAPI) There is dependency injection in the application but it's not used correctly at all.

** In this case "Module" is just an object like "Customer" or "Book" would be. There is nothing special about it.

Much thanks for the input.

Posted Dec 3, 2015, 12:11 AM UTC

13
Loading comments…