This repository was archived by the owner on Jan 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor/remove extra builders (#314)
* lined with convention breaking changes * Moved extensions around * Updated conventions Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
7406443
commit d52dfb7
Showing
20 changed files
with
287 additions
and
856 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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
namespace Rocket.Surgery.Conventions.Autofac | ||
using Autofac; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Rocket.Surgery.Conventions.Autofac | ||
{ | ||
/// <summary> | ||
/// Delegate ServiceConventionAction | ||
/// </summary> | ||
/// <param name="context">The context.</param> | ||
public delegate void AutofacConventionDelegate(IAutofacConventionContext context); | ||
/// <param name="conventionContext"></param> | ||
/// <param name="configuration"></param> | ||
/// <param name="services"></param> | ||
/// <param name="container"></param> | ||
public delegate void AutofacConvention(IConventionContext conventionContext, IConfiguration configuration, IServiceCollection services, ContainerBuilder container); | ||
} |
44 changes: 44 additions & 0 deletions
44
src/Conventions.Autofac/AutofacConventionServiceProviderFactory.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,44 @@ | ||
using Autofac; | ||
using Autofac.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
|
||
namespace Rocket.Surgery.Conventions.Autofac | ||
{ | ||
class AutofacConventionServiceProviderFactory : IServiceProviderFactory<ContainerBuilder> | ||
{ | ||
private readonly IConventionContext _conventionContext; | ||
private readonly ContainerBuilder _container; | ||
|
||
public AutofacConventionServiceProviderFactory(IConventionContext conventionContext, ContainerBuilder? container = null) | ||
{ | ||
_conventionContext = conventionContext; | ||
_container = container ?? new ContainerBuilder(); | ||
} | ||
|
||
public ContainerBuilder CreateBuilder(IServiceCollection services) | ||
{ | ||
var container = _container; | ||
|
||
var configuration = _conventionContext.Get<IConfiguration>() ?? throw new ArgumentException("Configuration was not found in context"); | ||
foreach (var item in _conventionContext.Conventions.Get<IAutofacConvention, AutofacConvention>()) | ||
{ | ||
if (item.Convention is IAutofacConvention convention) | ||
{ | ||
convention.Register(_conventionContext, configuration, services, _container); | ||
} | ||
else if (item.Delegate is AutofacConvention @delegate) | ||
{ | ||
@delegate(_conventionContext, configuration, services, _container); | ||
} | ||
} | ||
|
||
container.Populate(services); | ||
|
||
return container; | ||
} | ||
|
||
public IServiceProvider CreateServiceProvider(ContainerBuilder containerBuilder) => containerBuilder.Build().Resolve<IServiceProvider>(); | ||
} | ||
} |
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,88 @@ | ||
using System; | ||
using Autofac; | ||
using JetBrains.Annotations; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Rocket.Surgery.Conventions.Autofac; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Rocket.Surgery.Conventions | ||
{ | ||
/// <summary> | ||
/// Class AutofacRocketHostExtensions. | ||
/// </summary> | ||
[PublicAPI] | ||
public static class AutofacConventionRocketHostExtensions | ||
{ | ||
/// <summary> | ||
/// Uses the Autofac. | ||
/// </summary> | ||
/// <param name="builder">The builder.</param> | ||
/// <param name="delegate">The container.</param> | ||
/// <returns>IHostBuilder.</returns> | ||
public static ConventionContextBuilder ConfigureAutofac([NotNull] this ConventionContextBuilder builder, AutofacConvention @delegate) | ||
{ | ||
if (builder == null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
builder.AppendDelegate(@delegate); | ||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// Uses the Autofac. | ||
/// </summary> | ||
/// <param name="builder">The builder.</param> | ||
/// <param name="delegate">The container.</param> | ||
/// <returns>IHostBuilder.</returns> | ||
public static ConventionContextBuilder ConfigureAutofac( | ||
[NotNull] this ConventionContextBuilder builder, | ||
Action<IConfiguration, IServiceCollection, ContainerBuilder> @delegate | ||
) | ||
{ | ||
if (builder == null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
builder.AppendDelegate(new AutofacConvention((context, configuration, services, container) => @delegate(configuration, services, container))); | ||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// Uses the Autofac. | ||
/// </summary> | ||
/// <param name="builder">The builder.</param> | ||
/// <param name="delegate">The container.</param> | ||
/// <returns>IHostBuilder.</returns> | ||
public static ConventionContextBuilder ConfigureAutofac([NotNull] this ConventionContextBuilder builder, Action<IServiceCollection, ContainerBuilder> @delegate) | ||
{ | ||
if (builder == null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
builder.AppendDelegate(new AutofacConvention((context, configuration, services, container) => @delegate(services, container))); | ||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// Uses the Autofac. | ||
/// </summary> | ||
/// <param name="builder">The builder.</param> | ||
/// <param name="delegate">The container.</param> | ||
/// <returns>IHostBuilder.</returns> | ||
public static ConventionContextBuilder ConfigureAutofac([NotNull] this ConventionContextBuilder builder, Action<ContainerBuilder> @delegate) | ||
{ | ||
if (builder == null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
builder.AppendDelegate(new AutofacConvention((context, configuration, services, container) => @delegate(container))); | ||
return builder; | ||
} | ||
} | ||
} |
Oops, something went wrong.