Resolving external instances via Constructor Injection

Feb 10, 2009 at 8:25 PM
Edited Feb 10, 2009 at 8:26 PM

I have a business object called "CustomerService(ICustomerRepository repository, IPermissionPrincipal principal) : ICustomerService"

IPermissionPrincipal is an extended IPrincipal, which is created in AuthorizationModule (IHttpModule) by setting the Thread's current principal (Thread.CurrentPrincipal = value), or it can also be created somwehere else...

So, how do I (what is the best way to) inject IPermissionPrincipal into the CustomerService business object?..

Thank you!
Feb 11, 2009 at 7:12 PM
Is it always available through Thread.CurrentPricipal? If so, probably the easiest thing to do is to use a custom LifetimeManager class. In the Get method, pull the value out of Thread.CurrentPrincipal, in the Set method, stuff it in.

Take a look at my article on writing lifetime managers for more details (unfortunately, the pictures seem to have gotten lost in a blog engine upgrade).

Feb 18, 2009 at 4:06 PM
Thanks tavares, but wouldn't custom lifetime manager give me the same instance all the time? As you might know, principal belongs to a particular user, and app can be serving hundreds of users' requests simultaneously...
Feb 18, 2009 at 9:17 PM
A custom lifetime manager can return whatever instance you want it to. If you look at the webcast demos page (linked to from the codeplex front page) you'll see lifetime managers that pull from ASP.NET cache, a separate store per thread, and several other examples. It can do whatever it is you'd do to pull the principal if the container wasn't involved.

Feb 18, 2009 at 10:41 PM
This is cool. Life time managers are life savers! :) Hmm, I don't see any webcast demos anywhere on codeplex...
Feb 19, 2009 at 3:34 AM
Sorry, my bad. It's linked to from the Unity front page on codeplex, not the Codeplex front page itself. Sorry.

The page is here: demos. You want the Extensibility demos code.

Feb 19, 2009 at 4:09 AM
No problemo, but the recording is no longer available... They say it's been lost! :( Anyways, you've been very helpful and I'm on the right track to solving my problem. For that I thank you very much!

I also noticed something very weird - I wanted to learn more about the custom container extensions (they seem very interesting), but the actual read on it is missing, or it is very hard to find. All articles point to the link ( which tells you to go to codeplex and find it there (here)... Can you plz point me to the right direction?