0

I am trying to implement Dependency injection in my Global.asax file. Below is the code-

    public class MvcApplication : System.Web.HttpApplication
    {

        private readonly IJobTimerBS _objTimer;

           // want to replace this constructor injection code. How ????

        //public MvcApplication(IJobTimerBS jobTimerBS)
        //{
        //    _objTimer = jobTimerBS; 
        //}

       protected void Application_Start()
       {
          UnityConfig.RegisterComponents();
          processingTimer_Elapsed();
       }
       void processingTimer_Elapsed()
       {
        _objTimer.QuoteDeleteJobTimer();
       }
    }

Code in Business layer-

public class JobTimerBS : IJobTimerBS
{
    private readonly IUnitofWork _unitOfWork;
    private IJobTimerRepository _jobTimerRepository;

    public JobTimerBS(IJobTimerRepository jobTimerRepository, IUnitofWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
        _jobTimerRepository = jobTimerRepository;
        _jobTimerRepository.UnitOfWork = unitOfWork;
    }

    public void QuoteDeleteJobTimer()
    {
        _jobTimerRepository.JobTimer();
    }
}   

My question is how can I implement the dependency injection in this case when I can't use constructor injection. How to instantiate the JobTimerBS class in Global.asax present in Business layer.

trailmax
  • 34,305
  • 22
  • 140
  • 234
Anil kumar
  • 525
  • 2
  • 10
  • 32
  • 1
    Chapter 7 in [Dependency Injection in .NET](http://amzn.to/12p90MG) contains details on how to do this, as well as much other information about Dependency Injection. – Mark Seemann Jan 06 '15 at 16:43
  • Hi Mark, I don't have this book at this moment. can you please provide a code snippet as an answer to this question? That will be very helpful. – Anil kumar Jan 06 '15 at 16:52
  • http://stackoverflow.com/questions/20834660/dependency-injection-initialization – stink Jan 06 '15 at 17:20
  • I was reading the book and found this line- "The application entry point in ASP.NET is the global.asax file, and although you can’t compose anything at this point, you can create your mise en place, making everything ready for when the application starts:" Does this mean I can't achieve my requirement? – Anil kumar Jan 06 '15 at 19:46
  • Perhaps this will answer your question... http://stackoverflow.com/q/7752023/126014 – Mark Seemann Jan 07 '15 at 08:14
  • While trying the solution which you have mentioned in above link, I am getting this error- "myapp.Business.JobTimerBS' does not contain a constructor that takes 0 arguments". This is because the constructor I have in business class JobTimeBS is this- "JobTimerBS(IJobTimerRepository jobTimerRepository, IUnitofWork unitOfWork)". – Anil kumar Jan 07 '15 at 14:15

0 Answers0