Override ViewModelService

Monday, November 25, 2024 6:42:29 PM
  • Posted: Monday, September 23, 2019 6:15 PM
  • 32
I'd like to override Grand.Web.Services.CustomerViewModelService and override one of the virtual methods. I see that you can do this with other services like CustomerService, and in fact it works well. But when I override CustomerViewModelService, my class is never instantiated.


    // this works just fine
    public class OverrideCustomerService : CustomerService, ICustomerService
    {
        public OverrideCustomerService(ICacheManager cacheManager, IRepository<Customer> customerRepository, IRepository<CustomerRole> customerRoleRepository, IRepository<CustomerProduct> customerProductRepository, IRepository<CustomerProductPrice> customerProductPriceRepository, IRepository<CustomerHistoryPassword> customerHistoryPasswordProductRepository, IRepository<CustomerRoleProduct> customerRoleProductRepository, IRepository<CustomerNote> customerNoteRepository, IRepository<Order> orderRepository, IRepository<ForumPost> forumPostRepository, IRepository<ForumTopic> forumTopicRepository, IRepository<BlogComment> blogCommentRepository, IRepository<ProductReview> productReviewRepository, IGenericAttributeService genericAttributeService, IMediator mediator, IServiceProvider serviceProvider)
            : base(cacheManager, customerRepository, customerRoleRepository, customerProductRepository, customerProductPriceRepository, customerHistoryPasswordProductRepository, customerRoleProductRepository, customerNoteRepository, orderRepository, forumPostRepository, forumTopicRepository, blogCommentRepository, productReviewRepository, genericAttributeService, mediator, serviceProvider)
        {
            Console.WriteLine("CustomerService override success");
        }
    }


