我正在構建一個WPF應用程序。我正在使用Prism 4和Unity。我想嚮應用程序添加兩個企業庫5塊,記錄和異常處理。我在我的Infrastructure類中有一個支持ILoggerFacade的單例LoggerFacadeCustom.cs,並且在我的引導程序中創建了它,並且它正在生成日誌文件。它在它的構造函數(第二個代碼塊)中生成一個統一容器的「新聞」如何整合棱鏡,統一和企業庫
我在哪裏添加container.resolve for ExceptionManager?如何將異常處理塊連接到引導程序中的ILoggerFacade?我如何讓所有例外在同一個日誌中出現?這裏是我現有的bootstrapper.cs
public class Bootstrapper : UnityBootstrapper {
protected override ILoggerFacade CreateLogger() {
return LoggerFacadeCustom.Instance;
}
protected override DependencyObject CreateShell() {
return Container.Resolve<Shell>();
}
protected override void InitializeShell() {
base.InitializeShell();
App.Current.MainWindow = (Window)Shell;
App.Current.MainWindow.Show();
//Other shell stuff...
}
protected override IModuleCatalog CreateModuleCatalog() {
var catalog = new ModuleCatalog();
//These primary modules must register their own services as if they were acting independantly
catalog.AddModule(typeof(XmlCommentMergeModule));
//These support modules require at least one primary module above to be added first
catalog.AddModule(typeof(ToolboxHeaderModule));
catalog.AddModule(typeof(ToolboxFooterModule));
catalog.AddModule(typeof(ToolboxStartModule));
return catalog;
}
}
LoggerFacadeCustom:
public class LoggerFacadeCustom : ILoggerFacade {
private static readonly LoggerFacadeCustom _instance = new LoggerFacadeCustom();
public static LoggerFacadeCustom Instance { get { return _instance; } }
private LoggerFacadeCustom() {
var container = new UnityContainer();
container.AddNewExtension<EnterpriseLibraryCoreExtension>();
_logWriter = container.Resolve<LogWriter>();
}
private readonly LogWriter _logWriter;
public void Write(string message) { Write(message, null); }
public void Write(string message, string category, int priority) {
_logWriter.Write(message, category, priority);
}
public void Write(string message, Dictionary<string, object> properties) {
_logWriter.Write(message, LiteralString.LogCategoryProcess, properties);
}
#region ILoggerFacade Members
public void Log(string message, Category category, Priority priority) {
throw new NotImplementedException();
}
#endregion
}
微軟的棱鏡書建議在每個模塊中使用容器來統一。向我解釋一個模塊如何在不使用容器的情況下注冊接口?但是,您應該使用DI獲取該容器,而不是自己創建 – Alan
@Alan您可以將該配置外包給從UnityContainerExtension派生的特殊類,併爲模塊捆綁配置。讓引導程序搜索這些類並將它們添加到單個容器實例中。或者你可以在模塊中注入一個容器實例並在那裏進行配置。我認爲這也是一個有效的捷徑。 –
對我來說,每個模塊都有一個IModule類,並通過構造函數注入容器。然後,Initialize將註冊這些類型並解析模塊擁有的所有服務。 – Alan