aurelia / framework

The Aurelia 1 framework entry point, bringing together all the required sub-modules of Aurelia.
MIT License
11.75k stars 623 forks source link

Dependency injection and inheritance #703

Open cluka opened 7 years ago

cluka commented 7 years ago

I'm submitting a bug report

Please tell us about your environment:

Current behavior: I don't know if this is a bug or me not doing it right. Let's say I have this scenarion:

export abstract class ViewModelBase{
    model:any;
    constructor(protected service: any){}
    abstract save();
}

import {DataService1} from './dataService1';
@inject(DataService1)
export class ConcreteViewModel1 extends ViewModelBase{
    constructor(dataService: Dataservice1){
        super(dataService);
    }

    save(){
        this.service.saveData(this.model);
    }
}

import {DataService2} from './dataService2';
@inject(DataService2)
export class ConcreteViewModel2 extends ViewModelBase{
    constructor(dataService: Dataservice2){
        super(dataService);
    }

    save(){
        this.service.saveData(this.model);
    }
}

Now if I do this:

  1. Open a composite which view-model is ConcreteViewModel1. it will instantiate the correct view-model ConcreteViewModel1 and inject the correct dependent service DataService1.
  2. Call view-model save() it will call the correct one. then..
  3. Open a composite which view-model is ConcreteViewmodel2. It will again instantiate it correctly.
  4. Call view-model save(), it will call the ConcreteViewmodel2.save(). But..
  5. I again open a composite which view-model is ConcreteViewModel1 and then
  6. Call view-model save() it will throw exception because in the background it's service property is of type DataService2 and it should be DataService1

Expected/desired behavior:

I expect that the injected service is the correct one.

StrahilKazlachev commented 7 years ago

Which version of aurelia-dependency-injection? If it's prior to 1.3.0 could you try with it?

cluka commented 7 years ago

I don't know which version. I don't have it in packages.json as a separate library and I'm on aurelia-framework v.1.0.7., the one that came with the skeleton: typescript-webpack-skeleton. How to upgrade it?