How to setup DI in Sitecore using Sitecore Dependency Injection

  1. Create Interface with required method(s)
  2. Create Model with required properties
  3. Create Repository Class that inherits the Interface created
  4. Create Controller
  5. Inject the dependency as constructor injection
  6. Create service for the controller, Repository and Interface in service collection using DI
  7. Create the view file to bind the model data
  8. Register the DI in sitecore using patch config
  9. Finally create your controller rendering in the sitecore and test, everything should work now.

      Create Interface with required method(s)


using Feature.WeatherForecast.Models;

namespace Feature.WeatherForecast.Interfaces
{
    public interface IWeatherForecastRepository
    {
        ForecastData IsCloudy();
    }
}

Create Model with required properties


namespace Feature.WeatherForecast.Models
{
    public class ForecastData
    {
        public bool IsCloudy { get; set; }
    }
}

Create Repository Class that inherits the Interface created

using Feature.WeatherForecast.Interfaces;
using Feature.WeatherForecast.Models;

namespace Feature.WeatherForecast.Repositories
{
    public class WeatherForecastRepository : IWeatherForecastRepository
    {
        public ForecastData IsCloudy()
        {
            return new ForecastData { IsCloudy = true };
        }
    }
}

Create Controller


using Feature.WeatherForecast.Interfaces;
using Sitecore.XA.Foundation.Mvc.Controllers;
using System.Web.Mvc;

namespace Feature.WeatherForecast.Controllers
{
    public class WeatherForecastController : StandardController
    {
        
    }
}

Inject the dependency as constructor injection

using Feature.WeatherForecast.Interfaces;
using Sitecore.XA.Foundation.Mvc.Controllers;
using System.Web.Mvc;

namespace Feature.WeatherForecast.Controllers
{
    public class WeatherForecastController : StandardController
    {
        private readonly IWeatherForecastRepository _repository;

        public WeatherForecastController(IWeatherForecastRepository repository)
        {
            _repository = repository;
        }

        public ActionResult GetWeatherForecast()
        {
            var data = _repository.IsCloudy();
            return View("~/Views/WeatherForecast/GetWeatherForecast.cshtml", data);
        }
    }
}

Create service for the controller, Repository and Interface in service collection using DI


using Feature.WeatherForecast.Controllers;
using Feature.WeatherForecast.Interfaces;
using Feature.WeatherForecast.Repositories;
using Microsoft.Extensions.DependencyInjection;
using Sitecore.DependencyInjection;

namespace Feature.WeatherForecast
{
    public class Configurator : IServicesConfigurator
    {
        public void Configure(IServiceCollection serviceCollection)
        {
            serviceCollection.AddTransient<WeatherForecastController>();
            serviceCollection.AddTransient<IWeatherForecastRepository, WeatherForecastRepository>();
        }
    }
}

Create the view file to bind the model data

@using Feature.WeatherForecast.Models

<div>@Model.IsCloudy</div>

Register the DI in sitecore using patch config


<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <services>
      <configurator type="Feature.WeatherForecast.Configurator, Feature.WeatherForecast" />
    </services>
  </sitecore>
</configuration>

1 comment:

  1. https://sitecorepeanuts.blogspot.com/2024/03/how-to-setup-di-in-sitecore-using.html

    ReplyDelete

Troubleshoot personalization on xm cloud

Here are the troubleshooting steps below that helped personalization to work. We checked the events analytics in the page builder but it was...