Exemplo n.º 1
0
        /// <summary>
        /// Uses marten.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <returns>IServiceConventionContext.</returns>
        public static IServiceConventionContext UseMartenUnitOfWork(this IServiceConventionContext container)
        {
            var options = container.GetOrAdd(() => new MartenOptions());

            options.AutomaticUnitOfWork = true;
            return(container);
        }
 /// <summary>
 /// Registers the specified context.
 /// </summary>
 /// <param name="context">The context.</param>
 public void Register([NotNull] IServiceConventionContext context)
 {
     if (context == null)
     {
         throw new ArgumentNullException(nameof(context));
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Registers the specified context.
 /// </summary>
 /// <param name="context">The context.</param>
 public void Register(IServiceConventionContext context)
 {
     if (_options.AutomaticUnitOfWork)
     {
         context.Services.TryAddEnumerable(ServiceDescriptor.Transient <IStartupFilter, MartenStartupFilter>());
     }
 }
        /// <summary>
        /// Uses MediatR.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="serviceConfig">The MediatR service configuration.</param>
        /// <returns>IServiceConventionContext.</returns>
        public static IServiceConventionContext UseMediatR(
            this IServiceConventionContext builder,
            MediatRServiceConfiguration serviceConfig
            )
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (serviceConfig is null)
            {
                throw new ArgumentNullException(nameof(serviceConfig));
            }

            if (builder.Services.Any(z => z.ServiceType == typeof(IMediator)))
            {
                return(builder);
            }

            builder.Set(serviceConfig);
            var assemblies = builder.AssemblyCandidateFinder
                             .GetCandidateAssemblies(nameof(MediatR)).ToArray();

            ServiceRegistrar.AddRequiredServices(builder.Services, serviceConfig);
            ServiceRegistrar.AddMediatRClasses(builder.Services, assemblies);
            return(builder);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Uses marten.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <returns>IServiceConventionContext.</returns>
        public static IServiceConventionContext UseMartenWithDirtyTracking(this IServiceConventionContext container)
        {
            var options = container.GetOrAdd(() => new MartenOptions());

            options.UseSession      = true;
            options.SessionTracking = DocumentTracking.DirtyTracking;
            return(container);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Uses marten.
 /// </summary>
 /// <param name="container">The container.</param>
 /// <param name="connection">The factory for the connection</param>
 /// <returns>IServiceConventionContext.</returns>
 public static IServiceConventionContext UseMartenConnectionString(
     this IServiceConventionContext container,
     Func <NpgsqlConnection> connection
     )
 {
     container.Services.ConfigureOptions(new MartenNpgsqlConnectionConnectionStringConvention(connection));
     return(container);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Uses marten.
 /// </summary>
 /// <param name="container">The container.</param>
 /// <param name="connectionString">The connection string</param>
 /// <returns>IServiceConventionContext.</returns>
 public static IServiceConventionContext UseMartenConnectionString(
     this IServiceConventionContext container,
     string connectionString
     )
 {
     container.Services.ConfigureOptions(new MartenStringConnectionStringConvention(() => connectionString));
     return(container);
 }
 /// <summary>
 /// Registers the specified context.
 /// </summary>
 /// <param name="context">The context.</param>
 public void Register(IServiceConventionContext context)
 {
     context.Services.Configure <MvcNewtonsoftJsonOptions>(options =>
     {
         options.SerializerSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
         options.SerializerSettings.Converters.Add(new StringEnumConverter(new CamelCaseNamingStrategy(), true));
     });
 }
            public void Register([NotNull] IServiceConventionContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException(nameof(context));
                }

                context.Services.AddSingleton(A.Fake <IOtherAbc3>());
            }
Exemplo n.º 10
0
        /// <inheritdoc />
        public void Register([NotNull] IServiceConventionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.Services.AddSingleton <ILoggerFactory>(
                _ =>
            {
                if (_options.WriteToProviders)
                {
                    var providerCollection = _.GetRequiredService <LoggerProviderCollection>();
                    var factory            = new SerilogLoggerFactory(_.GetRequiredService <global::Serilog.ILogger>(), true, providerCollection);

                    foreach (var provider in _.GetServices <ILoggerProvider>())
                    {
                        factory.AddProvider(provider);
                    }

                    return(factory);
                }

                return(new SerilogLoggerFactory(_.GetRequiredService <global::Serilog.ILogger>(), true));
            }
                );
            context.Services.AddHostedService <SerilogFinalizerHostedService>();

            var loggerConfiguration = context.GetOrAdd(() => new LoggerConfiguration());

            if (_options.WriteToProviders)
            {
                var loggerProviderCollection = context.GetOrAdd(() => new LoggerProviderCollection());
                context.Services.AddSingleton(loggerProviderCollection);
                loggerConfiguration.WriteTo.Providers(loggerProviderCollection);
            }

            var serilogBuilder = new SerilogBuilder(
                _scanner,
                context.AssemblyProvider,
                context.AssemblyCandidateFinder,
                context.Environment,
                context.Configuration,
                loggerConfiguration,
                _diagnosticSource,
                context.Properties
                );

            var logger = serilogBuilder.Build();

            if (!_options.PreserveStaticLogger)
            {
                Log.Logger = logger;
            }

            context.Services.AddSingleton(logger);
        }
        /// <summary>
        /// Uses MediatR.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <returns>IServiceConventionContext.</returns>
        public static IServiceConventionContext UseMediatR([NotNull] this IServiceConventionContext builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var serviceConfig = builder.GetOrAdd(() => new MediatRServiceConfiguration());

            return(UseMediatR(builder, serviceConfig));
        }
Exemplo n.º 12
0
        /// <summary>
        /// Registers the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Register(IServiceConventionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.Services.AddSingleton <IClock>(SystemClock.Instance);
            context.Services.AddSingleton <IDateTimeZoneProvider, DateTimeZoneCache>();
            context.Services.AddSingleton <IDateTimeZoneSource>(TzdbDateTimeZoneSource.Default);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Withes the marten.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns>MartenServicesBuilder.</returns>
        public static IServiceConventionContext WithMarten(this IServiceConventionContext context)
        {
            DefaultServices(context.Services);

            context.Services.AddOptions();
            context.Services.AddMemoryCache();

            context.Services.TryAddSingleton(new ProjectionDescriptorCollection(context.AssemblyCandidateFinder));

            return(context);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Registers the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Register([NotNull] IServiceConventionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.Services
            .Configure <MvcNewtonsoftJsonOptions>(
                options => options.SerializerSettings.Converters.Add(
                    new ValidationProblemDetailsNewtonsoftJsonConverter()
                    )
                );
        }
        /// <summary>
        /// Registers the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Register(IServiceConventionContext context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.Services.Configure <JsonOptions>(
                options =>
            {
                options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
                options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
                options.JsonSerializerOptions.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
            }
                );
        }
Exemplo n.º 16
0
        /// <summary>
        /// Registers the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Register(IServiceConventionContext context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var serviceConfig = context.GetOrAdd(() => new MediatRServiceConfiguration());

            context.Services.TryAddEnumerable(
                new ServiceDescriptor(
                    typeof(IPipelineBehavior <,>),
                    typeof(FluentValidationMediatRPipelineBehavior <,>),
                    serviceConfig.Lifetime
                    )
                );
        }
        /// <summary>
        /// Registers the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// TODO Edit XML Comment Template for Register
        public void Register([NotNull] IServiceConventionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var coreBuilder = context.Services
                              .AddMvcCore()
                              .AddControllersAsServices()
                              .AddApiExplorer();

            foreach (var item in context.AssemblyCandidateFinder.GetCandidateAssemblies("Rocket.Surgery.AspNetCore", "Microsoft.AspNetCore.Mvc"))
            {
                coreBuilder
                .AddApplicationPart(item);
            }

            context.Services.Configure <RazorViewEngineOptions>(options =>
            {
                // {0} - Action Name
                // {1} - Controller Name
                // {2} - Area Name
                // {3} - Feature Name
                // Replace normal view location entirely
                for (var i = Locations.Length - 1; i >= 0; i--)
                {
                    options.AreaViewLocationFormats.Insert(0, $"/Areas/{{2}}{Locations[i]}");
                }

                for (var i = Locations.Length - 1; i >= 0; i--)
                {
                    options.ViewLocationFormats.Insert(0, Locations[i]);
                }

                options.ViewLocationExpanders.Add(new FeatureViewLocationExpander());
            });

            context.Services.Configure <MvcOptions>(options =>
            {
                options.Conventions.Add(new FeatureConvention());
                options.Filters.Add <NotFoundExceptionFilter>();
                options.Filters.Add <RequestExceptionFilter>();
            });
        }
        /// <summary>
        /// Registers the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Register(IServiceConventionContext context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.Services
            .AddMvcCore()
            .AddNewtonsoftJson();
            context.Services.Configure <MvcNewtonsoftJsonOptions>(
                options =>
            {
                options.SerializerSettings.Converters.Add(new StringEnumConverter(new CamelCaseNamingStrategy()));
                options.SerializerSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
            }
                );
        }
        /// <summary>
        /// Registers the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Register(IServiceConventionContext context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.Services.AddSwaggerGenNewtonsoftSupport();
            context.Services.Configure <SwaggerGenOptions>(
                options =>
            {
                options.MapType <JToken>(
                    () => new OpenApiSchema()
                {
                    Type = "object",
                    AdditionalPropertiesAllowed = true,
                    Nullable = true,
                }
                    );

                options.MapType <JObject>(
                    () => new OpenApiSchema()
                {
                    Type = "object",
                    AdditionalPropertiesAllowed = true,
                    Nullable = true,
                }
                    );

                options.MapType <JArray>(
                    () => new OpenApiSchema()
                {
                    Type  = "array",
                    Items = new OpenApiSchema()
                    {
                        Type = "object",
                        AdditionalPropertiesAllowed = true,
                        Nullable = true,
                    }
                }
                    );
            }
                );
        }
