2

I patched the Initialize Pipeline InitializeControllerFactory processor to point to my new solution InitializeControllerProcessor via:

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <initialize>
        <processor type="Sitecore.Mvc.Pipelines.Loader.InitializeControllerFactory, Sitecore.Mvc">
          <patch:attribute name="type">Test.Engine.Storefront.Core.Pipelines.ControllerFactory.InitializeControllerFactory, Test.Engine.Storefront.Core</patch:attribute>
        </processor>
      </initialize>
    </pipelines>
  </sitecore>
</configuration>

The sonfig is reflected conrrectly in /sitecore/admin/showconfig.aspx

I added a new class that implements IWindsorInstaller and pointed my Initialize Controller factory class to use Windsor.cs that uses that class

public class TestDependenciesInstaller : IWindsorInstaller
{
    /// <summary>
    /// Performs the installation in the <see cref="T:Castle.Windsor.IWindsorContainer" />.
    /// </summary>
    /// <param name="container">The container.</param>
    /// <param name="store">The configuration store.</param>
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Classes.FromAssemblyNamed("Sitecore.Reference.Storefront.Powered.by.SitecoreCommerce")
          .BasedOn<IController>()
          .LifestyleTransient());

        container.Register(Classes.FromAssemblyNamed("Sitecore.Reference.Storefront.Powered.by.SitecoreCommerce")
          .BasedOn<IHttpController>()
          .LifestyleTransient());

        container.Register(Classes.FromThisAssembly()
          .BasedOn<IController>()
          .LifestyleTransient());

        container.Register(Classes.FromThisAssembly()
          .BasedOn<IHttpController>()
          .LifestyleTransient());

        container.Register(Classes.FromAssemblyNamed("Test.Engine.Storefront")
          .BasedOn<IController>()
          .LifestyleTransient());

        container.Register(Classes.FromAssemblyNamed("Test.Engine.Storefront")
          .BasedOn<IHttpController>()
          .LifestyleTransient());

        container.Register(Classes.FromAssemblyContaining<CartServiceProvider>()
          .Pick()
          .WithService.DefaultInterfaces());

        container.Register(Classes.FromAssemblyNamed("Sitecore.Reference.Storefront.Powered.by.SitecoreCommerce")
          .Pick()
          .WithService.DefaultInterfaces());

        container.Register(Classes.FromThisAssembly()
          .Pick()
          .WithService.DefaultInterfaces());

        container.Register(Classes.FromAssemblyNamed("Sitecore.Commerce.Connect.CommerceServer").Pick());

        container.Register(Classes.FromAssemblyNamed("Sitecore.Reference.Storefront.Common").Pick());

        container.Register(Classes.FromAssemblyNamed("Test.Engine.Storefront").Pick());
    }
}

Then I added a new controller that extends catalogController and updated the rendering in sitecore to point to that controller.

public class TestCatalogController : CatalogController
    {
        public TestCatalogController (
            InventoryManager inventoryManager, 
            ContactFactory contactFactory, 
            AccountManager accountManager, 
            CatalogManager catalogManager, 
            GiftCardManager giftCardManager, 
            PricingManager pricingManager, 
            CartManager cartManager) : base(inventoryManager, contactFactory, accountManager, catalogManager, giftCardManager, pricingManager, cartManager)
        {
        }

        /// <summary>
        /// An action to manage data for the ProductList
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="facetValues">The facet values.</param>
        /// <param name="sortField">The sort field.</param>
        /// <param name="sortDirection">The sort direction.</param>
        /// <returns>
        /// The view that represents the ProductList
        /// </returns>
        public ActionResult MultipleProductLists(
            [Bind(Prefix = StorefrontConstants.QueryStrings.Paging)] int? pageNumber,
            [Bind(Prefix = StorefrontConstants.QueryStrings.Facets)] string facetValues,
            [Bind(Prefix = StorefrontConstants.QueryStrings.Sort)] string sortField,
            [Bind(Prefix = StorefrontConstants.QueryStrings.SortDirection)] CommerceConstants.SortDirection? sortDirection)
        {
            var productSearchOptions = new CommerceSearchOptions
            {
                NumberOfItemsToReturn = StorefrontConstants.Settings.DefaultItemsPerPage,
                StartPageIndex = 0,
                SortField = sortField
            };

            var currentRendering = RenderingContext.Current.Rendering;
            if (string.IsNullOrEmpty(currentRendering.DataSource))
            {
                return GetNoDataSourceView();
            }

            var datasource = currentRendering.Item;

            var multipleProductSearchResults = this.CatalogManager.GetMultipleProductSearchResults(datasource, productSearchOptions);
            if (multipleProductSearchResults != null)
            {
                multipleProductSearchResults.Initialize(this.CurrentRendering);
                multipleProductSearchResults.DisplayName = datasource.DisplayName;

                var products = multipleProductSearchResults.ProductSearchResults.SelectMany(productSearchResult => productSearchResult.Products).ToList();
                this.CatalogManager.GetProductBulkPrices(this.CurrentVisitorContext, products);
                this.CatalogManager.InventoryManager.GetProductsStockStatusForList(this.CurrentStorefront, products);

                foreach (var productViewModel in products)
                {
                    Item productItem = multipleProductSearchResults.SearchResults
                        .SelectMany(productSearchResult => productSearchResult.SearchResultItems)
                        .Where(item => item.Name == productViewModel.ProductId).FirstOrDefault();
                    productViewModel.CustomerAverageRating = this.CatalogManager.GetProductRating(productItem);
                }

                return View(this.GetRenderingView("ProductRecommendation"), multipleProductSearchResults);
            }

            return View(this.GetRenderingView("ProductRecommendation"));
        }
    }

However, when I deploy this solution in sitecore commerce out of the box solution, I get an error asking me to ensure that controller has a parameterless constructor. How do I extend the solution to inject the dependencies in constructor correctly?

  • Also tried adding container.Register(Classes.FromAssemblyNamed("Sitecore.Reference.Storefront.Powered.by.SitecoreCommerce").Pick()); to TestDependenciesInstaller Install method but didn't help – Commerce Test Aug 16 '18 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.