Close

adding new service to CatalogController constructor break all the plugins

Hass
10 years ago
#2653 Quote
Avatar
  • 9
Hi

I added new table to the database and added new view to admin everything work fine for me except when trying to accessing the new service that i have create the plugins give an error
if I uninstall the plugins everything work fine how can it seems that any change to the catalogcontroller constructor breaks all the plugin how can I access my service?

thanks
Support
10 years ago
#2656 Quote
Avatar
  • Moderator
  • 1044
Hi Haas,

As some of our products use the Catalog service, they will break if you change its constructor. If you want to use any other service or setting you can use property injection, instead of constructor injection. So instead of adding your new service in the constructor, you can do the following:

private IMyService _myService;

        public IMyService MyService
        {
            get
            {
                if (_myService == null)
                {
                    return EngineContext.Current.Resolve<IMyService>();
                }

                return _myService;
            }
        }

Hope that helps. Please let us know if the errors still occur.
well
9 years ago
#7437 Quote
Avatar
  • 3
But

private IMyService _myService;

is always null or not?
Hass
9 years ago
#7439 Quote
Avatar
  • 9
You need to add in your controller this property also public IMyService MyService
        {
            get
            {
                if (_myService == null)
                {
                    return EngineContext.Current.Resolve<IMyService>();
                }

                return _myService;
            }
        }
And then you use MyService property and its not null anymore
well
9 years ago
#7443 Quote
Avatar
  • 3
OK i write:

private readonly ShippingSettings shippingSettings;

        public ShippingSettings _shippingSettings
        {
            get
            {
                if (shippingSettings == null)
                {
                    return EngineContext.Current.Resolve<ShippingSettings>();
                }

                return shippingSettings;
            }
        }

And VS tell me: Field 'Nop.Web.Controllers.CatalogController.shippingSettings' is never assigned to, and will always have its default value nul
Boyko
9 years ago
#7444 Quote
Avatar
  • Moderator
  • 1570
Hi well,

You should assign the private field _shippingSettings before returning it. The change is in bold. Also it should not be readonly as you won't be able to assign it.

private ShippingSettings _shippingSettings;

        public ShippingSettings ShippingSettings
        {
            get
            {
                if (_shippingSettings == null)
                {
                    _shippingSettings = EngineContext.Current.Resolve<ShippingSettings>();
                }

                return _shippingSettings;
            }
        }


Then you just need to the ShippingSettings property in your code.

Thanks
Regards,
Nop-Templates.com Team
well
9 years ago
#7446 Quote
Avatar
  • 3
I have only changed variables, becouse in nopCommerce are used variables _something for services
Boyko
9 years ago
#7449 Quote
Avatar
  • Moderator
  • 1570
well wrote:
I have only changed variables, becouse in nopCommerce are used variables _something for services


Hi well,

If you don't need to create a property then you can directly assign your variable in the constructor.
You code should be like this:

private readonly ShippingSettings shippingSettings;


Then in the constructor of the CatalogController add this line:

shippingSettings = EngineContext.Current.Resolve<ShippingSettings>();


That is all!

Thanks
Regards,
Nop-Templates.com Team