I put my variables into a base class like this:
public class BaseController : Controller
{
protected BaseViewModel vm { get; set; }
protected IAccountService _account;
protected IDataSourceService _dataSource;
protected IPackageService _package;
Each controller currently does the following:
private void InitializeServices(string ds, string ac = null, string pr = null, string pa = null, string co = null) {
_dataSource = new DataSourceService();
_account = new AccountService(ds);
_product = new ProductService(ds);
_package = new PackageService(ds);
But is there a way that I can now change this, put the method InitializeService into the base class and then call it from the derived class. If it’s possible then how would I call that method from the derived class and what would be the modifier for that class once it is inside the base class? Would that also be protected?
Just move the code for the method to the base class with a modifier of
protected. You can then just call it from your derived class as though it was a member of that class (which it is by inheritance).