Skip to content

Commit

Permalink
add some dependency helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
info-miura committed Jan 21, 2025
1 parent 7a71d1b commit 635c054
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 26 deletions.
165 changes: 163 additions & 2 deletions src/Sekiban.Core.DotNet/Dependency/SekibanEventSourcingDependency.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Sekiban.Core.Command;
using Sekiban.Core.PubSub;
using Sekiban.Core.Setting;
using Sekiban.Core.Shared;
using Sekiban.Core.Snapshot.Aggregate;
Expand Down Expand Up @@ -28,6 +31,124 @@ public static class SekibanEventSourcingDependency
typeof(ReportVersionToSnapshotManger.Handler));
}

/// <summary>
/// Add Sekiban without <c>IUserInformationFactory</c>
/// </summary>
/// <see cref="Sekiban.Core.Command.UserInformation.IUserInformationFactory" />>
/// <param name="builder"></param>
/// <param name="dependencyDefinition"></param>
/// <returns></returns>
public static IHostApplicationBuilder AddSekibanWithoutUser(
this IHostApplicationBuilder builder,
IDependencyDefinition dependencyDefinition)
{
builder.Services.AddSekibanWithoutUser(dependencyDefinition, builder.Configuration);
return builder;
}

/// <summary>
/// Add Sekiban without <c>IUserInformationFactory</c>
/// </summary>
/// <see cref="Sekiban.Core.Command.UserInformation.IUserInformationFactory" />>
/// <param name="builder"></param>
/// <typeparam name="TDependency"></typeparam>
/// <returns></returns>
public static IHostApplicationBuilder AddSekibanWithoutUser<TDependency>(this IHostApplicationBuilder builder)
where TDependency : IDependencyDefinition, new()
{
builder.Services.AddSekibanWithoutUser(new TDependency(), builder.Configuration);
return builder;
}

/// <summary>
/// Add Sekiban without <c>IUserInformationFactory</c>
/// </summary>
/// <see cref="Sekiban.Core.Command.UserInformation.IUserInformationFactory" />>
/// <param name="builder"></param>
/// <param name="dependencyDefinition"></param>
/// <param name="settings"></param>
/// <returns></returns>
public static IHostApplicationBuilder AddSekibanWithoutUser(
this IHostApplicationBuilder builder,
IDependencyDefinition dependencyDefinition,
SekibanSettings settings)
{
builder.Services.AddSekibanWithoutUser(dependencyDefinition, settings);
return builder;
}

/// <summary>
/// Add Sekiban without <c>IUserInformationFactory</c>
/// </summary>
/// <see cref="Sekiban.Core.Command.UserInformation.IUserInformationFactory" />>
/// <typeparam name="TDependency"></typeparam>
/// <param name="services"></param>
/// <param name="configuration"></param>
/// <returns></returns>
public static IServiceCollection AddSekibanWithoutUser<TDependency>(
this IServiceCollection services,
IConfiguration configuration
)
where TDependency : IDependencyDefinition, new()
{
var dependencyDefinition = new TDependency();
var settings = SekibanSettings.FromConfiguration(configuration);
return AddSekibanWithoutUser(services, dependencyDefinition, settings);
}

/// <summary>
/// Add Sekiban without <c>IUserInformationFactory</c>
/// </summary>
/// <see cref="Sekiban.Core.Command.UserInformation.IUserInformationFactory" />>
/// <param name="services"></param>
/// <param name="dependencyDefinition"></param>
/// <param name="configuration"></param>
public static IServiceCollection AddSekibanWithoutUser(
this IServiceCollection services,
IDependencyDefinition dependencyDefinition,
IConfiguration configuration
)
{
var settings = SekibanSettings.FromConfiguration(configuration);
return AddSekibanWithoutUser(services, dependencyDefinition, settings);
}

/// <summary>
/// Add Sekiban without <c>IUserInformationFactory</c>
/// </summary>
/// <see cref="Sekiban.Core.Command.UserInformation.IUserInformationFactory" />>
/// <param name="services"></param>
/// <param name="dependencyDefinition"></param>
/// <param name="settings"></param>
public static IServiceCollection AddSekibanWithoutUser(
this IServiceCollection services,
IDependencyDefinition dependencyDefinition,
SekibanSettings settings
) => AddSekibanCoreWithoutUser(services, dependencyDefinition, settings);

/// <summary>
/// Add Sekiban without <c>IUserInformationFactory</c>
/// </summary>
/// <see cref="Sekiban.Core.Command.UserInformation.IUserInformationFactory" />>
/// <param name="services"></param>
/// <param name="dependencyDefinition"></param>
/// <param name="settings"></param>
/// <param name="sekibanDateProducer"></param>
/// <param name="multiProjectionType"></param>
/// <returns></returns>
public static IServiceCollection AddSekibanCoreWithoutUser(
this IServiceCollection services,
IDependencyDefinition dependencyDefinition,
SekibanSettings settings,
ISekibanDateProducer? sekibanDateProducer = null,
SekibanCoreServiceExtensions.MultiProjectionType multiProjectionType
= SekibanCoreServiceExtensions.MultiProjectionType.MemoryCache
)
{
RegisterWithoutUser(services, dependencyDefinition, settings, sekibanDateProducer, multiProjectionType);
return services;
}

