Skip to content
This repository was archived by the owner on Jan 17, 2022. It is now read-only.

Commit

Permalink
Refactor/remove extra builders (#314)
Browse files Browse the repository at this point in the history
* 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
david-driscoll and mergify[bot] authored Sep 5, 2020
1 parent 7406443 commit d52dfb7
Show file tree
Hide file tree
Showing 20 changed files with 287 additions and 856 deletions.
8 changes: 4 additions & 4 deletions Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
<ItemGroup>
<PackageReference Update="Autofac" Version="5.2.0" />
<PackageReference Update="Autofac.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Update="Rocket.Surgery.Conventions" Version="10.0.0-beta.16" />
<PackageReference Update="Rocket.Surgery.Conventions.Abstractions" Version="10.0.0-beta.16" />
<PackageReference Update="Rocket.Surgery.Hosting" Version="10.0.0-beta.16" />
<PackageReference Update="Rocket.Surgery.WebAssembly.Hosting" Version="10.0.0-beta.16" />
<PackageReference Update="Rocket.Surgery.Conventions" Version="10.0.0-beta.17" />
<PackageReference Update="Rocket.Surgery.Conventions.Abstractions" Version="10.0.0-beta.17" />
<PackageReference Update="Rocket.Surgery.Hosting" Version="10.0.0-beta.17" />
<PackageReference Update="Rocket.Surgery.WebAssembly.Hosting" Version="10.0.0-beta.17" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="Bogus" Version="30.0.4" />
Expand Down
223 changes: 0 additions & 223 deletions src/Conventions.Autofac/AutofacBuilder.cs

This file was deleted.

13 changes: 10 additions & 3 deletions src/Conventions.Autofac/AutofacConventionDelegate.cs
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 src/Conventions.Autofac/AutofacConventionServiceProviderFactory.cs
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>();
}
}
88 changes: 88 additions & 0 deletions src/Conventions.Autofac/AutofacRocketHostExtensions.cs
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;
}
}
}
Loading

0 comments on commit d52dfb7

Please sign in to comment.