Override Services

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
Can anyone tell me the syntax for hidding/overriding a method in a class that is in the nop.services project from the nop.web project.

I want to override the method GetAllCategoriesByParentCategoryId to sort by Category Name by creating a class in my themes folder.

The code below doesn't work because it creates a namespace in nop.web and not nop.services

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Nop.Services.Catalog;
using Nop.Core;
using Nop.Core.Caching;
using Nop.Core.Data;
using Nop.Core.Domain.Catalog;
using Nop.Core.Events;

namespace Nop.Services.Catalog
{
    public class CategoryServiceCustom : CategoryService
    {
        private readonly IRepository<Category> _categoryRepository;
        private readonly IRepository<ProductCategory> _productCategoryRepository;
        private readonly IRepository<Product> _productRepository;
        private readonly IEventPublisher _eventPublisher;
        private readonly ICacheManager _cacheManager;



        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="cacheManager">Cache manager</param>
        /// <param name="categoryRepository">Category repository</param>
        /// <param name="productCategoryRepository">ProductCategory repository</param>
        /// <param name="productRepository">Product repository</param>
        /// <param name="eventPublisher">Event publisher</param>
       public CategoryServiceCustom(ICacheManager cacheManager,
            IRepository<Category> categoryRepository,
            IRepository<ProductCategory> productCategoryRepository,
            IRepository<Product> productRepository,
            IEventPublisher eventPublisher)
            : base(cacheManager,
                categoryRepository,
                productCategoryRepository,
                productRepository,
                eventPublisher)
        {
            this._cacheManager = cacheManager;
            this._categoryRepository = categoryRepository;
            this._productCategoryRepository = productCategoryRepository;
            this._productRepository = productRepository;
            _eventPublisher = eventPublisher;
        }

        new public IList<Category> GetAllCategoriesByParentCategoryId(int parentCategoryId,
          bool showHidden = false)
        {

            var query = from c in _categoryRepository.Table
                        orderby c.Name
                        where (showHidden || c.Published) &&
                        !c.Deleted &&
                        c.ParentCategoryId == parentCategoryId
                        select c;

            //filter by access control list (public store)
            //if (!showHidden)
            //{
            //    query = query.WhereAclPerObjectNotDenied(_categoryRepository);
            //}

            var categories = query.ToList();
            return categories;
        }
    }
}
12 years ago
wunpac wrote:
The code below doesn't work because it creates a namespace in nop.web and not nop.services


You have to put your new class in the Nop.Services project instead of your Themes folder.  You also need to mark your function as public override and change the registration in DependencyRegistrar to use your new class instead of CategoryService.

Nop.Web.Framework/DependencyRegistrar.cs
builder.RegisterType<CategoryService>().As<ICategoryService>().InstancePerHttpRequest();

becomes
builder.RegisterType<CategoryServiceCustom>().As<ICategoryService>().InstancePerHttpRequest();
12 years ago
AndyMcKenna wrote:
The code below doesn't work because it creates a namespace in nop.web and not nop.services

You have to put your new class in the Nop.Services project instead of your Themes folder.  You also need to mark your function as public override and change the registration in DependencyRegistrar to use your new class instead of CategoryService.

Nop.Web.Framework/DependencyRegistrar.cs
builder.RegisterType<CategoryService>().As<ICategoryService>().InstancePerHttpRequest();

becomes
builder.RegisterType<CategoryServiceCustom>().As<ICategoryService>().InstancePerHttpRequest();


Ah.. Will be easier to just change the source at upgrade time then in that case. I'm only changing the category sort order to c.Name.

Currently all my changes are in the theme so I don't have to worry about anything when upgrading.

Would be nice if all custom code could go in the same folder.

Thanks for your help!

Darren
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.