I currently have a type that I inject into my controllers that’s used for getting and setting session data.
I use this so that I can obtain relevant session information as _sessionData.Username rather than using Session["username"].
I’d like to use this session information across all of my views and would previously have done this by making the SessionData members static instead of injecting the SessionData class into my controller.
I want to avoid using static members as well as having to pass the object to the view in each controller.
What patterns best suit this type of scenario? What do you do to solve this same problem?
Why not just use Session[“username”]? You can wrap the static call in an instance like this:
You gain the maintenance and flexibility of an instance that can then be mocked in a test without having to rewrite the Session class over again
UPDATE
As far as using your instance in your view, what is stopping you from writing a code-block and newing it up and using it? You should be able to write code against it like any other code nuggets in MVC. However, I would suggest against doing this as it defeats the purpose of tiering your architecture. You can either access the session directly, like you would with any HTML page, or you can pass it through as an object from your controller.