Using AutoFac in ASP.Net with Session Object

269 views
Skip to first unread message

John O'Brien

unread,
Jul 3, 2008, 3:40:09 PM7/3/08
to Autofac
I probably need is a little guidance.

I have a static class/property that I use to store a CustomPrincial
and this looks like a good candidate of a DI container. The property
is used throughout our web application for security and is also used
in our business layer.

See code here: http://jdobrien.paste-bin.com/13518

So when I use ApplicationContext.User...

...from a web application, the preperty uses Session between page
requests.
...from Unit Tests, the property comes from Thread.CurrentPrincipal

I am unure how(if) I should using a DI container in this scenario.

Any help, guidance or criticism is appreciated.



Nicholas Blumhardt

unread,
Jul 5, 2008, 9:07:39 AM7/5/08
to aut...@googlegroups.com
Hi John,

Looks like a good candidate for dependency injection, there are a few ways to approach it. Here is one straightforward way, YMMV.

1. Make your ApplicationContext class into an regular non-static class
2. Extract the IApplicationContext interface
3. Update anything that uses ApplicationContext to accept an IApplicationContext in its constructor instead
4. Simplify the ApplicationContext class into HttpApplicationContext (remove the thread-local stuff)
5. Register HttpApplicationContext as the provider of IApplicationContext in your real application
6. Consider creating a stubbed implementation of IApplicationContext to use in your unit tests (or use a mocking library such as Moq)

I suspect step 5 is where things are going to get tough. If you're not already using dependency injection and a DI container, there's probably a lot to consider right about here. The likely result is that any classes using IApplicationContext will themselves end up being created through the container.

Check out:

http://code.google.com/p/autofac/wiki/ExistingApplications
http://kohari.org/2008/06/20/applying-ioc-to-brownfield-projects/
http://tech.groups.yahoo.com/group/altdotnet/message/10434

...for some discussion and advice.

Hope this helps,

Nick
Reply all
Reply to author
Forward
0 new messages