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

No way to get scopes tags list for MatchingScopeLifetime #730

Closed
AqlaSolutions opened this issue Mar 31, 2016 · 4 comments
Closed

No way to get scopes tags list for MatchingScopeLifetime #730

AqlaSolutions opened this issue Mar 31, 2016 · 4 comments

Comments

@AqlaSolutions
Copy link

I want to be able to get scopes tags from InstancePerMatchingLifetimeScope when iterating through ComponentRegistry.Registrations. E.g. I want to activate everything registered for Application scope but I can't access scopes for components.

Please add this.

@AqlaSolutions AqlaSolutions changed the title No way to get scopes tags list for InstancePerMatchingLifetimeScope service No way to get scopes tags list for MatchingScopeLifetime Mar 31, 2016
@tillig
Copy link
Member

tillig commented Mar 31, 2016

Will have to think about this one. Keep in mind there are things like registration sources that dynamically generate registrations. I worry about solutions or features that rely on iterating through the registrations collection.

On the other hand, I could potentially see something where you may query about information for a specific registration. Hmmm.

@tillig
Copy link
Member

tillig commented Mar 31, 2016

The initial stated desire here seems to overlap with #731 and #567.

@tillig
Copy link
Member

tillig commented Jun 28, 2016

Reading over these now that we've come up for air a bit from the .NET Core RTM, I think if we can address #567 - the ability for AutoActivate/IStartable to work in nested lifetime scopes - that may be the best we can do. We don't actually figure out what the InstancePerMatchingLifetimeScope tags are for things until the component resolves, and even then it's only used to determine which scope owns the component.

Saying something like, "auto-activate everything that was registered just for this matching scope at the time the scope is created" is sort of edge case from an IoC perspective.

One thing you could do to work around the issue is to use events and metadata.

First, when registering things, add metadata to the things you want to auto-activate.

builder.RegisterType<MyComponent>()
  .As<IService>()
  .InstancePerMatchingLifetimeScope("child")
  .WithMetadata("scope", "child");

Then attach to the container ChildLifetimeScopeBeginning event and resolve/activate the things you are interested in:

private static void Activate(IEnumerable<Meta<Lazy<IService>>> services)
{
  foreach(var meta in services)
  {
    if(meta["scope"].Equals("child"))
    {
      // The value of Meta<Lazy<T>> is Lazy<T>
      // The value of Lazy<T> is the resolved instance
      meta.Value.Value.DoActivate();
    }
  }
}

var container = builder.Build();

// Handle the child scope beginning event to activate everything.
container.ChildLifetimeScopeBeginning += (sender, args) =>
  Activate(args.LifetimeScope.Resolve<IEnumerable<Meta<Lazy<IService>>>>());

using(var scope = container.BeginLifetimeScope("child"))
{
  // The named lifetime scope will kick off the event handler.
}

(Note I wrote that stuff off the top of my head. You may have to tweak it a bit, add error handling, etc.)

Working around it with events seems like more effort, but it means a couple of things:

  1. You can do everything you need to right now with stuff that's already supported.
  2. It'll work perfectly with registration sources and other dynamically registered things as long as they also include the appropriate metadata.

Something we could add, though, would be a "well known" metadata item on every registration that's registered using InstancePerMatchingLifetimeScope to include the set of tags. Then you wouldn't have to add the metadata yourself and would only have to add the event handler.

@tillig
Copy link
Member

tillig commented Apr 25, 2017

I added an extension method on IComponentRegistration to allow retrieval of matching lifetime scope tags for those components that have them. It'll be an empty enumeration if it's a singleton or otherwise doesn't have matching scope tags.

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

No branches or pull requests

2 participants