/// <summary>
/// Add Sekiban with Dependency for InMemory Test
/// </summary>
Expand All @@ -39,11 +160,50 @@ public static IServiceCollection AddSekibanCoreInMemoryTestWithDependency(
this IServiceCollection services,
IDependencyDefinition dependencyDefinition,
ISekibanDateProducer? sekibanDateProducer = null)

{
RegisterForInMemoryTest(services, dependencyDefinition, sekibanDateProducer);
return services;
}

public static void RegisterWithoutUser(
IServiceCollection services,
IDependencyDefinition dependencyDefinition,
SekibanSettings settings,
ISekibanDateProducer? sekibanDateProducer = null,
SekibanCoreServiceExtensions.MultiProjectionType multiProjectionType
= SekibanCoreServiceExtensions.MultiProjectionType.MemoryCache)
{
// MediatR
services.AddMediatR(
new MediatRServiceConfiguration().RegisterServicesFromAssemblies(
Assembly.GetExecutingAssembly(),
typeof(UpdateNoticeEventSubscriber<>).Assembly)); // Sekiban.Core.DotNet needs to be added

// Sekiban Event Sourcing
services.AddSekibanCore(settings, sekibanDateProducer ?? new SekibanDateProducer(), multiProjectionType);
services.AddSingleton(settings);
services.AddTransient<IAggregateSettings, ContextAggregateSettings>();

// run Define() before using.
dependencyDefinition.Define();

// Each Domain contexts
services.AddSingleton(dependencyDefinition.GetSekibanDependencyOptions().RegisteredEventTypes);
services.AddSingleton(dependencyDefinition.GetSekibanDependencyOptions().SekibanAggregateTypes);
services.AddTransient(dependencyDefinition.GetSekibanDependencyOptions().TransientDependencies);
services.AddTransient([
(typeof(ICommandHandlerCommon<SnapshotManager, CreateSnapshotManager>), typeof(CreateSnapshotManager.Handler)),
(typeof(ICommandHandlerCommon<SnapshotManager, ReportVersionToSnapshotManger>), typeof(ReportVersionToSnapshotManger.Handler)),
]);

services.AddQueriesFromDependencyDefinition(dependencyDefinition);

foreach (var action in dependencyDefinition.GetServiceActions())
{
action(services);
}
}

/// <summary>
/// Register Sekiban with Dependency for InMemory Test
/// </summary>
Expand Down Expand Up @@ -147,7 +307,8 @@ public static void AddTransient(
if (implementationType is null)
{
services.AddTransient(serviceType);
} else
}
else
{
services.AddTransient(serviceType, implementationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Sekiban.Core.Command;
using Sekiban.Core.PubSub;
using Sekiban.Core.Setting;
using Sekiban.Core.Shared;
using Sekiban.Core.Snapshot.Aggregate;
Expand Down Expand Up @@ -156,30 +155,9 @@ public static void Register(
SekibanCoreServiceExtensions.MultiProjectionType multiProjectionType
= SekibanCoreServiceExtensions.MultiProjectionType.MemoryCache)
{
// MediatR
services.AddMediatR(
new MediatRServiceConfiguration().RegisterServicesFromAssemblies(
Assembly.GetExecutingAssembly(),
GetAssembly(),
typeof(UpdateNoticeEventSubscriber<>).Assembly)); // Sekiban.Core.DotNet needs to be added
// Sekiban Event Sourcing
services.AddSekibanCore(settings, sekibanDateProducer ?? new SekibanDateProducer(), multiProjectionType);
services.AddSekibanHTTPUser();
services.AddSingleton(settings);
services.AddTransient<IAggregateSettings, ContextAggregateSettings>();
// run Define() before using.
dependencyDefinition.Define();
// Each Domain contexts
services.AddSingleton(dependencyDefinition.GetSekibanDependencyOptions().RegisteredEventTypes);
services.AddSingleton(dependencyDefinition.GetSekibanDependencyOptions().SekibanAggregateTypes);
services.AddTransient(dependencyDefinition.GetSekibanDependencyOptions().TransientDependencies);
services.AddTransient(GetDependencies());

services.AddQueriesFromDependencyDefinition(dependencyDefinition);

foreach (var action in dependencyDefinition.GetServiceActions())
{
action(services);
}
SekibanEventSourcingDependency.RegisterWithoutUser(
services, dependencyDefinition, settings, sekibanDateProducer, multiProjectionType);
}
}

0 comments on commit 635c054

Please sign in to comment.