LoggingOperationHandler DI Clarification

Jul 1, 2011 at 4:13 AM

Hi. I'm not sure if this is the right place to ask this question.

I was looking at the LoggingOperationHandler as it is using DI. I just had a question on, how would you instantiate ILogger when adding this to the configuration?

The following implementation comes to mind first:

container = new UnityContainer()
                .RegisterType<ISomeRepo, <ActualRepo>();

ILogger logger = new Logger();

var configuration = HttpHostConfiguration.Create()
                .SetResourceFactory(new UnityFactory(container))
                .SetOperationHandlerFactory(new LoggingOperationHandler(logger));

If I didn't want to use Logger implementation during testing, I'd have to modify the actual code.

Would the better way of doing this be the following? I haven't used .NET in a few years and it has come a long way :-).

container = new UnityContainer()
                .RegisterType<ISomeRepo, ActualRepo>()
                .RegisterType<ILogger, Logger>();

            var configuration = HttpHostConfiguration.Create()
                .SetResourceFactory(new UnityFactory(container))
                .SetOperationHandlerFactory(new LoggingOperationHandler(container.Resolve<ILogger>()));

This would allow me to switch to MockLogger when writing my tests. Though I am still unsure if this is a good way of doing it.