I'm really new to Castle Windsor IoC container. I wanted to know if theres a way to store session variables using the IoC container. I was thinking something in the line of this:
I want to have a class to store search options:
public interface ISearchOptions{
public string Filter{get;set;}
public string SortOrder{get;set;}
}
public class SearchOptions{
public string Filter{get;set;}
public string SortOrder{get;set;}
}
And then inject that into the class that has to use it:
public class SearchController{
private ISearchOptions _searchOptions;
public SearchController(ISearchOptions searchOptions){
_searchOptions=searchOptions;
}
...
}
then in my web.config, where I configure castle I want to have something like:
<castle>
<components>
<component id="searchOptions" service="Web.Models.ISearchOptions, Web" type="Web.Models.SearchOptions, Web" lifestyle="PerSession" />
</components>
</castle>
And have the IoC container handle the session object without having to explicitly access it myself.
How can I do this?
Thanks.
EDIT: Been doing some research. Basically, what I want is to have the a session Scoped component. I come from Java and Spring Framework and there I have session scoped beans which I think are very useful to store session data.
My experience has been that Andy's answer does not work, as the
SessionStateModule.End
is never raised directly:For this reason, it becomes pointless to add a HttpModule that does nothing. I have adapted Andy's answer into a single
SessionScopeAccessor
class:}
It is important to call the
SessionEnd
method from yourglobal.asax.cs
file:This is the only way to handle a SessionEnd event.
This solution will work for Windsor 3.0 and above. It;s based on the implementation of PerWebRequest Lifestyle and makes use of the new Scoped Lifestyle introduced in Windsor 3.0.
You need two classes...
An implementation of
IHttpModule
to handle session management. Adding theILifetimeScope
object into session and disposing it again when the session expires. This is crucial to ensure that components are released properly. This is not taken care of in other solutions given here so far.The second class you need is an implementation of
IScopeAccessor
. This is used to bridge the gap between your HttpModule and the built in WindsorScopedLifestyleManager
class.Two
internal static
methods were added toPerWebSessionLifestyleModule
to support this.That's it, expect to register it...
Optionally, I wrapped this registration up into an extension method...
So it can be called like this...
It sounds like you are on the right track, but your SearchOptions class needs to implement ISearchOptions:
You also need to tell Windsor that your SearchController is a component, so you may want to register that in the web.config as well, although I prefer to do it from code instead (see below).
To make Windsor pick up your web.config, you should instantiate it like this:
To make a new instance of SearchController, you can then simply do this:
To register all Controllers in a given assembly using convention-based techniques, you can do something like this:
this might be what your looking for.
And then add