Super constructor call in C #

I have classes like AccountsController, ProductsController etc. that all inherit from BaseController. Unity installs my services as needed. These classes also require _sequence maintenance. Since this is a general requirement for all classes, I would like to code this in BaseController.

public class AccountsController : BaseController { public AccountsController( IService<Account> accountService) { _account = accountService; } public class ProductsController : BaseController { public ProductsController( IService<Account> productService) { _product = productService; } public class BaseController : Controller { public IService<Account> _account; public IService<Product> _product; protected ISequenceService _sequence; public BaseController( ISequenceService sequenceService) { _sequence = sequenceService; } 

But how can I do this? Should I customize the BaseController call inside the constructors of each of the AccountsController and ProductsController?

+7
source share
1 answer

You can bind the constructors :

 public class ProductsController : BaseController { public ProductsController( IService<Account> productService) : base(productService) { _product = productService; } } 

Note that the chained BaseController (using base ) is passed the productService parameter, it could be anything.

Update:

You can do the following (injection of addictions of poor men):

 public class ProductsController : BaseController { public ProductsController( IService<Account> productService) : base(new SequenceService()) { _product = productService; } } 

Or, go into the ISequenceService through your inheriting types:

 public class ProductsController : BaseController { public ProductsController( IService<Account> productService, ISequenceService sequenceService) : base(sequenceService) { _product = productService; } } 
+12
source

All Articles