Skip to content

Incorrect behavior when call Inject from a child container #306

@mcdis

Description

@mcdis
public class Log : ILog
{
  public Log(string _scope)
  {
    Scope = _scope;
  }
  public string Scope { get; }
  public ILog this[string _subScope] => new Log($"{Scope}/{_subScope}");
  public string? Trace(string? _message)
  {
    if (_message is not null)
      Android.Util.Log.Error(Scope, _message);
    return _message;
  }
}

Main app:

  Scope = new DependencyInjectionContainer(_ =>
    {
      _.Behaviors.ConstructorSelection = ConstructorSelectionMethod.Dynamic;
      _.Behaviors.AllowInjectionScopeLocation = true;
    });

    Scope.Configure(_ => _.ExportInstance<ILog>(new Log("$")));

In activity:

public abstract class ScopedActivity : Activity
{
  [Import]
  public ILog Log { get; init; }
  
  protected override void OnCreate(Bundle? savedInstanceState)
  {
    var appScope = (Application as IScopedApplication)!.Scope;
    var childScope = appScope.CreateChildScope(_ =>
    {
      _.ExportFactory<ILog>(() =>
      {
        var appLog = appScope.Locate<ILog>();
        
        return appLog["activitities"][Class.SimpleName];
      });
    });
    var log = childScope.Locate<ILog>();
    childScope.Inject(this);   /// this.Log is different than log  (check scope)
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions