Skip to content

Commit

Permalink
refactor: reworked builder for different target applications
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Dec 6, 2023
1 parent 0910679 commit 3e1ad27
Show file tree
Hide file tree
Showing 68 changed files with 537 additions and 744 deletions.
13 changes: 9 additions & 4 deletions src/Sitko.Core.App.Web/WebApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

namespace Sitko.Core.App.Web;

public class SitkoCoreWebApplicationBuilder : SitkoCoreApplicationBuilder
public interface ISitkoCoreWebApplicationBuilder : ISitkoCoreApplicationBuilder
{
}

public class SitkoCoreWebApplicationBuilder : SitkoCoreServerApplicationBuilder, ISitkoCoreWebApplicationBuilder
{
private readonly WebApplicationBuilder webApplicationBuilder;

Expand All @@ -31,13 +35,14 @@ protected override void ConfigureHostBuilder<TModule, TModuleOptions>(Applicatio

public static class WebApplicationExtensions
{
public static SitkoCoreApplicationBuilder AddSitkoCore(this WebApplicationBuilder builder) =>
public static ISitkoCoreWebApplicationBuilder AddSitkoCore(this WebApplicationBuilder builder) =>
builder.AddSitkoCore(Array.Empty<string>());

public static SitkoCoreApplicationBuilder AddSitkoCore(this WebApplicationBuilder builder, string[] args)
public static ISitkoCoreWebApplicationBuilder AddSitkoCore(this WebApplicationBuilder builder, string[] args)
{
builder.Services.TryAddTransient<IStartupFilter, SitkoCoreWebStartupFilter>();
return new SitkoCoreWebApplicationBuilder(builder, args);
return ApplicationBuilderFactory.GetOrCreateApplicationBuilder(builder,
applicationBuilder => new SitkoCoreWebApplicationBuilder(applicationBuilder, args));
}

public static WebApplication MapSitkoCore(this WebApplication webApplication)
Expand Down
215 changes: 0 additions & 215 deletions src/Sitko.Core.App/Application.cs

This file was deleted.

42 changes: 42 additions & 0 deletions src/Sitko.Core.App/ApplicationBuilderFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Concurrent;

namespace Sitko.Core.App;

public static class ApplicationBuilderFactory
{
private static readonly ConcurrentDictionary<int, ISitkoCoreApplicationBuilder> Builders = new();

public static TSitkoCoreApplicationBuilder
GetOrCreateApplicationBuilder<TApplicationBuilder, TSitkoCoreApplicationBuilder>(
TApplicationBuilder applicationBuilder, Func<TApplicationBuilder, TSitkoCoreApplicationBuilder> create)
where TSitkoCoreApplicationBuilder : ISitkoCoreApplicationBuilder where TApplicationBuilder : notnull
{
var appBuilder = Builders.GetOrAdd(applicationBuilder.GetHashCode(), _ => create(applicationBuilder));
if (appBuilder is not TSitkoCoreApplicationBuilder typedBuilder)
{
throw new InvalidOperationException($"Application builder is not {typeof(TSitkoCoreApplicationBuilder)}");
}

return typedBuilder;
}


public static TSitkoCoreApplicationBuilder GetApplicationBuilder<TApplicationBuilder,
TSitkoCoreApplicationBuilder>(TApplicationBuilder applicationBuilder)
where TSitkoCoreApplicationBuilder : ISitkoCoreApplicationBuilder where TApplicationBuilder : notnull
{
if (Builders.ContainsKey(applicationBuilder.GetHashCode()))
{
var builder = Builders[applicationBuilder.GetHashCode()];
if (builder is TSitkoCoreApplicationBuilder typedBuilder)
{
return typedBuilder;
}

throw new InvalidOperationException($"Application builder is not {typeof(TSitkoCoreApplicationBuilder)}");
}

throw new InvalidOperationException(
$"Application builder wasn't created for this HostBuilder. Call .AddSitkoCore*()");
}
}
27 changes: 24 additions & 3 deletions src/Sitko.Core.App/BuilderApplicationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,34 @@

namespace Sitko.Core.App;

public interface IApplicationEnvironment
{
string EnvironmentName { get; }
bool IsDevelopment();
bool IsProduction();
}

public class ServerApplicationEnvironment : IApplicationEnvironment
{
private readonly IHostEnvironment hostEnvironment;

public ServerApplicationEnvironment(IHostEnvironment hostEnvironment) => this.hostEnvironment = hostEnvironment;

public string EnvironmentName => hostEnvironment.EnvironmentName;
public bool IsDevelopment() => hostEnvironment.IsDevelopment();

public bool IsProduction() => hostEnvironment.IsProduction();
}

public class BuilderApplicationContext : IApplicationContext
{
private readonly IHostEnvironment environment;
public static string OptionsKey => "Application";

private readonly IApplicationEnvironment environment;

private ApplicationOptions? applicationOptions;

public BuilderApplicationContext(IConfiguration configuration, IHostEnvironment environment,
public BuilderApplicationContext(IConfiguration configuration, IApplicationEnvironment environment,
IApplicationArgsProvider applicationArgsProvider)
{
this.environment = environment;
Expand Down Expand Up @@ -54,7 +75,7 @@ private ApplicationOptions GetApplicationOptions()
}

applicationOptions = new ApplicationOptions();
Configuration.Bind(SitkoCoreApplicationBuilder.OptionsKey, applicationOptions);
Configuration.Bind(OptionsKey, applicationOptions);
if (string.IsNullOrEmpty(applicationOptions.Name))
{
applicationOptions.Name = Assembly.GetEntryAssembly()?.GetName().Name ?? "App";
Expand Down
17 changes: 11 additions & 6 deletions src/Sitko.Core.App/HostApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
using System.Collections.Concurrent;
using Microsoft.Extensions.Hosting;

namespace Sitko.Core.App;

public static class HostApplicationBuilderExtensions
{
private static readonly ConcurrentDictionary<IHostApplicationBuilder, SitkoCoreApplicationBuilder> Builders = new();
public static ISitkoCoreApplicationBuilder
AddSitkoCore(this IHostApplicationBuilder builder) => AddSitkoCore<ISitkoCoreApplicationBuilder>(builder);

public static SitkoCoreApplicationBuilder
AddSitkoCore(this IHostApplicationBuilder builder) =>
public static TSitkoCoreApplicationBuilder
AddSitkoCore<TSitkoCoreApplicationBuilder>(this IHostApplicationBuilder builder)
where TSitkoCoreApplicationBuilder : ISitkoCoreApplicationBuilder =>
ApplicationBuilderFactory.GetApplicationBuilder<IHostApplicationBuilder, TSitkoCoreApplicationBuilder>(builder);

public static ISitkoCoreServerApplicationBuilder AddSitkoCore(this HostApplicationBuilder builder) =>
builder.AddSitkoCore(Array.Empty<string>());

public static SitkoCoreApplicationBuilder AddSitkoCore(this IHostApplicationBuilder builder, string[] args) =>
Builders.GetOrAdd(builder, _ => new SitkoCoreApplicationBuilder(builder, args));
public static ISitkoCoreServerApplicationBuilder AddSitkoCore(this HostApplicationBuilder builder, string[] args) =>
ApplicationBuilderFactory.GetOrCreateApplicationBuilder(builder,
applicationBuilder => new SitkoCoreServerApplicationBuilder(applicationBuilder, args));
}
Loading

0 comments on commit 3e1ad27

Please sign in to comment.