Dependency Injection using IUnityContainer

In this example sample for ErrorLogging to Windows which can be viewed using Windows EventViewer >> Windows Log >> Application is explained along with dependency injection.

In the constructor get the instance of IUnityContainer as below

public class MvcTest : IMvcTest
{
   private readonly IUnityContainer _unity;
   public MvcTest (IUnityContainer unity)
   {
      _unity = unity;      
   }

   public void SomeProcess()

   {
      //Here Utils is the class where you need will need the instance
      //with all its dependency resolved
      var loggerUtil = _unity.Resolve<Utils>();
     loggerUtil.LogEvent(System.Diagnostics.EventLogEntryType.Information,        sywrDataLoaderMessage, "Data load Successfully", _settings.TeraDataProcessEventId);
   }
}


public class Utils
{

   private readonly ISettings _settings;

   public Utils(ISettings settings)
   {
      _settings = settings;
   }


public void LogEvent(EventLogEntryType eventLogType,string sourceMessage,string logEntry,int eventId)

{
    EventLog eventLog = new EventLog("Application");
    eventLog.Source = sourceMessage;
    eventLog.WriteEntry(logEntry, eventLogType, eventId);
}

}

Comments

Popular posts from this blog

WEB API

MVC