Skip to content

Commit 207ffce

Browse files
Chris Martinezcommonsensesoftware
authored andcommitted
Refactor to make registering model configurations public
1 parent 64ac132 commit 207ffce

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;
@@ -69,8 +68,7 @@ private static void AddServices( IServiceCollection services )
6968
services.TryRemoveODataService( typeof( IApplicationModelProvider ), ODataRoutingApplicationModelProviderType );
7069

7170
var partManager = services.GetOrCreateApplicationPartManager();
72-
73-
ConfigureDefaultFeatureProviders( partManager );
71+
var configured = partManager.ConfigureDefaultFeatureProviders();
7472

7573
services.AddHttpContextAccessor();
7674
services.TryAddSingleton<VersionedODataOptions>();
@@ -87,24 +85,11 @@ private static void AddServices( IServiceCollection services )
8785
services.TryAddEnumerable( Singleton<MatcherPolicy, DefaultMetadataMatcherPolicy>() );
8886
services.TryAddEnumerable( Transient<IApplicationModelProvider, ODataApplicationModelProvider>() );
8987
services.TryAddEnumerable( Transient<IApplicationModelProvider, ODataMultiModelApplicationModelProvider>() );
90-
services.AddModelConfigurationsAsServices( partManager );
91-
}
92-
93-
private static T GetService<T>( this IServiceCollection services ) =>
94-
(T) services.LastOrDefault( d => d.ServiceType == typeof( T ) )?.ImplementationInstance!;
95-
96-
private static ApplicationPartManager GetOrCreateApplicationPartManager( this IServiceCollection services )
97-
{
98-
var partManager = services.GetService<ApplicationPartManager>();
9988

100-
if ( partManager == null )
89+
if ( configured )
10190
{
102-
partManager = new ApplicationPartManager();
103-
services.TryAddSingleton( partManager );
91+
services.AddModelConfigurationsAsServices( partManager );
10492
}
105-
106-
partManager.ApplicationParts.Add( new AssemblyPart( typeof( ODataApiVersioningOptions ).Assembly ) );
107-
return partManager;
10893
}
10994

11095
[MethodImpl( MethodImplOptions.AggressiveInlining )]
@@ -151,27 +136,6 @@ private static void TryReplaceODataService(
151136
}
152137
}
153138

154-
private static void AddModelConfigurationsAsServices( this IServiceCollection services, ApplicationPartManager partManager )
155-
{
156-
var feature = new ModelConfigurationFeature();
157-
var modelConfigurationType = typeof( IModelConfiguration );
158-
159-
partManager.PopulateFeature( feature );
160-
161-
foreach ( var modelConfiguration in feature.ModelConfigurations )
162-
{
163-
services.TryAddEnumerable( Transient( modelConfigurationType, modelConfiguration ) );
164-
}
165-
}
166-
167-
private static void ConfigureDefaultFeatureProviders( ApplicationPartManager partManager )
168-
{
169-
if ( !partManager.FeatureProviders.OfType<ModelConfigurationFeatureProvider>().Any() )
170-
{
171-
partManager.FeatureProviders.Add( new ModelConfigurationFeatureProvider() );
172-
}
173-
}
174-
175139
private static object CreateInstance( this IServiceProvider services, ServiceDescriptor descriptor )
176140
{
177141
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)