52 lines
1.8 KiB
C#
52 lines
1.8 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using WorkFlow.Application.Mapping;
|
|
|
|
namespace WorkFlow.Application;
|
|
|
|
public static class DependencyInjection
|
|
{
|
|
public static IServiceCollection AddWorkFlowServices(this IServiceCollection services, Action<WorkFlowServiceOptions>? options = null)
|
|
{
|
|
WorkFlowServiceOptions sOptions = new(services);
|
|
options?.Invoke(sOptions);
|
|
|
|
services.AddAutoMapper(typeof(MappingProfile).Assembly);
|
|
services.AddMediatR(cfg =>
|
|
{
|
|
cfg.RegisterServicesFromAssembly(typeof(DependencyInjection).Assembly);
|
|
cfg.LicenseKey = sOptions.MediatRLicense;
|
|
});
|
|
|
|
if(!sOptions.IsMappingConfigured)
|
|
services.Configure<MappingOptions>(_ => { });
|
|
|
|
services.AddTransient<TfFileUriResolver>();
|
|
services.AddTransient<TfFileIconUriResolver>();
|
|
|
|
return services;
|
|
}
|
|
|
|
public class WorkFlowServiceOptions
|
|
{
|
|
private readonly IServiceCollection _services;
|
|
|
|
internal bool IsMappingConfigured { get; private set; } = false;
|
|
|
|
public WorkFlowServiceOptions(IServiceCollection services) => _services = services;
|
|
|
|
private void EnsureSingleMappingConfiguration(Action action)
|
|
{
|
|
if (IsMappingConfigured)
|
|
throw new InvalidOperationException("Mapping configuration has already been set.");
|
|
action();
|
|
IsMappingConfigured = true;
|
|
}
|
|
|
|
public void ConfigMapping(IConfiguration config) => EnsureSingleMappingConfiguration(() => _services.Configure<MappingOptions>(config));
|
|
|
|
public void ConfigMapping(Action<MappingOptions> options) => EnsureSingleMappingConfiguration(() => _services.Configure(options));
|
|
|
|
public string? MediatRLicense { get; set; }
|
|
}
|
|
} |