Accessing current request context in NHibernate interceptor

305 Views Asked by At

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

There are 1 best solutions below

2
On

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.