Skip to content

Injection for multiple instances of a class cannot have unique config #662

@kjdelisle

Description

@kjdelisle

Problem

Injection does not work for multiple class instances that need unique config:

class Magician {
  constructor(@inject('equipment') private inventory: {}) {
  }
}

app.bind('equipment).to({
  weapon: superPowerfulArtifact,
});


app.bind('hero').toClass(Magician);
app.bind('villain').toClass(Magician);
// now the villain can access the superPowerfulArtifact! Oh noes!

Acceptance Criteria

Find a way to override the existing injection (if any) on a per-binding level so that different instances can be configured easily, but with injection still at work.

class Magician {
  constructor(@inject('equipment') private inventory: {}) {
  }
}

app.bind('equipment).to({
  weapon: squirtGun,
});

app.bind('hero').toClass(Magician).with(() => {
  equipment: {
    weapon: superPowerfulArtifact,
  }
});
app.bind('villain').toClass(Magician);
// Only the hero gets the superPowerfulArtifact now! Yay!

See #631 for previous discussion on the topic

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions