injectable icon indicating copy to clipboard operation
injectable copied to clipboard

bug: Order annotation doesn't move dependent instances

Open jamontes79 opened this issue 1 year ago • 5 comments

If I have the following code

@singleton
@Order(-1)
class InitFeatureTogglesUseCase {
  InitFeatureTogglesUseCase(
    this._buildConfig,
    this._hiddenSettingsRepository,
    this._featureToggleRepository,
  );

The generated code is the following:

gh.singleton<_i3.InitFeatureTogglesUseCase>(_i3.InitFeatureTogglesUseCase(
      gh<_i4.BuildConfig>(),
      gh<_i5.HiddenSettingsRepository>(),
      gh<_i6.FeatureToggleRepository>(),
    )

But the instances of BuildConfig, HiddenSettingsRepository and FeatureToggleRepository are not instantiated because they have no order.

Any way of doing this without reordering manually all the instances?

Thanks in advance

jamontes79 avatar May 02 '24 15:05 jamontes79

Same problem. Did you find any solution? My code is heavily based on this structure

vicenterusso avatar May 06 '24 17:05 vicenterusso

Same problem. Did you find any solution? My code is heavily based on this structure

I've made a workaround using scopes and grouped some specific classes.

It's not the best approach, but it work for us.

jamontes79 avatar May 07 '24 09:05 jamontes79

same there, solution with scopes is the best but i found something pricky

@singleton
Future<FooInterface> bar(FooRepository repository, IJohn john, IDoe doe) async {
...
// returns implementation of FooInterface
}

and the point, that I do not use IJohn john and IDoe doe fields in this singleton, but they init first. And i need such order of init

equescodebelike avatar Dec 26 '24 15:12 equescodebelike

@jamontes79, late reply, but nevertheless Swapping annotations seems to fix the problem

@Order(-1)
@singleton
class InitFeatureTogglesUseCase {
  InitFeatureTogglesUseCase(
    this._buildConfig,
    this._hiddenSettingsRepository,
    this._featureToggleRepository,
  );

allthough you wiil need to register objects you use in constructor before this.

  @Order(-2)
  BuildConfig get config => <...>,
  @Order(-2)
  HiddenSettingsRepository get repo1 => <...>,
  @Order(-2)
  FeatureToggleRepository get repo2 => <...>,

LordOfTheApples123 avatar Dec 26 '24 17:12 LordOfTheApples123

same there, solution with scopes is the best but i found something pricky

@singleton
Future<FooInterface> bar(FooRepository repository, IJohn john, IDoe doe) async {
...
// returns implementation of FooInterface
}

and the point, that I do not use IJohn john and IDoe doe fields in this singleton, but they init first. And i need such order of init

I do not recommend to use my solution, instead try using scopes

equescodebelike avatar Apr 01 '25 20:04 equescodebelike