Now we just need to wire up our presenter in the Page_Load event, passing the implementation of the ISessionDataProvider into the Presenter:
protected void
Page_Load(object sender, EventArgs e)
{
WebSessionDataProvider session = new WebSessionDataProvider();
AccountCategoryPresenter presenter =
new AccountCategoryPresenter(AccountCategory1,
AccountingFacade.Instance,session);
AccountCategory1.AttachPresenter(presenter);
presenter.Initialize();
}
The class that manages the Session in the Service Layer (or beneath) will be implemented as a Singleton here and provides
methods for fetching/setting the ISessionDataProvider using IoC (again). The comments should explain what happens:
/// <summary>
/// Singleton IoC container
for managing sessions that might come from a variety of UI
/// environments. Inspired
(stolen) from an email exchange with Greg Young at
///
http://codebetter.com/blogs/gregyoung/. This container accepts any provider
that implements
/// the <see cref="ISessionDataProvider"/> interface.
/// </summary>
/// <remarks>
/// For cross-threading
ops, consider the following remark from Greg:
/// "The reason I ask
if if you have cross thread behaviors it that this will obviously not work very
/// well if you are
switching threads (but keeping the same context) if you want to do this the
best way
/// of doing it is to move
your context to the new thread, this would require a context map as well
/// (where the manager
pointed to the map) .. you could then use the map item
/// (once brokerred to the
other thread to reset your current scope for that thread from within the facade
/// layer itself but this
can be a much more tricky scenario ... )".
/// </remarks>
public class SessionManager
{
#region Singleton Instantiation
SessionManager() { }
public static SessionManager Instance
{
get{ return Singleton.instance; }
}
class Singleton
{
static Singleton() { }
internal static readonly SessionManager
instance =
new SessionManager();
}
#endregion
private Hashtable
_hash = new Hashtable();
/// <summary>
/// Provider must implement
the <see
cref="ISessionDataProvider"/>
interface.
/// </summary>
/// <param name="scope"></param>
public void
SetCurrentSession(ISessionDataProvider scope)
{
lock (_hash)
{
_hash[Thread.CurrentThread.GetHashCode()] = scope;
}
}
/// <summary>
/// Cross-threaded
operations will not be supported here yet (if keeping the same context).
/// See remarks above if
implementing cross-threading, uni-contextual processes.
/// </summary>
/// <returns></returns>
public ISessionDataProvider
GetCurrentSession()
{
return _hash[Thread.CurrentThread.GetHashCode()]
as ISessionDataProvider;
}
/// <summary>
/// Session data provider
for the current thread...only applies to
/// use cases where
multi-threading is not required (typical)
/// </summary>
public SessionManager
Session
{
get
{
return SessionManager.Instance.GetCurrentSession();
}
}
public void DoMethod()
{
object
obj = Session[“key”] ;
}
This should help me for testing any kind of mock session data.