-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reworked builder for different target applications
- Loading branch information
Showing
68 changed files
with
537 additions
and
744 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 |
---|---|---|
@@ -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*()"); | ||
} | ||
} |
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
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)); | ||
} |
Oops, something went wrong.