How to update a class constructor

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 years ago
Hello.

I'm using nopCommerce 4.0.  I need to get to SettingService in Nop.Services/Task.cs.  I've added it like this:


// Add the name space:
using Nop.Services.Configuration;

// Add field:
private SettingService _settingService;

// Update the constructor:
public Task(ScheduleTask task, SettingService settingService)
{
    ScheduleTask = task;
    settingService = _settingService;
}

// Use the value:
var someVariable = _settingService.GetSettingByKey<string>("SomeSetting");


Nop.Services builds fine.  Nop.Web, however, is now angry because ScheduleTaskController.cs instantiates Task with just one parameter:

var task = new Task(scheduleTask);


I can get around this by adding _settingService here, but then I'm updating the constructor for ScheduledTaskController.  What is the proper way to handle this situation?  I've gone down this path before where I update constructors in 9 or 10 classes because I needed something new in one class.  Is there a better way?  There must be something I don't understand here.

Thanks!

Jeremy
3 years ago
Task is a special case, but if any type of instance is being created explicitly rather than through dependency injection, then you need to use Resolve<>().  Similar to how task is already doing it in a few places - e.g.
var scheduleTaskService = EngineContext.Current.Resolve<IScheduleTaskService>();
3 years ago
Great!  That works.

I had seen that around, but some time ago I was told that was a bad practice.  Maybe it's just bad to do the resolve thing in a view.

At any rate, now I can get to my settings from Task.  Thanks!

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