Skip to content

Commit e40495f

Browse files
Chris Martinezcommonsensesoftware
Chris Martinez
authored andcommitted
Refactor to make registering model configurations public
1 parent 88d2cd4 commit e40495f

File tree

3 files changed

+79
-40
lines changed

3 files changed

+79
-40
lines changed

src/AspNetCore/OData/src/Asp.Versioning.OData/DependencyInjection/IApiVersioningBuilderExtensions.cs

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ namespace Microsoft.Extensions.DependencyInjection;
99
using Microsoft.AspNetCore.Http;
1010
using Microsoft.AspNetCore.Http.Features;
1111
using Microsoft.AspNetCore.Mvc.ApplicationModels;
12-
using Microsoft.AspNetCore.Mvc.ApplicationParts;
1312
using Microsoft.AspNetCore.OData;
1413
using Microsoft.AspNetCore.OData.Routing.Template;
1514
using Microsoft.AspNetCore.Routing;
@@ -66,8 +65,7 @@ private static void AddServices( IServiceCollection services )
6665
services.TryRemoveODataService( typeof( IApplicationModelProvider ), ODataRoutingApplicationModelProviderType );
6766

6867
var partManager = services.GetOrCreateApplicationPartManager();
69-
70-
ConfigureDefaultFeatureProviders( partManager );
68+
var configured = partManager.ConfigureDefaultFeatureProviders();
7169

7270
services.AddHttpContextAccessor();
7371
services.TryAddSingleton<VersionedODataOptions>();
@@ -84,24 +82,11 @@ private static void AddServices( IServiceCollection services )
8482
services.TryAddEnumerable( Singleton<MatcherPolicy, DefaultMetadataMatcherPolicy>() );
8583
services.TryAddEnumerable( Transient<IApplicationModelProvider, ODataApplicationModelProvider>() );
8684
services.TryAddEnumerable( Transient<IApplicationModelProvider, ODataMultiModelApplicationModelProvider>() );
87-
services.AddModelConfigurationsAsServices( partManager );
88-
}
89-
90-
private static T GetService<T>( this IServiceCollection services ) =>
91-
(T) services.LastOrDefault( d => d.ServiceType == typeof( T ) )?.ImplementationInstance!;
92-
93-
private static ApplicationPartManager GetOrCreateApplicationPartManager( this IServiceCollection services )
94-
{
95-
var partManager = services.GetService<ApplicationPartManager>();
9685

97-
if ( partManager == null )
86+
if ( configured )
9887
{
99-
partManager = new ApplicationPartManager();
100-
services.TryAddSingleton( partManager );
88+
services.AddModelConfigurationsAsServices( partManager );
10189
}
102-
103-
partManager.ApplicationParts.Add( new AssemblyPart( typeof( ODataApiVersioningOptions ).Assembly ) );
104-
return partManager;
10590
}
10691

10792
[MethodImpl( MethodImplOptions.AggressiveInlining )]
@@ -148,27 +133,6 @@ private static void TryReplaceODataService(
148133
}
149134
}
150135

151-
private static void AddModelConfigurationsAsServices( this IServiceCollection services, ApplicationPartManager partManager )
152-
{
153-
var feature = new ModelConfigurationFeature();
154-
var modelConfigurationType = typeof( IModelConfiguration );
155-
156-
partManager.PopulateFeature( feature );
157-
158-
foreach ( var modelConfiguration in feature.ModelConfigurations )
159-
{
160-
services.TryAddEnumerable( Transient( modelConfigurationType, modelConfiguration ) );
161-
}
162-
}
163-
164-
private static void ConfigureDefaultFeatureProviders( ApplicationPartManager partManager )
165-
{
166-
if ( !partManager.FeatureProviders.OfType<ModelConfigurationFeatureProvider>().Any() )
167-
{
168-
partManager.FeatureProviders.Add( new ModelConfigurationFeatureProvider() );
169-
}
170-
}
171-
172136
private static object CreateInstance( this IServiceProvider services, ServiceDescriptor descriptor )
173137
{
174138
if ( descriptor.ImplementationInstance != null )
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
3+
namespace Microsoft.Extensions.DependencyInjection;
4+
5+
using Asp.Versioning.OData;
6+
using Microsoft.AspNetCore.Mvc.ApplicationParts;
7+
using Microsoft.Extensions.DependencyInjection.Extensions;
8+
using System.Runtime.CompilerServices;
9+
using static Microsoft.Extensions.DependencyInjection.ServiceDescriptor;
10+
11+
/// <summary>
12+
/// Provides extension methods for <see cref="IServiceCollection"/>.
13+
/// </summary>
14+
public static class IServiceCollectionExtensions
15+
{
16+
[MethodImpl( MethodImplOptions.AggressiveInlining )]
17+
internal static T GetService<T>( this IServiceCollection services ) =>
18+
(T) services.LastOrDefault( d => d.ServiceType == typeof( T ) )?.ImplementationInstance!;
19+
20+
internal static ApplicationPartManager GetOrCreateApplicationPartManager( this IServiceCollection services )
21+
{
22+
var partManager = services.GetService<ApplicationPartManager>();
23+
24+
if ( partManager == null )
25+
{
26+
partManager = new ApplicationPartManager();
27+
services.TryAddSingleton( partManager );
28+
}
29+
30+
partManager.ApplicationParts.Add( new AssemblyPart( typeof( ODataApiVersioningOptions ).Assembly ) );
31+
return partManager;
32+
}
33+
34+
internal static void AddModelConfigurationsAsServices( this IServiceCollection services, ApplicationPartManager partManager )
35+
{
36+
var feature = new ModelConfigurationFeature();
37+
var modelConfigurationType = typeof( IModelConfiguration );
38+
39+
partManager.PopulateFeature( feature );
40+
41+
foreach ( var modelConfiguration in feature.ModelConfigurations )
42+
{
43+
services.TryAddEnumerable( Transient( modelConfigurationType, modelConfiguration ) );
44+
}
45+
}
46+
47+
internal static bool ConfigureDefaultFeatureProviders( this ApplicationPartManager partManager )
48+
{
49+
if ( partManager.FeatureProviders.OfType<ModelConfigurationFeatureProvider>().Any() )
50+
{
51+
return false;
52+
}
53+
54+
partManager.FeatureProviders.Add( new ModelConfigurationFeatureProvider() );
55+
return true;
56+
}
57+
58+
/// <summary>
59+
/// Registers discovered <see cref="IModelConfiguration">model configurations</see> as services in the <see cref="IServiceCollection"/>.
60+
/// </summary>
61+
/// <param name="services">The extended <see cref="IServiceCollection"/>.</param>
62+
public static void AddModelConfigurationsAsServices( this IServiceCollection services )
63+
{
64+
if ( services == null )
65+
{
66+
throw new ArgumentNullException( nameof( services ) );
67+
}
68+
69+
var partManager = services.GetOrCreateApplicationPartManager();
70+
71+
if ( ConfigureDefaultFeatureProviders( partManager ) )
72+
{
73+
services.AddModelConfigurationsAsServices( partManager );
74+
}
75+
}
76+
}

src/AspNetCore/OData/src/Asp.Versioning.OData/OData/ODataMultiModelApplicationModelProvider.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Asp.Versioning.OData;
66

7-
using Microsoft.AspNetCore.Http;
87
using Microsoft.AspNetCore.Mvc.ApplicationModels;
98
using Microsoft.AspNetCore.OData;
109
using Microsoft.AspNetCore.OData.Routing.Conventions;

0 commit comments

Comments
 (0)