示例#1
0
        public static T GetOrAdd <T>(
            [NotNull] this IConventionHostBuilder builder,
            string key,
            [NotNull] Func <T> factory
            )
            where T : class
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            if (!(builder.ServiceProperties[key] is T value))
            {
                value = factory();
                builder.Set(value);
            }

            return(value);
        }
示例#2
0
        /// <summary>
        /// Uses marten.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <returns>IConventionHostBuilder.</returns>
        public static IConventionHostBuilder UseMartenUnitOfWork(this IConventionHostBuilder container)
        {
            var options = container.GetOrAdd(() => new MartenOptions());

            options.AutomaticUnitOfWork = true;
            return(container);
        }
示例#3
0
        /// <summary>
        /// Uses marten.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <returns>IConventionHostBuilder.</returns>
        public static IConventionHostBuilder UseMartenWithDirtyTracking(this IConventionHostBuilder container)
        {
            var options = container.GetOrAdd(() => new MartenOptions());

            options.UseSession      = true;
            options.SessionTracking = DocumentTracking.DirtyTracking;
            return(container);
        }
示例#4
0
 /// <summary>
 /// Uses marten.
 /// </summary>
 /// <param name="container">The container.</param>
 /// <param name="connection">The factory for the connection</param>
 /// <returns>IConventionHostBuilder.</returns>
 public static IConventionHostBuilder UseMartenConnectionString(
     this IConventionHostBuilder container,
     Func <NpgsqlConnection> connection
     )
 {
     container.Scanner.PrependConvention(new MartenNpgsqlConnectionConnectionStringConvention(connection));
     return(container);
 }
示例#5
0
 /// <summary>
 /// Uses marten.
 /// </summary>
 /// <param name="container">The container.</param>
 /// <param name="connectionString">The connection string</param>
 /// <returns>IConventionHostBuilder.</returns>
 public static IConventionHostBuilder UseMartenConnectionString(
     this IConventionHostBuilder container,
     string connectionString
     )
 {
     container.Scanner.PrependConvention(new MartenStringConnectionStringConvention(() => connectionString));
     return(container);
 }
        /// <summary>
        /// Adds the marten functions unit of work.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <returns>IConventionHostBuilder.</returns>
        public static IConventionHostBuilder AddMartenUnitOfWorkMiddleware(this IConventionHostBuilder builder)
        {
            var options = builder.GetOrAdd(() => new MartenOptions());

            options.AutomaticUnitOfWork = true;
            builder.Scanner.PrependConvention <MartenMiddlewareUnitOfWorkConvention>();
            return(builder);
        }
        /// <summary>
        /// Adds the marten functions unit of work.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <returns>IConventionHostBuilder.</returns>
        public static IConventionHostBuilder AddMartenUnitOfWorkFunctionFilter(this IConventionHostBuilder builder)
        {
            var options = builder.GetOrAdd(() => new MartenOptions());

            options.AutomaticUnitOfWork = true;
            builder.Scanner.AppendConvention <MartenFunctionsUnitOfWorkConvention>();
            return(builder);
        }
示例#8
0
        /// <summary>
        /// Check if this is a test host (to allow conventions to behave differently during unit tests)
        /// </summary>
        /// <param name="context">The context</param>
        public static bool IsUnitTestHost([NotNull] this IConventionHostBuilder context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return(context.ServiceProperties.IsUnitTestHost());
        }
示例#9
0
        public static T Get <T>([NotNull] this IConventionHostBuilder context, string key)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return((T)context.ServiceProperties[key]);
        }
示例#10
0
        /// <summary>
        /// Get a value by type from the context
        /// </summary>
        /// <typeparam name="T">The type of the value</typeparam>
        /// <param name="context">The context</param>
        /// <param name="key">The key where the value is saved</param>
        /// <param name="value">The value to save</param>
        public static void Set <T>([NotNull] this IConventionHostBuilder context, string key, T value)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.ServiceProperties[key] = value;
        }
示例#11
0
 /// <summary>
 /// Uses marten.
 /// </summary>
 /// <param name="container">The container.</param>
 /// <param name="options">The options.</param>
 /// <returns>IConventionHostBuilder.</returns>
 public static IConventionHostBuilder UseMarten(
     this IConventionHostBuilder container,
     MartenOptions?options = null
     )
 {
     container.Set(options ?? new MartenOptions());
     container.Scanner.PrependConvention <MartenCommandConvention>();
     container.Scanner.PrependConvention <MartenConvention>();
     return(container);
 }
        /// <summary>
        /// Adds fluent validation.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <returns>IConventionHostBuilder.</returns>
        public static IConventionHostBuilder UseFluentValidation([NotNull] this IConventionHostBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Scanner.PrependConvention <FluentValidationConvention>();
            return(builder);
        }
        /// <summary>
        /// Adds fluent validation.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <returns>IConventionHostBuilder.</returns>
        public static IConventionHostBuilder UseSwashbuckleNewtonsoftJson(this IConventionHostBuilder builder)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Scanner.PrependConvention <SwashbuckleNewtonsoftJsonConvention>();
            return(builder);
        }
