Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling super constructor in C#

I have classes such as AccountsController, ProductsController etc that all inherit from BaseController. Unity sets up my services as needed. These classes also all require a _sequence service. As it is a common requirement for all classes I would like to code this in the 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 set up a call to the BaseController inside the constructors of each of the AccountsController and ProductsController?

like image 439
Samantha J T Star Avatar asked Dec 24 '11 08:12

Samantha J T Star


People also ask

How do I call a super constructor?

To explicitly call the superclass constructor from the subclass constructor, we use super() . It's a special form of the super keyword.

Do you have to call super constructor?

Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error.

Is super () always called?

Super class constructor is always called during construction process and it's guaranteed that super class construction is finished before subclass constructor is called. This is the case for most if not all the object oriented language.

How do you call a base class constructor?

To call the parameterized constructor of base class inside the parameterized constructor of sub class, we have to mention it explicitly. The parameterized constructor of base class cannot be called in default constructor of sub class, it should be called in the parameterized constructor of sub class.


1 Answers

You can chain constructors:

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

Note that the chained BaseController (using the base keyword) has been passed the productService parameter, tough this can be anything.

Update:

You could do the following (poor mans' dependency injection):

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

Or, pass in the dependency on ISequenceService through your inheriting types:

public class ProductsController : BaseController
{
    public ProductsController(
        IService<Account> productService, ISequenceService sequenceService) 
        : base(sequenceService)
    {
        _product = productService;
    }
}
like image 145
Oded Avatar answered Oct 16 '22 10:10

Oded