Click here to Skip to main content
11,702,890 members (73,080 online)
Click here to Skip to main content

Tagged as

Creating Per-Session Services with Hiro

, 1 Nov 2011 LGPL3 3.3K
Rate this:
Please Sign up or sign in to vote.
Over the past week, I've had a few people ask me how they can use Hiro to create services that are instantiated once per web session.
Over the past week, I've had a few people ask me how they can use Hiro to create services that are instantiated once per web session. Ideally, Hiro should be able to do something like this:

// Instantiate the IFoo service
var context = HttpContext.Current;

if (context.Session["SomeFooKey"] == null)
  context.Session["SomeFookey"] = container.GetInstance<IFoo>();

return (IFoo)context.Session["SomeFooKey"];

The code itself is pretty simple, but the problem is that this can get quite cumbersome if you have more than a few services that need to be stored in the Session object. There has to be some way to do this with Hiro using a single method call, and indeed, Hiro makes creating per-session services just as equally effortless:

// Register the Foo service into the DependencyMap
var map = new DependencyMap();
<span style="font-weight: bold;">map.AddPerSessionService(typeof(IFoo), typeof(Foo));</span>

// Compile the container
var container = map.CreateContainer();

var foo = container.GetInstance<IFoo>();

// ...use the IFoo per-session service here (just like any other service)

As you can see from the example above, Hiro abstracted away the details of having to deal with caching your services into a Session object. In fact, all you have to do to add per-session services to your dependency maps is to use the AddPerSessionService extension method, compile your container and use it like any other Hiro-compiled container. It's just that simple.

NOTE: You can download the Hiro.Web extensions (including the per-session service support extension method) here.


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Philip Laureano
Software Developer (Senior) Readify
Australia Australia
No Biography provided

You may also be interested in...

Comments and Discussions

Questiongood! Pin
kkmylove4-Jan-13 15:33
memberkkmylove4-Jan-13 15:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 1 Nov 2011
Article Copyright 2011 by Philip Laureano
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid