resolve DI

Dependency Injection with Sitecore 9 Scheduling

With this post I hope to draw attention to the ability to use dependency injection with Sitecore’s Scheduling Agents, for more information about agents see Johns blog post.

Most solutions now use DI with MVC controllers which is great, but I have noticed it is not used with agents?

Which is a pity as it is very simple to do this in Sitecore 9.0, Just add resolve =”true”.

resolve DI

Then add any dependencies to your constructor as required.

private readonly ILogger _logger;
private readonly UpdateSeatAvailabilityService _updateSeatAvailabilityService;

public UpdateSeatAvailabilityAgent(
       [NotNull]ILogger logger, 
       [NotNull]UpdateSeatAvailabilityService updateSeatAvailabilityService)
        {
            Assert.ArgumentNotNull(logger, nameof(logger));
            Assert.ArgumentNotNull(updateSeatAvailabilityService, nameof(updateSeatAvailabilityService));
            _logger = logger;
            _updateSeatAvailabilityService = updateSeatAvailabilityService;
        }

Hope this helps, Alan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.