Developer forum

Forum » Development » Dependency Injection

Dependency Injection

Karol Barkowski
Reply

Hello,

I have a custom scheduled task in my project. So I have a class that derives from the "BaseScheduledTaskAddIn" class. Is there any way to use constructor dependency injection in those tasks? Or anywhere in DW in general? So to have something like this:


    public class AwesomeScheduledTask : BaseScheduledTaskAddIn
    {
	private readonly IAwesomeService service;
 
        public AwesomeScheduledTask(IAwesomeService service)
        {
            this.service = service;
        }
 
        public override bool Run()
        {
            service.DoSomethingAwesomeHere();    //service will always be null here???
            return true;
        }
    }

 

And have the "service" reference resolved by the DI container?
I tried multilple different approaches and it seems like the scheduled task instances are created by reflection and they need a parameterless constructor. Does that mean that there's no way to use dependency injection in DynamicWeb?


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Correct.

What do you need injected?

Most services are static singletons and can be accessed directly - i.e. Dynamicweb.Ecommerce.Services.Products will give you the product service.

BR Nicolai

 
Karol Barkowski
Reply

Hi,

I would like to be able to inject my own, custom code in there. For example, if I have a service that performs some pretty complex business logic, and it has quite complex set of internal dependencies, then I what I would like to do is to define all those internal dependencies of that service inside a DI container. It may ba autofac, nInject or whatever else is available. The point is that I don't want to create those dependencies by hand, using a ton of "new" operators whenever I need an instance of my service. And I want to expose the logic of whatever my service is doing as a scheduled task that will be visible in DW admin settings so that admin users will be able to trigger the process when it's needed.

Usually that's not an issue as in all .net projects, even in older .net versions I have access to the dependency injection mechanism. I can configure my website to use it the way I want. But in DW it seems like it is not possible. First of all - I don't have access to global.asax file where the DI container setup should be done. Secondly - it seems that my custom scheduled task class is instantiated trough the reflection mechanism and it must provide a parameterelss constructor. Any other constructor will be just ignored. 

So I'm basically cut of from one of the most basic principles of programming, which is setting up the deplendencies trough some dependency injection container and a composition root pattern and the only option is to instantiate everything by hand, using the "new" operator, right? I hope I'm wrong here as it takes the project architecture and design back to the 90's. 

 
Emil Dumitrescu
Reply
This post has been marked as an answer

Hello,

What you could do it would be to use an autofac module, create a composition root, and inside the scheduled task, to use the container as a service locator (some may say it's an anti-pattern though). You won't have 100% IoC, to have all the dependencies injected into the scheduled task constructor directly, but you could reuse all the dependencies registration used into your container.

Votes for this answer: 1
 
Karol Barkowski
Reply

Yeah, seems like it's the only possibility. Pretty shame honestly. It literally means that using DW forces me to degrade the quality of my own code. 

 

You must be logged in to post in the forum