誰かが興味を持っている場合は、グーグル検索中に:私はカスタムグローバルクラスの使用を終了しました。物事を開始するための1つの静的呼び出し。残りはインターフェース/アダプターパターンとして。この例では、UnityとEnterpriseLibraryを登録しています。また、認証されたロギング要求からの重要な詳細を記録します。
...これは部分的な抜粋です...説明のため
MVCアプリケーションGlobal.asaxからの呼び出し
public class BosMasterWebApplication : System.Web.HttpApplication
{
private bool IsBootStraped;
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
IsBootStraped = false;
}
public override void Init()
{
base.Init();
// handlers managed by ASP.Net during Forms authentication
AuthenticateRequest += new EventHandler(AuthHandler);
PostAuthenticateRequest += new EventHandler(PostAuthHandler);
}
public void AuthHandler(object sender, EventArgs e)
{
// if you need to setup anything
}
public void PostAuthHandler(object sender, EventArgs e)
{
// called several times during the authentication process
if (!IsBootStraped)
BosGlobal.HttpBootStrap(this);
if (Request.IsAuthenticated)
{
// did FormsAuthentication.SetAuthCookie was executed if we get here, so set username
BosGlobal.BGA.IBosCurrentState.CurrentUser.SetFromEnvironment();
}
}
}
/// <summary>
/// Central adapter management. Dont abuse this class
/// </summary>
public static class BosGlobal
{
public static IBosGlobalAdpater BGA { get; private set; }
// set up the adapter
static BosGlobal()
{
BGA = new BosGlobalAdapter( );
}
public static void HttpBootStrap(HttpApplication httpApplication)
{
BGA.BootStrap(httpApplication);
}
public static void LocalBootStrap(string machineName)
{ // the LOCAL WPF bootstrap alternative... no http request exists.
BGA.BootStrap(machineName);
}
}
public class BosGlobalAdapter : IBosGlobalAdpater
{
public static bool IsBootStrapped { get; private set; }
/// Unity Container with key dependencies registered
public IUnityContainer IUnityContainer { get; private set; } //IOC Container
//Adapters
public IBosLogAdapter ILogAdapter { get; private set; }
public IBosExceptionManagerAdapter ExceptionManagerAdapter { get; private set; }
public BosGlobalAdapter()
{
IsBootStrapped = false;
IUnityContainer = new UnityContainer();// one container per work process. managing and resolving dependencies
}
public void BootStrap(HttpApplication httpApplication )
{
IBosCurrentState = new BosCurrentState(httpApplication);
BootStrapEnterpriseLibrary();
BootStrapAdapters();
IsBootStrapped = true;
}
private void BootStrapAdapters()
{
ILogAdapter = new BosLogAdapter(IUnityContainer.Resolve<LogWriter>());
ExceptionManagerAdapter = new BosExceptionManagerAdapter(IUnityContainer.Resolve<ExceptionManager>());
}
private void BootStrapEnterpriseLibrary()
{ // now we tell unity about the container manager inside EntLib.
// we dont want 2 containers, so we tell UNity look after EntLib as well please
IUnityContainer.AddNewExtension<EnterpriseLibraryCoreExtension>();
}