Samantha J T Star
Samantha J T Star

Reputation: 32808

Does a derived class always have to call the default base constructor?

I have the following:

    public class BaseController : Controller
    {
        protected ISequenceService _sequence;

        public BaseController()
        {
        }

        [InjectionConstructor]
        public BaseController(ISequenceService sequence)
        {
            _sequence = sequence;
        }


 public class ProductsController : BaseController
    {

        public ProductsController(
            IService<Account> accountService,
            IService<Product> productService
            ) {
            _account = accountService;
            _product = productService;   
        }

I have been trying everything I can think of to get the BaseController one parameter constructor parameter called. However the parameterless constructor is always called. When I remove the parameterless constructor I get an error.

Is it possible to have a derived class and no parameterless constructor in the parent ? Is there some way that I can configure Unity to call the one parameter constructor?

Upvotes: 0

Views: 2395

Answers (3)

Dominic Savio M
Dominic Savio M

Reputation: 9

you can even have just the parameter less constructor and use Method Injection for parameters

public abstract class Base : IBase
>     {
> 
>         private IDep dep;
> 
>         [InjectionMethod]
>         public void Initialize(IDep dep)
>         {
>             if (dep == null) throw new ArgumentNullException("dep");
>             this.dep = dep;
> 
>             OnInitialize();
>         }
> 
>         public dep DepProperty
>         {
>             get
>             {
>                 return dep;
>             }
>         }
>         protected abstract void OnInitialize();
>     }

//now your Derived class Constructor will not be forced to have the IDep Parameter 
class Derived : Base
{
    public Derived()
    {

    }

    protected override void OnInitialize()
    {
        // you can access the baseclass dependency Instance in this override
        object depObject = this.DepProperty;
    }
}

Upvotes: -1

CodingWithSpike
CodingWithSpike

Reputation: 43728

This actually has nothing to do with Unity. You would experience the same behavior if you simply did:

var prodCont = new ProductsController(accountService, productsService);

This would call the ProductsController 2-parameter constructor, and call the BaseController 0-parameter default constructor.

What you would need to do is tell the ProductsController constructor to in turn call the 1-parameter base contructor instead of the default one, by doing:

public ProductsController(IService<Account> accountService, IService<Product> productService)
    : base(sequenceService)

But there is no ISequenceService being passed to that constructor to forward on to the base constructor, so you probably actually want:

public ProductsController(IService<Account> accountService,
                          IService<Product> productService,
                          ISequenceService sequenceService)
    : base(sequenceService)

Assuming ISequenceService is also mapped through Unity.

Upvotes: 0

Jeff Mercado
Jeff Mercado

Reputation: 134891

You must call a constructor of the base class. Most of the time, that means calling the default constructor and if you leave it out, it will be done for you implicitly. You can specify which constructor to call using the following syntax:

public ProductsController(IService<Account> accountService,
                          IService<Product> productService)
    : base((ISequenceService)someISequenceService) // calls the one parameter constructor
                                                   // of the base class
{
    //...
}

Trying to construct a derived class without initializing the base class first is just not going to work.

Upvotes: 4

Related Questions