Exemplo n.º 20
0
        /// <summary>
        /// Registers the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Register([NotNull] IServiceConventionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.Services
            .Configure <MvcOptions>(
                options => { options.Filters.Insert(0, new ValidationExceptionFilter()); }
                )
            .Configure <JsonOptions>(
                options => options.JsonSerializerOptions.Converters.Add(new ValidationProblemDetailsConverter())
                )
            .AddMvcCore()
            .AddFluentValidation(
                config =>
            {
                foreach (var field in typeof(FluentValidationMvcConfiguration).GetFields(
                             BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
                             ))
                {
                    field.SetValue(config, field.GetValue(_configuration));
                }

                config.ValidatorFactoryType ??= typeof(ValidatorFactory);
            }
                );

            context.Services.AddSingleton <IValidatorInterceptor, ValidatorInterceptor>();
            context.Services.AddSingleton <ProblemDetailsFactory, FluentValidationProblemDetailsFactory>();
            context.Services.Configure <ApiBehaviorOptions>(
                o =>
            {
                ProblemDetailsFactory?problemDetailsFactory = null;
                o.InvalidModelStateResponseFactory          = context =>
                {
                    // ProblemDetailsFactory depends on the ApiBehaviorOptions instance. We intentionally avoid constructor injecting
                    // it in this options setup to to avoid a DI cycle.
                    problemDetailsFactory ??= context.HttpContext.RequestServices
                    .GetRequiredService <ProblemDetailsFactory>();
                    return(problemDetailsInvalidModelStateResponse(problemDetailsFactory, context));
                };
Exemplo n.º 21
0
        /// <summary>
        /// Registers the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Register(IServiceConventionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var loggingBuilder = new LoggingBuilder(
                context.Get <IConventionScanner>(),
                context.AssemblyProvider,
                context.AssemblyCandidateFinder,
                context.Services,
                context.Environment,
                context.Configuration,
                context.Logger,
                context.Properties
                );

            context.Services.AddLogging();

            loggingBuilder.Build();
        }
        /// <summary>
        /// Registers the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Register([NotNull] IServiceConventionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            foreach (var item in new AssemblyScanner(
                         context
                         .AssemblyCandidateFinder
                         .GetCandidateAssemblies(nameof(FluentValidation))
                         .SelectMany(z => z.DefinedTypes)
                         .Select(x => x.AsType())
                         ))
            {
                context.Services.TryAddEnumerable(
                    ServiceDescriptor.Transient(item.InterfaceType, item.ValidatorType)
                    );
            }

            context.Services.TryAddSingleton <IValidatorFactory, ValidatorFactory>();
        }