示例#14
0
        /// <summary>
        /// Adds fluent validation.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <returns>IConventionHostBuilder.</returns>
        public static IConventionHostBuilder UseSwashbuckle(this IConventionHostBuilder builder)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.UseFluentValidation();
            builder.Scanner.PrependConvention <SwashbuckleConvention>();
            return(builder);
        }
示例#15
0
        /// <summary>
        /// Uses the serilog request logging.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <returns>IConventionHostBuilder.</returns>
        public static IConventionHostBuilder UseSerilogRequestLogging(
            [NotNull] this IConventionHostBuilder container
            )
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            container.Scanner.PrependConvention <RequestLoggingConvention>();
            return(container);
        }
        /// <summary>
        /// Configure the configuration delegate to the convention scanner
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="delegate">The delegate.</param>
        /// <returns>IConventionHostBuilder.</returns>
        public static IConventionHostBuilder ConfigureConfiguration(
            [NotNull] this IConventionHostBuilder container,
            ConfigurationConventionDelegate @delegate
            )
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            container.Scanner.AppendDelegate(@delegate);
            return(container);
        }
示例#17
0
        /// <summary>
        /// Adds fluent validation.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <returns>IConventionHostBuilder.</returns>
        public static IConventionHostBuilder UseOperations(this IConventionHostBuilder builder)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.UseAutoMapper();
            builder.UseMediatR();
            builder.UseFluentValidation();
            builder.Scanner.PrependConvention <MediatorROperationsConvention>();
            return(builder);
        }
示例#18
0
        /// <summary>
        /// Uses the logging.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="options">The options.</param>
        /// <returns>IConventionHostBuilder.</returns>
        public static IConventionHostBuilder UseLogging(
            [NotNull] this IConventionHostBuilder container,
            RocketLoggingOptions?options = null
            )
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            container.ServiceProperties[typeof(RocketLoggingOptions)] = options ?? new RocketLoggingOptions();
            container.Scanner.PrependConvention <LoggingServiceConvention>();
            return(container);
        }
        /// <summary>
        /// Uses the default application metrics.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <returns>IConventionHostBuilder.</returns>
        public static IConventionHostBuilder UseMetricsWithDefaults([NotNull] this IConventionHostBuilder container)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            container.Set(new RocketMetricsOptions()
            {
                UseDefaults = true
            });
            container.Scanner.PrependConvention <HostingMetricsConvention>();
            return(container);
        }
示例#20
0
        /// <summary>
        /// Uses AutoMapper.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="options">The options object</param>
        /// <returns>IConventionHostBuilder.</returns>
        public static IConventionHostBuilder UseAutoMapper(
            [NotNull] this IConventionHostBuilder container,
            AutoMapperOptions?options = null
            )
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            container.Set(options ?? new AutoMapperOptions());
            container.Scanner.PrependConvention <AutoMapperConvention>();
            return(container);
        }
        /// <summary>
        /// Uses the serilog.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="options">The options.</param>
        /// <returns>IConventionHostBuilder.</returns>
        public static IConventionHostBuilder UseSerilog(
            [NotNull] this IConventionHostBuilder container,
            RocketSerilogOptions?options = null
            )
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            container.ServiceProperties[typeof(RocketSerilogOptions)] = options ?? new RocketSerilogOptions();
            container.Scanner.PrependConvention <SerilogExtensionsConvention>();
            container.Scanner.PrependConvention <SerilogReadFromConfigurationConvention>();
            container.Scanner.PrependConvention <SerilogEnrichLoggingConvention>();
            container.Scanner.PrependConvention <SerilogConsoleLoggingConvention>();
            container.Scanner.PrependConvention <SerilogDebugLoggingConvention>();
            container.Scanner.PrependConvention <EnvironmentLoggingConvention>();
            return(container);
        }
示例#22
0
        /// <summary>
        /// Adds MediatR.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="serviceConfig">The MediatR service configuration.</param>
        /// <returns>IConventionHostBuilder.</returns>
        public static IConventionHostBuilder UseMediatR(
            this IConventionHostBuilder builder,
            MediatRServiceConfiguration serviceConfig
            )
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

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

            builder.Set(serviceConfig);
            builder.Scanner.PrependConvention <MediatRConvention>();
            return(builder);
        }
示例#23
0
 /// <summary>
 /// Check if this is a test host (to allow conventions to behave differently during unit tests)
 /// </summary>
 /// <param name="context">The context</param>
 internal static HostType GetHostType(this IConventionHostBuilder context)
 => context.ServiceProperties.TryGetValue(typeof(HostType), out var hostType)
         ? (HostType)hostType !
         : HostType.Undefined;
示例#24
0
 public static IConventionHostBuilder AddMartenFunctionsUnitOfWork(this IConventionHostBuilder builder)
 {
     builder.Scanner.AppendConvention(new MartenMiddlewareUnitOfWorkConvention());
     return(builder);
 }