0

I'm playing around with SharpArchitecture and I'm trying to create a simple AuditInterceptor for NHibernate in the Infrastructure layer.

Now, my problem is to make the current request details accessible from within the interceptor. This includes the current user and the date/time at which the server received the active request.

Should I be using some kind of UnitOfWork pattern? If so, what is a good way of making the UnitOfWork object available to the interceptor code?

1 Answers1

1

As far as I have found out the best place to get the current user is:

System.Threading.Thread.CurrentPrincipal.Identity.Name

I had a need to also get the Date/Time of the current request and decided it was best to create a custom IPrincipal which had a CurrentDateTime property and then I set the custom principal either in Global.asax OnAuthenticated or in my MVC ActionFilter.

Mark Perry
  • 1,705
  • 10
  • 12
  • Thanks! What I've decided to do is pass an "IAuditInfoProvider" object to the interceptor constructor when configuring NHibernate on application startup. Not sure yet how I'll implement it, but at least it will allow me to change it at a later date without affecting the interceptor code. I'm thinking of creating a custom Context object that implements this (and other) interfaces, which is modified in Begin/EndRequest and injected into objects with whatever IoC container I'm using. Still not sure on the details and I'm completely new to MVC and SharpArchitecture, so this is just an idea. –  Nov 01 '11 at 10:17
  • When I tackled the same problem I couldn't see how I could get away from use of a Static property/class. You are also going to have to deal with threading issues as well. – Mark Perry Nov 01 '11 at 11:26