Exemplo n.º 23
0
        /// <summary>
        /// Registers the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Register(IServiceConventionContext context)
        {
            context.WithMarten();

            var connectionString =
                !string.IsNullOrEmpty(_options.ConnectionString)
                    ? _options.ConnectionString
                    : context.Configuration.GetValue <string?>("PostgresSql:ConnectionString", null)
                ?? context.Configuration.GetValue <string?>("Postgres:ConnectionString", null)
                ?? context.Configuration.GetValue <string?>("Marten:ConnectionString", null);

            if (_options.UseSession)
            {
                context.Services.TryAddScoped(
                    c => c.GetRequiredService <IDocumentStore>().OpenSession(_options.SessionTracking)
                    );
            }

            if (!string.IsNullOrEmpty(connectionString))
            {
                context.Services.Configure <StoreOptions>(options => options.Connection(connectionString));
            }
        }
        private void AddAutoMapperClasses(IServiceConventionContext context)
        {
            var assemblies = context.AssemblyCandidateFinder.GetCandidateAssemblies(nameof(AutoMapper)).ToArray();

            context.Services.AddAutoMapper(assemblies, _options.ServiceLifetime);

            context.Services.Configure <MapperConfigurationExpression>(expression => expression.Features.Set(_options));

            context.Services.Replace(
                ServiceDescriptor.Singleton <IConfigurationProvider>(
                    _ =>
            {
                var options = _.GetService <IOptions <MapperConfigurationExpression> >();
                options.Value.AddMaps(assemblies);
                options.Value.Features.Set(
                    new AutoMapperLogger(
                        _.GetRequiredService <ILoggerFactory>().CreateLogger(nameof(AutoMapperLogger))
                        )
                    );
                return(new MapperConfiguration(options?.Value ?? new MapperConfigurationExpression()));
            }
                    )
                );
        }
Exemplo n.º 25
0
 public void Register(IServiceConventionContext context)
 {
 }
Exemplo n.º 26
0
 public void Register(IServiceConventionContext context) => throw new NotImplementedException();
Exemplo n.º 27
0
 public void Register(IServiceConventionContext context)
 {
     context.Services
     .AddLogging()
     .AddOptions();
 }
Exemplo n.º 28
0
 public void Register(IServiceConventionContext context)
 {
     context.Services.AddTransient <ServiceA>();
 }
Exemplo n.º 29
0
 public void Register(IServiceConventionContext context) => context.Services.AddSingleton(context);
Exemplo n.º 30
0
 public void Register(IServiceConventionContext context)
 {
     context.Services.AddSingleton <Shared.ISomeService>(new SomeService("Hello "));
 }