1
votes

I'm building a WPF application. I'm using Prism 4, and Unity. I want to add two Enterprise Library 5 blocks to the application, Logging and Exception Handling. I have a singleton LoggerFacadeCustom.cs in my Infrastructure class that supports the ILoggerFacade and I've created it in my bootstrapper, and it is generating log files. It "news" up a unity container in its constructor (second code block)

Where do I add the container.resolve for ExceptionManager? How do I connect the Exception handling block to ILoggerFacade in my bootstrapper? How do I get all the exceptions to come out in the same log? Here is my existing 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
  }
1

1 Answers

0
votes

Your bootstrapper is the Composition Root of your application. You should register all dependencies there. And only there. You should never reference the container directly outside the composition root.

If your classes have a dependency you should inject that dependency using a pattern like constructor injection.

Don't use static classes. Static kills dependency injection and testability and it hides dependencies to a point where everything is referenced from everywhere.

Make your logger facade a constructor parameter. You can do the same with the error handling block.

Don't use the container as a ServiceLocator. That is considered an anti-pattern in modern software architecture.