2

I am trying out Enterprise Library 5.0. My plan is to use it for coming project with ASP.Net MVC. I saw a few examples and was trying out the logging. In of the example, I see the developer get an instance of logger through:

LogWriter logWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

The above code works well in my application. In this code, basically we are getting an instance from existing container, right? My questions are:

  1. How and when is this instance of container created?
  2. Can I use the same container in my MVC?
  3. What was the idea of using this method to create container and why not use the EnterpriseLibraryContainer instance in MVC?

Thanks in advance for you comments,ideas & solution.

Abdel Raoof Olakara
  • 19,223
  • 11
  • 88
  • 133

1 Answers1

3

The Enterprise Library container is automatically created the first time it's accessed if it wasn't explicitly initialized beforehand.

The EnterpriseLibrary.Current instance is intended for designs that would otherwise not want or care about a container, and just want to get the Entlib objects. If you're already using a DI container (I'm assuming Unity since you don't say) it's faster and easier to just get your Entlib objects directly from the container you're already using.

With Unity, all you need to do is, when you create the container, do:

container.AddNewExtension<EnterpriseLibraryCoreExtension>();

and that'll load the entlib configuration into the container. From there, you can resolve Entlib objects as dependencies the same way you resolve any other dependencies. So, for the above code, you could get a LogWriter simply by having a constructor parameter of type LogWriter on your controller.

I wouldn't recommend using EnterpriseLibraryContainer for all your IOC needs in an MVC app; I'd say use an explicit container you control instead.

Chris Tavares
  • 29,165
  • 4
  • 46
  • 63
  • Using EnterpriseLibraryCoreExtension to inject a LogWriter seems to create a new LogWriter instance. Do you know how to have it inject an existing instance instead, or do I need to use EnterpriseLibraryContainer.Current.GetInstance() to do that? – Tuan Aug 09 '11 at 15:59
  • @Chris Can you expand your answer for Tuan's last comment (above)? How do we register the EnterpriseLibraryCoreExtension so that it creates only one instance of the LogWriter and reuses it? – Thiago Silva Feb 07 '12 at 00:03
  • @Chris also, can you tell us if the approach from your answer is better than trying to use the facade class "Logger"? – Thiago Silva Feb 07 '12 at 00:04
  • The code inside Entlib specifies the LogWriter lifetime as a singleton, so I don't know why you are seeing separate instances. the only thing I can think of is you're using multiple containers. – Chris Tavares Feb 09 '12 at 06:25
  • 1
    @Thiago: Using constructor injection instead of the facade improves testability, since you can mock out the LogWriter easily. – Chris Tavares Feb 09 '12 at 06:25
  • @ChrisTavares Ok, so that makes sense, however how do you mock out the LogWriter? Does it implement an interface that I should be using when injecting via constructor? Or should I be wrapping the LogWriter in my own "Logger" class, as seen in this post: http://stackoverflow.com/questions/5521675/entlib5-loggin-application-block-not-logging-to-event-log-exception-type-logw – Thiago Silva Mar 11 '12 at 04:51
  • 1
    LogWriter is an abstract class, so it is the interface. Derive your mock from LogWriter and inject that instead. – Chris Tavares Mar 12 '12 at 20:11