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

Use TryAdd in AddXxx methods to only add services if not already present. #1182

Merged
merged 1 commit into from
Dec 2, 2014

Conversation

ajcvickers
Copy link
Contributor

Issue #956 and also see pull request #1129.

This change updates all of our AddXxx methods to use TryAdd when adding to the service collection such that only services that are not already registered will be registered. This also means that the code we had to check the service collection for "hosting" services is no longer needed.

Note that services for which it is expected that multiple instances can be registered (e.g. listeners and data sources) must not use TryAdd since otherwise only the first registered will be used.

@ajcvickers
Copy link
Contributor Author

/cc @HaoK

@HaoK
Copy link
Member

HaoK commented Nov 27, 2014

As part of this change, you might want to consider using ServiceDescriber instead of a new ServiceCollection which allows you to pass in the configuration instance which would provide service replacement and align EF with the other frameworks AddXXX methods...

.AddScoped<IRelationshipListener>(p => p.GetService<NavigationFixer>())
.AddScoped<IPropertyListener>(p => p.GetService<ChangeDetector>());

serviceCollection.TryAdd(new ServiceCollection()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here, instead of try adding a new service collection that you fill, just directly TryAdd which gets rid of the nested ServiceCollection which looks a bit odd:

var describer = new ServiceDescriber(Configuration);
servicesCollection.AddScoped(your 3 listeners)
servicesCollection.TryAdd(describer.Singleton<>()) 

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TryAdd can't be chained. It returns bool.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mainly just a nit anyways, but my point was that you can just directly TryAdd the services even without chaining, rather than adding them to a new ServiceCollection and then passing it to the overload of TryAdd that turns around and enumerates the new service collection and adds them one by one...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stylistically, I prefer chaining. It would be nice to chain without the additional collection, but given this isn't possible the collection is the way to use chaining with this API.

@anpete
Copy link
Contributor

anpete commented Dec 2, 2014

:shipit:

…ent.

Issue #956 and also see pull request #1129.

This change updates all of our AddXxx methods to use TryAdd when adding to the service collection such that only services that are not already registered will be registered. This also means that the code we had to check the service collection for "hosting" services is no longer needed.

Note that services for which it is expected that multiple instances can be registered (e.g. listeners and data sources) must not use TryAdd since otherwise only the first registered will be used.
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

Successfully merging this pull request may close these issues.

3 participants