Similar questions have been asked before but not quite the same (unless I missed it)
I want to pass IUserInfo class instance through my Service, Domain , Domain Events, Domain Event Handlers...
Whats is the best way to do it.
Should I
Inject it using IoC by registering it against instance of Httpcontext.Current.session["CurrentUser"];
Add the data to Current Thread.
Any other way
I am stuck at Domain Event Handlers where I want to use the data for auditing as well as sending emails.
I want to be able to use the CurrentUser information from almost anywhere in my application.
With threading as threads are pooled I am skeptical if the reuse of threads will reset the data. If not please shopw me how to use threading to pass IUser instance.
Regards,
Mar
My approach might not be ideal, but I find it working quite well. Thing what I did - I decided not to
use dependency injectionpass current user everywhere directly because that was getting too cumbersome and switched to static context. Problem with contexts - they are a bit difficult to manage.This one is defined in my domain:
Note that delegate is static - for whole app only one at a time. And I'm not 100% sure about it's life cycle, possible memory leaks or whatnot.
Client application is responsible to initialize context. My web application does that on every request:
Using mvcextensions library to stream-line bootstrapping tasks. You can just subscribe for according events in global.asax for that.
In client side (web app), I implement application service named IUserSession:
There is some more lame code necessary in order to use forms auth with roles w/o membership provider and role provider. But that's not the point of this question.
At domain level - I'm explicitly describing permissions that users might have like this one:
Cool thing is - those permissions can be made more sophisticated. E.g.:
Permissions can be checked vica versa too - User has these fellas:
Here's my aggregate root base class:
And here's how I check permissions:
I hope that helps...
I've done this kind of thing before using IoC. The benefit of this is that it's very testable -- you can stub out your user info for testing -- and reasonably readable and easy to follow.