Help! Im losing hair with this constructor!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
Hi all,

Im moving the checkout attributes from being part of the shopping cart, to being part of the checkout. This means the checkout attributes will be shown on the ConfirmOrder view.

To do so, I have added a method to the CheckoutAttributeService called GetCheckoutAttributeWarnings which is essentially the same as the Shopping Cart Method GetShoppingCartWarnings. The ShoppingCartService had a constructor parameter for the ICheckoutAttributeParser. I have removed it and all works fine.

However, when I added it to the CheckoutAttributeService constructor I always get the error message "No parameterless constructor defined for this object". What is wrong with this code:

#region Fields

        private readonly IRepository<CheckoutAttribute> _checkoutAttributeRepository;
        private readonly IRepository<CheckoutAttributeValue> _checkoutAttributeValueRepository;
        private readonly ICheckoutAttributeParser _checkoutAttributeParser;
        private readonly IEventPublisher _eventPublisher;
        private readonly ICacheManager _cacheManager;
        private readonly ILocalizationService _localizationService;
        
        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="cacheManager">Cache manager</param>
        /// <param name="checkoutAttributeRepository">Checkout attribute repository</param>
        /// <param name="checkoutAttributeValueRepository">Checkout attribute value repository</param>
        /// <param name="eventPublisher">Event published</param>
        public CheckoutAttributeService(ICacheManager cacheManager,
            IRepository<CheckoutAttribute> checkoutAttributeRepository,
            IRepository<CheckoutAttributeValue> checkoutAttributeValueRepository,
            //ICheckoutAttributeParser checkoutAttributeParser,
            IEventPublisher eventPublisher,
            ILocalizationService localizationService)
        {
            _cacheManager = cacheManager;
            _checkoutAttributeRepository = checkoutAttributeRepository;
            _checkoutAttributeValueRepository = checkoutAttributeValueRepository;
            //_checkoutAttributeParser = checkoutAttributeParser;
            _eventPublisher = eventPublisher;
            _localizationService = localizationService;

            if (this._checkoutAttributeParser == null)
            {
                this._checkoutAttributeParser = Nop.Core.Infrastructure.EngineContext.Current.Resolve<ICheckoutAttributeParser>();
            }

        }

I have also tried:

public CheckoutAttributeService(ICacheManager cacheManager,
            IRepository<CheckoutAttribute> checkoutAttributeRepository,
            IRepository<CheckoutAttributeValue> checkoutAttributeValueRepository,
            ICheckoutAttributeParser checkoutAttributeParser,
            IEventPublisher eventPublisher,
            ILocalizationService localizationService)
        {
            _cacheManager = cacheManager;
            _checkoutAttributeRepository = checkoutAttributeRepository;
            _checkoutAttributeValueRepository = checkoutAttributeValueRepository;
           _checkoutAttributeParser = checkoutAttributeParser;
            _eventPublisher = eventPublisher;
            _localizationService = localizationService;
        }

I would really appreciate some help on this one - it's driving me round the bend!

Thanks in advance guys

Al
11 years ago
You're creating a circular dependency chain.  CheckoutAttributeParser's only dependency is CheckoutAttributeService.  If you try to add Parser into Service, it can never create them because they depend on each other.  You should probably move your new function to CheckoutAttributeParser or maybe the ShoppingCartService.
11 years ago
You were absolutely right!!! Thanks!

In the end I moved the method to the controller rather than the service - it's only called in a single place in the website so really doesnt need to be part of the service.

Thanks again
Al
11 years ago
Are you able to post the changes you made to get this to work please.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.