Skip to content

Add TryGetByName #16388

@JamesNK

Description

@JamesNK

It's very common to look up resources like this:

var dashboardResource = model.Resources.SingleOrDefault(r => StringComparers.ResourceName.Equals(r.Name, KnownResourceNames.AspireDashboard));

This is verbose, error prone, creates duplication, and could have performance issues.

It could be fixed by adding a TryGetByName method to IResourceCollection. It would be a DIM that by default uses the enumerator to get the resource name. But ResourceCollection overrides it to get the resource from a private Dictionary<string, IResource> that is kept in sync with _resources`.

Existing code that loops over the resources collection to get a resource by name can use the new method.

Metadata

Metadata

Labels

area-app-modelIssues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions