-
Notifications
You must be signed in to change notification settings - Fork 703
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to use a default internal container and dependency resolver …
…instead of static singletons
- Loading branch information
1 parent
49cdc8d
commit d0b4fd5
Showing
20 changed files
with
226 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/AspNet/WebApi/src/Asp.Versioning.WebApi/Dependencies/DefaultContainer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
|
||
namespace Asp.Versioning.Dependencies; | ||
|
||
using Asp.Versioning; | ||
using Asp.Versioning.Conventions; | ||
using System.ComponentModel.Design; | ||
using System.Web.Http.Dependencies; | ||
|
||
internal sealed class DefaultContainer : IDependencyResolver, IDependencyScope | ||
{ | ||
private readonly ServiceContainer container = new(); | ||
private bool disposed; | ||
|
||
internal DefaultContainer() | ||
{ | ||
container.AddService( typeof( ApiVersioningOptions ), static ( sc, t ) => new ApiVersioningOptions() ); | ||
container.AddService( typeof( IApiVersionParser ), static ( sc, t ) => ApiVersionParser.Default ); | ||
container.AddService( typeof( IControllerNameConvention ), static ( sc, t ) => ControllerNameConvention.Default ); | ||
container.AddService( typeof( IProblemDetailsFactory ), static ( sc, t ) => new ProblemDetailsFactory() ); | ||
container.AddService( typeof( ISunsetPolicyManager ), NewSunsetPolicyManager ); | ||
container.AddService( typeof( IReportApiVersions ), NewApiVersionReporter ); | ||
} | ||
|
||
public ApiVersioningOptions ApiVersioningOptions | ||
{ | ||
get => GetApiVersioningOptions( container ); | ||
set | ||
{ | ||
container.RemoveService( typeof( ApiVersioningOptions ) ); | ||
container.AddService( typeof( ApiVersioningOptions ), value ); | ||
} | ||
} | ||
|
||
public void Replace( Type serviceType, ServiceCreatorCallback activator ) | ||
{ | ||
container.RemoveService( serviceType ); | ||
container.AddService( serviceType, activator ); | ||
} | ||
|
||
public IDependencyScope BeginScope() => this; | ||
|
||
public void Dispose() | ||
{ | ||
if ( disposed ) | ||
{ | ||
return; | ||
} | ||
|
||
disposed = true; | ||
container.Dispose(); | ||
} | ||
|
||
public object GetService( Type serviceType ) => container.GetService( serviceType ); | ||
|
||
public IEnumerable<object> GetServices( Type serviceType ) | ||
{ | ||
var service = container.GetService( serviceType ); | ||
|
||
if ( service is not null ) | ||
{ | ||
yield return service; | ||
} | ||
} | ||
|
||
private static ApiVersioningOptions GetApiVersioningOptions( IServiceProvider serviceProvider ) => | ||
(ApiVersioningOptions) serviceProvider.GetService( typeof( ApiVersioningOptions ) ); | ||
|
||
private static ISunsetPolicyManager NewSunsetPolicyManager( IServiceProvider serviceProvider, Type type ) => | ||
new SunsetPolicyManager( GetApiVersioningOptions( serviceProvider ) ); | ||
|
||
private static IReportApiVersions NewApiVersionReporter( IServiceProvider serviceProvider, Type type ) | ||
{ | ||
var options = GetApiVersioningOptions( serviceProvider ); | ||
|
||
if ( options.ReportApiVersions ) | ||
{ | ||
var sunsetPolicyManager = (ISunsetPolicyManager) serviceProvider.GetService( typeof( ISunsetPolicyManager ) ); | ||
return new DefaultApiVersionReporter( sunsetPolicyManager ); | ||
} | ||
|
||
return new DoNotReportApiVersions(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/AspNet/WebApi/src/Asp.Versioning.WebApi/Dependencies/DependencyResolverChain.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
|
||
namespace Asp.Versioning.Dependencies; | ||
|
||
using System.Web.Http; | ||
using System.Web.Http.Dependencies; | ||
|
||
internal sealed class DependencyResolverChain : IDependencyResolver | ||
{ | ||
private readonly HttpConfiguration configuration; | ||
private readonly IDependencyResolver resolver; | ||
|
||
internal DependencyResolverChain( HttpConfiguration configuration ) | ||
{ | ||
this.configuration = configuration; | ||
resolver = configuration.DependencyResolver; | ||
} | ||
|
||
public IDependencyScope BeginScope() => | ||
new DependencyScopeChain( configuration, resolver.BeginScope() ); | ||
|
||
public void Dispose() => resolver.Dispose(); | ||
|
||
public object GetService( Type serviceType ) => | ||
resolver.GetService( serviceType ) ?? | ||
configuration.ApiVersioningServices().GetService( serviceType ); | ||
|
||
public IEnumerable<object> GetServices( Type serviceType ) => resolver.GetServices( serviceType ); | ||
|
||
private sealed class DependencyScopeChain : IDependencyScope | ||
{ | ||
private readonly IDependencyScope scope; | ||
private readonly HttpConfiguration configuration; | ||
|
||
internal DependencyScopeChain( HttpConfiguration configuration, IDependencyScope scope ) | ||
{ | ||
this.configuration = configuration; | ||
this.scope = scope; | ||
} | ||
|
||
public void Dispose() => scope.Dispose(); | ||
|
||
public object GetService( Type serviceType ) => | ||
scope.GetService( serviceType ) ?? | ||
configuration.ApiVersioningServices().GetService( serviceType ); | ||
|
||
public IEnumerable<object> GetServices( Type serviceType ) => scope.GetServices( serviceType ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.