Skip to content

Commit

Permalink
Minor ServiceCollectionExtentions cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danielskovli committed Nov 27, 2024
1 parent 0548d1d commit 1b64d15
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private static void AddApplicationIdentifier(IServiceCollection services)
{
services.AddSingleton(sp =>
{
var appIdentifier = GetApplicationId();
string appIdentifier = GetApplicationId();
return new AppIdentifier(appIdentifier);
});
}
Expand All @@ -132,14 +132,10 @@ private static string GetApplicationId()

var id = appMetadataJObject?.SelectToken("id")?.Value<string>();

if (id == null)
{
throw new KeyNotFoundException(
"Could not find id in applicationmetadata.json. Please ensure applicationmeta.json is well formed and contains a key for id."
return id
?? throw new KeyNotFoundException(
"Could not find id in applicationmetadata.json. Please ensure the file is well formed and contains a key for `id`"
);
}

return id;
}

/// <summary>
Expand All @@ -154,9 +150,7 @@ public static void AddAppServices(
IWebHostEnvironment env
)
{
// Services for Altinn App
services.TryAddTransient<IPDP, PDPAppSI>();
AddValidationServices(services, configuration);
services.TryAddTransient<IPrefill, PrefillSI>();
services.TryAddTransient<ISigningCredentialsResolver, SigningCredentialsResolver>();
services.TryAddSingleton<IAppResources, AppResourcesSI>();
Expand All @@ -175,13 +169,14 @@ IWebHostEnvironment env
services.TryAddTransient<ILayoutEvaluatorStateInitializer, LayoutEvaluatorStateInitializer>();
services.TryAddTransient<LayoutEvaluatorStateInitializer>();
services.AddTransient<IDataService, DataService>();
services.AddSingleton<ModelSerializationService>();
services.Configure<Common.PEP.Configuration.PepSettings>(configuration.GetSection("PEPSettings"));
services.Configure<Common.PEP.Configuration.PlatformSettings>(configuration.GetSection("PlatformSettings"));
services.Configure<AccessTokenSettings>(configuration.GetSection("AccessTokenSettings"));
services.Configure<FrontEndSettings>(configuration.GetSection(nameof(FrontEndSettings)));
services.Configure<PdfGeneratorSettings>(configuration.GetSection(nameof(PdfGeneratorSettings)));
services.AddSingleton<ModelSerializationService>();

AddValidationServices(services, configuration);
AddAppOptions(services);
AddExternalApis(services);
AddActionServices(services);
Expand Down Expand Up @@ -209,32 +204,29 @@ private static void AddValidationServices(IServiceCollection services, IConfigur
{
services.AddTransient<IValidatorFactory, ValidatorFactory>();
services.AddTransient<IValidationService, ValidationService>();
if (configuration.GetSection("AppSettings").Get<AppSettings>()?.RequiredValidation == true)
services.AddTransient<IFormDataValidator, DataAnnotationValidator>();
services.AddTransient<IDataElementValidator, DefaultDataElementValidator>();
services.AddTransient<ITaskValidator, DefaultTaskValidator>();

var appSettings = configuration.GetSection("AppSettings").Get<AppSettings>();
if (appSettings?.RequiredValidation is true)
{
services.AddTransient<IValidator, RequiredLayoutValidator>();
}

if (configuration.GetSection("AppSettings").Get<AppSettings>()?.ExpressionValidation == true)
if (appSettings?.ExpressionValidation is true)
{
services.AddTransient<IValidator, ExpressionValidator>();
}
services.AddTransient<IFormDataValidator, DataAnnotationValidator>();
services.AddTransient<IDataElementValidator, DefaultDataElementValidator>();
services.AddTransient<ITaskValidator, DefaultTaskValidator>();
}

/// <summary>
/// Checks if a service is already added to the collection.
/// </summary>
/// <returns>true if the services allready exists in the collection, otherwise false</returns>
/// <returns>true if the services already exists in the collection, otherwise false</returns>
public static bool IsAdded(this IServiceCollection services, Type serviceType)
{
if (services.Any(x => x.ServiceType == serviceType))
{
return true;
}

return false;
return services.Any(x => x.ServiceType == serviceType);
}

private static void AddEventServices(IServiceCollection services)
Expand All @@ -243,7 +235,8 @@ private static void AddEventServices(IServiceCollection services)
services.AddTransient<IEventHandlerResolver, EventHandlerResolver>();
services.TryAddSingleton<IEventSecretCodeProvider, KeyVaultEventSecretCodeProvider>();

// The event subscription client depends uppon a maskinporten messagehandler beeing
// TODO: Event subs could be handled by the new automatic Maskinporten auth, once implemented.

Check warning on line 238 in src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / Static code analysis

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
// The event subscription client depends upon a Maskinporten message handler being
// added to the client during setup. As of now this needs to be done in the apps
// if subscription is to be added. This registration is to prevent the DI container
// from failing for the apps not using event subscription. If you try to use
Expand Down Expand Up @@ -317,18 +310,17 @@ private static void AddAppOptions(IServiceCollection services)
private static void AddExternalApis(IServiceCollection services)
{
services.AddTransient<IExternalApiService, ExternalApiService>();

services.TryAddTransient<IExternalApiFactory, ExternalApiFactory>();
}

private static void AddProcessServices(IServiceCollection services)
{
services.AddTransient<IProcessExclusiveGateway, ExpressionsExclusiveGateway>();
services.TryAddTransient<IProcessEngine, ProcessEngine>();
services.TryAddTransient<IProcessNavigator, ProcessNavigator>();
services.TryAddSingleton<IProcessReader, ProcessReader>();
services.TryAddTransient<IProcessEventHandlerDelegator, ProcessEventHandlingDelegator>();
services.TryAddTransient<IProcessEventDispatcher, ProcessEventDispatcher>();
services.AddTransient<IProcessExclusiveGateway, ExpressionsExclusiveGateway>();
services.TryAddTransient<ExclusiveGatewayFactory>();

services.AddTransient<IProcessTaskInitializer, ProcessTaskInitializer>();
Expand All @@ -340,14 +332,14 @@ private static void AddProcessServices(IServiceCollection services)
services.AddTransient<IAbandonTaskEventHandler, AbandonTaskEventHandler>();
services.AddTransient<IEndEventEventHandler, EndEventEventHandler>();

//PROCESS TASKS
// Process tasks
services.AddTransient<IProcessTask, DataProcessTask>();
services.AddTransient<IProcessTask, ConfirmationProcessTask>();
services.AddTransient<IProcessTask, FeedbackProcessTask>();
services.AddTransient<IProcessTask, SigningProcessTask>();
services.AddTransient<IProcessTask, NullTypeProcessTask>();

//SERVICE TASKS
// Service tasks
services.AddTransient<IServiceTask, PdfServiceTask>();
services.AddTransient<IServiceTask, EformidlingServiceTask>();
}
Expand Down

0 comments on commit 1b64d15

Please sign in to comment.