[code]
    // this does not work, break point is never hit
    public class OverrideCustomerViewModelService : CustomerViewModelService, ICustomerViewModelService
    {
        public OverrideCustomerViewModelService(IExternalAuthenticationService externalAuthenticationService, ICustomerAttributeParser customerAttributeParser, ICustomerAttributeService customerAttributeService, ILocalizationService localizationService, IDateTimeHelper dateTimeHelper, INewsLetterSubscriptionService newsLetterSubscriptionService, IWorkContext workContext, IStoreContext storeContext, ICountryService countryService, IStateProvinceService stateProvinceService, IGenericAttributeService genericAttributeService, IWorkflowMessageService workflowMessageService, IReturnRequestService returnRequestService, IStoreMappingService storeMappingService, IAddressViewModelService addressViewModelService, IOrderService orderService, IDownloadService downloadService, IPictureService pictureService, IProductService productService, IAuctionService auctionService, INewsletterCategoryService newsletterCategoryService, IServiceProvider serviceProvider, CustomerSettings customerSettings, DateTimeSettings dateTimeSettings, TaxSettings taxSettings, ForumSettings forumSettings, ExternalAuthenticationSettings externalAuthenticationSettings, SecuritySettings securitySettings, CaptchaSettings captchaSettings, RewardPointsSettings rewardPointsSettings, OrderSettings orderSettings, MediaSettings mediaSettings, VendorSettings vendorSettings) : base(externalAuthenticationService, customerAttributeParser, customerAttributeService, localizationService, dateTimeHelper, newsLetterSubscriptionService, workContext, storeContext, countryService, stateProvinceService, genericAttributeService, workflowMessageService, returnRequestService, storeMappingService, addressViewModelService, orderService, downloadService, pictureService, productService, auctionService, newsletterCategoryService, serviceProvider, customerSettings, dateTimeSettings, taxSettings, forumSettings, externalAuthenticationSettings, securitySettings, captchaSettings, rewardPointsSettings, orderSettings, mediaSettings, vendorSettings)
        {
            Console.WriteLine("OverrideCustomerViewModelService success");
        }
  
0
  • Posted: Monday, September 23, 2019 6:16 PM
  • 32
Let's try this again.


    public class DependencyRegistrar : IDependencyRegistrar
    {
        public int Order => 1;
        public void Register(ContainerBuilder builder, ITypeFinder typeFinder, GrandConfig config)
        {
            builder.RegisterType<OverrideCustomerService>().As<ICustomerService>().InstancePerLifetimeScope();
            builder.RegisterType<OverrideCustomerViewModelService>().As<ICustomerViewModelService>().InstancePerLifetimeScope();
        }
    }



    // this works just fine
    public class OverrideCustomerService : CustomerService, ICustomerService
    {
        public OverrideCustomerService(ICacheManager cacheManager, IRepository<Customer> customerRepository, IRepository<CustomerRole> customerRoleRepository, IRepository<CustomerProduct> customerProductRepository, IRepository<CustomerProductPrice> customerProductPriceRepository, IRepository<CustomerHistoryPassword> customerHistoryPasswordProductRepository, IRepository<CustomerRoleProduct> customerRoleProductRepository, IRepository<CustomerNote> customerNoteRepository, IRepository<Order> orderRepository, IRepository<ForumPost> forumPostRepository, IRepository<ForumTopic> forumTopicRepository, IRepository<BlogComment> blogCommentRepository, IRepository<ProductReview> productReviewRepository, IGenericAttributeService genericAttributeService, IMediator mediator, IServiceProvider serviceProvider)
            : base(cacheManager, customerRepository, customerRoleRepository, customerProductRepository, customerProductPriceRepository, customerHistoryPasswordProductRepository, customerRoleProductRepository, customerNoteRepository, orderRepository, forumPostRepository, forumTopicRepository, blogCommentRepository, productReviewRepository, genericAttributeService, mediator, serviceProvider)
        {
            Console.WriteLine("CustomerService override success");
        }
    }


[code]
    // this does not work, break point is never hit
    public class OverrideCustomerViewModelService : CustomerViewModelService, ICustomerViewModelService
    {
        public OverrideCustomerViewModelService(IExternalAuthenticationService externalAuthenticationService, ICustomerAttributeParser customerAttributeParser, ICustomerAttributeService customerAttributeService, ILocalizationService localizationService, IDateTimeHelper dateTimeHelper, INewsLetterSubscriptionService newsLetterSubscriptionService, IWorkContext workContext, IStoreContext storeContext, ICountryService countryService, IStateProvinceService stateProvinceService, IGenericAttributeService genericAttributeService, IWorkflowMessageService workflowMessageService, IReturnRequestService returnRequestService, IStoreMappingService storeMappingService, IAddressViewModelService addressViewModelService, IOrderService orderService, IDownloadService downloadService, IPictureService pictureService, IProductService productService, IAuctionService auctionService, INewsletterCategoryService newsletterCategoryService, IServiceProvider serviceProvider, CustomerSettings customerSettings, DateTimeSettings dateTimeSettings, TaxSettings taxSettings, ForumSettings forumSettings, ExternalAuthenticationSettings externalAuthenticationSettings, SecuritySettings securitySettings, CaptchaSettings captchaSettings, RewardPointsSettings rewardPointsSettings, OrderSettings orderSettings, MediaSettings mediaSettings, VendorSettings vendorSettings) : base(externalAuthenticationService, customerAttributeParser, customerAttributeService, localizationService, dateTimeHelper, newsLetterSubscriptionService, workContext, storeContext, countryService, stateProvinceService, genericAttributeService, workflowMessageService, returnRequestService, storeMappingService, addressViewModelService, orderService, downloadService, pictureService, productService, auctionService, newsletterCategoryService, serviceProvider, customerSettings, dateTimeSettings, taxSettings, forumSettings, externalAut
0
  • Posted: Monday, September 23, 2019 6:20 PM
  • 32
Maybe screenshots are better.





0
  • Posted: Tuesday, September 24, 2019 7:26 AM
  • 953
Hi Jon,

Please check the display order in your DependencyRegistrar. Maybe our display order is higher and your service is overridden by ours one.
Best regards,
Patryk

GrandNode Team
1
  • Posted: Tuesday, September 24, 2019 2:58 PM
  • 32
Order is 1. I've also tried:

public int Order => int.MinValue;
0
  • Posted: Tuesday, September 24, 2019 3:11 PM
  • 32
CustomerViewModelService is registered in Grand.Web.Infrastructure.DependencyRegistrar with Order of 2.
0
  • Posted: Tuesday, September 24, 2019 3:18 PM
  • 32
OH! You are right.

Apparently the number 1 is *higher* than the number 2.

Who knew!?!?

Thanks again for the quick and accurate response, Patryk.
0
back to top
Filters