/// <summary> /// Registers the application service in the container /// </summary> /// <param name="services"></param> /// <param name="throwOnError">Set to true if you want the app service to throw instead of returning the error result</param> /// <typeparam name="T">Application service implementation type</typeparam> /// <typeparam name="TState">Aggregate state type</typeparam> /// <typeparam name="TId">Aggregate identity type</typeparam> /// <typeparam name="TAggregate">Aggregate type</typeparam> /// <returns></returns> public static IServiceCollection AddApplicationService <T, TAggregate, TState, TId>( this IServiceCollection services, bool throwOnError = false ) where T : class, IApplicationService <TAggregate, TState, TId> where TState : AggregateState <TState, TId>, new() where TId : AggregateId where TAggregate : Aggregate <TState, TId> { services.TryAddSingleton <AggregateFactoryRegistry>(); services.AddSingleton <T>(); services.AddSingleton(sp => GetThrowingService(GetTracedService(sp))); return(services); IApplicationService <TAggregate, TState, TId> GetThrowingService( IApplicationService <TAggregate, TState, TId> inner ) => throwOnError ? new ThrowingApplicationService <TAggregate, TState, TId>(inner) : inner; IApplicationService <TAggregate, TState, TId> GetTracedService(IServiceProvider serviceProvider) => EventuousDiagnostics.Enabled ? TracedApplicationService <TAggregate, TState, TId> .Trace(serviceProvider.GetRequiredService <T>()) : serviceProvider.GetRequiredService <T>(); }
/// <summary> /// Registers the application service in the container /// </summary> /// <param name="services"></param> /// <typeparam name="T"></typeparam> /// <typeparam name="TAggregate"></typeparam> /// <returns></returns> public static IServiceCollection AddApplicationService <T, TAggregate>(this IServiceCollection services) where T : class, IApplicationService <TAggregate> where TAggregate : Aggregate { services.TryAddSingleton <AggregateFactoryRegistry>(); services.AddSingleton <T>(); if (EventuousDiagnostics.Enabled) { services.AddSingleton( sp => TracedApplicationService <TAggregate> .Trace(sp.GetRequiredService <T>()) ); } else { services.AddSingleton <IApplicationService <TAggregate> >(sp => sp.GetRequiredService <T>()); } return(services); }