1. If there's a service provider in the constructor
serviceProvider.GetService<IRenderingConfig>()
2. If no - use this
DependencyResolver.Current.GetService<IErrorsController>())
ServiceLocator.ServiceProvider.GetService<ICountryRepository>();
3. Uses Sitecore.DependencyInjection
4. Without passing in the constructor new the concrete type and populate properties
To register the type:
using Globus.Feature.Products.Controllers;
using Microsoft.Extensions.DependencyInjection;
using Sitecore.DependencyInjection;
using Sitecore.LayoutService.Configuration;
namespace Globus.Feature.Products.Services
{
public class ServiceConfigurator : IServicesConfigurator
{
public void Configure(IServiceCollection serviceCollection)
{
serviceCollection.AddScoped(typeof(IRenderingConfiguration), typeof(DefaultRenderingConfiguration));
}
}
}
Then add it in the config:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<httpRequestBegin>
<processor type="Globus.Feature.Products.Pipelines.HttpRequestBegin.ItineraryItemResolver, Globus.Feature.Products"
patch:before="processor[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']"
resolve="true" />
</httpRequestBegin>
<initialize>
<processor type="Globus.Feature.Products.Pipelines.Initialize.RegisterCustomRoute, Globus.Feature.Products" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" />
</initialize>
</pipelines>
<services>
<configurator type="Globus.Feature.Products.Services.ServiceConfigurator, Globus.Feature.Products"/>
</services>
</sitecore>
</configuration>
Then in the controller class:
protected readonly IRenderingConfiguration _JSSConfig;
Then add to constructor:
public ProductsController(
IItinerariesRepository itinerariesRepository,
IDeparturesRepository departuresRepository,
IPromotionsRepository promotionsRepository,
IRenderingConfiguration jssconfig
)
======================
public ProductsController(
IItinerariesRepository itinerariesRepository,
IDeparturesRepository departuresRepository,
IPromotionsRepository promotionsRepository
)
{
_itinerariesRepository = itinerariesRepository;
_departuresRepository = departuresRepository;
_promotionsRepository = promotionsRepository;
// 1. When passed in the constructor
//_JSSConfig = jssconfig;
// 2. When 'newed'
//_JSSConfig = new DefaultRenderingConfiguration();
//_JSSConfig.Name = "jss"; and so on
// 3.
_JSSConfig = DependencyResolver.Current.GetService<IRenderingConfiguration>();
//4.
_JSSConfig = ServiceLocator.ServiceProvider.GetService<IRenderingConfiguration>();
}
No comments:
Post a Comment