Skip to content

Commit b77c5b3

Browse files
committed
Clean up IAzureContainerRegistry explicit implementation
1 parent 6d3e2fb commit b77c5b3

File tree

2 files changed

+22
-56
lines changed

2 files changed

+22
-56
lines changed

src/Aspire.Hosting.Azure.AppContainers/AzureContainerAppEnvironmentResource.cs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -165,40 +165,23 @@ await context.ReportingStep.CompleteAsync(
165165

166166
internal Dictionary<string, (IResource resource, ContainerMountAnnotation volume, int index, BicepOutputReference outputReference)> VolumeNames { get; } = [];
167167

168-
private IContainerRegistry? GetAssociatedRegistry()
168+
private IAzureContainerRegistry GetAssociatedRegistry()
169169
{
170-
return this.TryGetLastAnnotation<ContainerRegistryReferenceAnnotation>(out var annotation)
171-
? annotation.Registry
172-
: null;
173-
}
174-
175-
// Implement IAzureContainerRegistry interface by delegating to the associated registry
176-
ReferenceExpression IContainerRegistry.Name
177-
{
178-
get
170+
if (this.TryGetLastAnnotation<ContainerRegistryReferenceAnnotation>(out var annotation) &&
171+
annotation.Registry is IAzureContainerRegistry registry)
179172
{
180-
var registry = GetAssociatedRegistry();
181-
return registry?.Name ?? throw new InvalidOperationException($"No container registry associated with environment '{Name}'");
173+
return registry;
182174
}
183-
}
184175

185-
ReferenceExpression IContainerRegistry.Endpoint
186-
{
187-
get
188-
{
189-
var registry = GetAssociatedRegistry();
190-
return registry?.Endpoint ?? throw new InvalidOperationException($"No container registry associated with environment '{Name}'");
191-
}
176+
throw new InvalidOperationException($"No Azure container registry associated with environment '{Name}'");
192177
}
193178

194-
ReferenceExpression IAzureContainerRegistry.ManagedIdentityId
195-
{
196-
get
197-
{
198-
var registry = GetAssociatedRegistry() as IAzureContainerRegistry;
199-
return registry?.ManagedIdentityId ?? throw new InvalidOperationException($"No Azure container registry associated with environment '{Name}'");
200-
}
201-
}
179+
// Implement IAzureContainerRegistry interface by delegating to the associated registry
180+
ReferenceExpression IContainerRegistry.Name => GetAssociatedRegistry().Name;
181+
182+
ReferenceExpression IContainerRegistry.Endpoint => GetAssociatedRegistry().Endpoint;
183+
184+
ReferenceExpression IAzureContainerRegistry.ManagedIdentityId => GetAssociatedRegistry().ManagedIdentityId;
202185

203186
ReferenceExpression IComputeEnvironmentResource.GetHostAddressExpression(EndpointReference endpointReference)
204187
{

src/Aspire.Hosting.Azure.AppService/AzureAppServiceEnvironmentResource.cs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -203,40 +203,23 @@ await context.ReportingStep.CompleteAsync(
203203
internal static BicepValue<string> GetWebSiteSuffixBicep() =>
204204
BicepFunction.GetUniqueString(BicepFunction.GetResourceGroup().Id);
205205

206-
private IContainerRegistry? GetAssociatedRegistry()
206+
private IAzureContainerRegistry GetAssociatedRegistry()
207207
{
208-
return this.TryGetLastAnnotation<ContainerRegistryReferenceAnnotation>(out var annotation)
209-
? annotation.Registry
210-
: null;
211-
}
212-
213-
// Implement IAzureContainerRegistry interface by delegating to the associated registry
214-
ReferenceExpression IAzureContainerRegistry.ManagedIdentityId
215-
{
216-
get
208+
if (this.TryGetLastAnnotation<ContainerRegistryReferenceAnnotation>(out var annotation) &&
209+
annotation.Registry is IAzureContainerRegistry registry)
217210
{
218-
var registry = GetAssociatedRegistry() as IAzureContainerRegistry;
219-
return registry?.ManagedIdentityId ?? throw new InvalidOperationException($"No Azure container registry associated with environment '{Name}'");
211+
return registry;
220212
}
221-
}
222213

223-
ReferenceExpression IContainerRegistry.Name
224-
{
225-
get
226-
{
227-
var registry = GetAssociatedRegistry();
228-
return registry?.Name ?? throw new InvalidOperationException($"No container registry associated with environment '{Name}'");
229-
}
214+
throw new InvalidOperationException($"No Azure container registry associated with environment '{Name}'");
230215
}
231216

232-
ReferenceExpression IContainerRegistry.Endpoint
233-
{
234-
get
235-
{
236-
var registry = GetAssociatedRegistry();
237-
return registry?.Endpoint ?? throw new InvalidOperationException($"No container registry associated with environment '{Name}'");
238-
}
239-
}
217+
// Implement IAzureContainerRegistry interface by delegating to the associated registry
218+
ReferenceExpression IAzureContainerRegistry.ManagedIdentityId => GetAssociatedRegistry().ManagedIdentityId;
219+
220+
ReferenceExpression IContainerRegistry.Name => GetAssociatedRegistry().Name;
221+
222+
ReferenceExpression IContainerRegistry.Endpoint => GetAssociatedRegistry().Endpoint;
240223

241224
ReferenceExpression IComputeEnvironmentResource.GetHostAddressExpression(EndpointReference endpointReference)
242225
{

0 commit comments

Comments
 (0)