Skip to content

Commit

Permalink
(#216) Replace Config Action with extension method
Browse files Browse the repository at this point in the history
The configuration action does not add any value to the
client and arguably makes things more difficult to follow
as there are two ways to set client configuration.
  • Loading branch information
pardahlman committed May 7, 2017
1 parent 0156281 commit 4306167
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 37 deletions.
3 changes: 2 additions & 1 deletion sample/RawRabbit.AspNet.Sample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using RawRabbit.Configuration;
using RawRabbit.Enrichers.GlobalExecutionId;
using RawRabbit.vNext;
using RawRabbit.vNext.Logging;
Expand Down Expand Up @@ -34,7 +35,7 @@ public void ConfigureServices(IServiceCollection services)
services
.AddRawRabbit(new RawRabbitOptions
{
Configuration = builder => Configuration.GetSection("RawRabbit"),
ClientConfiguration = Configuration.ForRawRabbit(),
DependencyInjection = ioc => ioc.AddSingleton(LoggingFactory.ApplicationLogger),
Plugins = p => p
.UseStateMachine()
Expand Down
7 changes: 5 additions & 2 deletions sample/RawRabbit.ConsoleApp.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using RawRabbit.Configuration;
using RawRabbit.Enrichers.GlobalExecutionId;
using RawRabbit.Enrichers.MessageContext;
using RawRabbit.Enrichers.MessageContext.Context;
Expand All @@ -24,9 +25,11 @@ public static async Task RunAsync()
{
_client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
{
Configuration = cfg => cfg
ClientConfiguration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("rawrabbit.json"),
.AddJsonFile("rawrabbit.json")
.Build()
.Get<RawRabbitConfiguration>(),
Plugins = p => p
.UseGlobalExecutionId()
.UseMessageContext<MessageContext>()
Expand Down
21 changes: 1 addition & 20 deletions src/RawRabbit.vNext/AddRawRabbitExtension.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using RawRabbit.Configuration;
using Microsoft.Extensions.DependencyInjection;
using RawRabbit.DependecyInjection;
using RawRabbit.vNext.DependecyInjection;
using RawRabbitOptions = RawRabbit.vNext.Pipe.RawRabbitOptions;
Expand All @@ -14,24 +10,9 @@ public static class AddRawRabbitExtension
public static IServiceCollection AddRawRabbit(this IServiceCollection collection, RawRabbitOptions options = null)
{
options?.DependencyInjection?.Invoke(collection);
if (options?.Configuration != null)
{
options.ClientConfiguration = CreateClientConfig(options.Configuration);
}
var adapter = new ServiceCollectionAdapter(collection);
adapter.AddRawRabbit(options);

return collection;
}

private static RawRabbitConfiguration CreateClientConfig(Action<IConfigurationBuilder> config)
{
var builder = new ConfigurationBuilder();
config.Invoke(builder);
var mainCfg = RawRabbitConfiguration.Local;
builder.Build().Bind(mainCfg);
mainCfg.Hostnames = mainCfg.Hostnames.Distinct(StringComparer.CurrentCultureIgnoreCase).ToList();
return mainCfg;
}
}
}
20 changes: 20 additions & 0 deletions src/RawRabbit.vNext/ConfigurationExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Linq;
using Microsoft.Extensions.Configuration;
using RawRabbit.Configuration;

namespace RawRabbit.vNext
{
public static class ConfigurationExtension
{
public static RawRabbitConfiguration ForRawRabbit(this IConfiguration config, string sectionName = "RawRabbit")
{
var section = config.GetSection(sectionName);
if (!section.GetChildren().Any())
{
throw new ArgumentException($"Unable to configuration section '{sectionName}'. Make sure it exists in the provided configuration");
}
return section.Get<RawRabbitConfiguration>();
}
}
}
9 changes: 0 additions & 9 deletions src/RawRabbit.vNext/Pipe/RawRabbitFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ public static InstanceFactory CreateInstanceFactory(RawRabbitOptions options = n
var collection = applicationCollection ?? new ServiceCollection();
var ioc = new ServiceCollectionAdapter(collection);

if (options?.Configuration != null)
{
var builder = new ConfigurationBuilder();
options.Configuration.Invoke(builder);
var mainCfg = RawRabbitConfiguration.Local;
builder.Build().Bind(mainCfg);
mainCfg.Hostnames = mainCfg.Hostnames.Distinct(StringComparer.CurrentCultureIgnoreCase).ToList();
ioc.AddSingleton(c => mainCfg);
}
Action<IDependecyRegister> di = register =>
{
options?.DependencyInjection?.Invoke(collection);
Expand Down
3 changes: 0 additions & 3 deletions src/RawRabbit.vNext/Pipe/RawRabbitOptions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using RawRabbit.DependecyInjection;

namespace RawRabbit.vNext.Pipe
{
public class RawRabbitOptions : Instantiation.RawRabbitOptions
{
public new Action<IServiceCollection> DependencyInjection { get; set; }
public Action<IConfigurationBuilder> Configuration { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/RawRabbit.vNext/RawRabbit.vNext.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="1.1.1" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
Expand Down

0 comments on commit 4306167

Please sign in to comment.