Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect behavior when call Inject from a child container #306

Open
mcdis opened this issue Jan 17, 2023 · 1 comment
Open

Incorrect behavior when call Inject from a child container #306

mcdis opened this issue Jan 17, 2023 · 1 comment

Comments

@mcdis
Copy link

mcdis commented Jan 17, 2023

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)
  }
}
@Lex45x
Copy link
Contributor

Lex45x commented Aug 14, 2023

Hi @mcdis,

I believe this is 'by-design'. I've checked the implementation of Inject extension method and here is what I've found:

image

Grace will get the top-most scope to perform property injection. So any changes in the child scopes won't take place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants