# \[Aurelia 2\] Dependency injection into a base class

**URL:** <https://discourse.aurelia.io/t/aurelia-2-dependency-injection-into-a-base-class/6358>\
**Category:** Help Requests\
**Created:** [February 25, 2025, 10:16am UTC](https://discourse.aurelia.io/t/aurelia-2-dependency-injection-into-a-base-class/6358 "2025-02-25T10:16:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![graycrow](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/graycrow/32/323_2.png) [@graycrow](https://discourse.aurelia.io/u/graycrow)\
**Post date:** [February 25, 2025, 10:16am UTC](https://discourse.aurelia.io/t/aurelia-2-dependency-injection-into-a-base-class/6358/1 "2025-02-25T10:16:17Z")

</div>

As I understand it, dependency injection into a base class using the `...rest` constructor parameters is dead in Aurelia 2, right? It’s a huge pain, now I have to explicitly inject all those dependencies into each derived class separately, resulting in many duplicated lines of code and wasted time. Is this the only way to go in Au 2 or is there some trick I don’t know about?

---

<div class="post-metadata">

**Author:** ![graycrow](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/graycrow/32/323_2.png) [@graycrow](https://discourse.aurelia.io/u/graycrow)\
**Post date:** [February 25, 2025, 11:34am UTC](https://discourse.aurelia.io/t/aurelia-2-dependency-injection-into-a-base-class/6358/2 "2025-02-25T11:34:41Z")

</div>

Ok, I found something that might be the easiest solution, not sure if it is correct because although this particular type of error is gone now, the app is still broken because of another migration issue and I don’t know if this is the final solution to this problem.

The solution  
Use `static inject` variable in the base class, no `@inject` decorator:

```auto
export abstract class BaseClass {
    public static readonly inject: any[] = [
        IBaseDependency1,
        IBaseDependency2,
    ];

    constructor(
        protected bd1?: IBaseDependency1,
        public bd2?:IBaseDependency2,
    ) { }

```

Again, use `static inject` variable in the derived class, no `@inject` decorator here as well:

```auto
export class DerivedClass extends BaseClass {
    public static readonly inject = [IDerivedDependency, ...BaseClass.inject];

    constructor(
        private dd:IDerivedDependency,
        ...rest: any[]
    ) {
        super(...rest);
    }

```

The trick is to use `...BaseClass.inject` when listing the required dependencies, the rest (literally, the `...rest` 🙂 ) is the same as in Au 1.

---

<div class="post-metadata">

**Author:** ![m-gallesio](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/m-gallesio/32/1288_2.png) [@m-gallesio](https://discourse.aurelia.io/u/m-gallesio)\
**Post date:** [February 27, 2025, 9:31am UTC](https://discourse.aurelia.io/t/aurelia-2-dependency-injection-into-a-base-class/6358/3 "2025-02-27T09:31:42Z")

</div>

I would consider [property injection](https://docs.aurelia.io/getting-to-know-aurelia/dependency-injection#property-injection); having a project which make heavy use of inheritance it greatly simplified our usage of constructors.

---

<div class="post-metadata">

**Author:** ![elitemike](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/elitemike/32/690_2.png) [@elitemike](https://discourse.aurelia.io/u/elitemike)\
**Post date:** [February 28, 2025, 1:53pm UTC](https://discourse.aurelia.io/t/aurelia-2-dependency-injection-into-a-base-class/6358/4 "2025-02-28T13:53:21Z")

</div>

This exactly. At first, I was annoyed at the change as well, but it’s nice to have keep the derived class constructers cleaner.
