/// <summary>
        /// <para>
        /// Registers an implementation based on SQL Server / Azure SQL. Use the options to specify the database connection.
        /// </para>
        /// <para>
        /// This overload takes a generic dependency to inject, and a function to get a <see cref="DbConnection"/> from that dependency.
        /// The dependency must be registered separately.
        /// </para>
        /// <para>
        /// The type parameter determines the database connection factory to get from the service provider, which should be registered separately.
        /// </para>
        /// <para>
        /// The implementation will throw if the database does not exist.
        /// The table, however, is created automatically, because different instances of the table may be used in various databases or bounded contexts.
        /// </para>
        /// </summary>
        /// <param name="getConnectionFromFactory">A function that gets a new <see cref="DbConnection"/> from the registered connection factory.</param>
        /// <param name="connectionString">Written onto produced <see cref="DbConnection"/> objects, if given. Required only if a <see cref="DbConnection"/> is produced without a connection string.</param>
        /// <param name="databaseAndSchemaName">If the connection factory's connection string does not specify the database and schema name, specify database.schema here.</param>
        public static ApplicationInstanceIdSourceExtensions.Options UseSqlServer <TDatabaseConnectionFactory>(this ApplicationInstanceIdSourceExtensions.Options options,
                                                                                                              Func <TDatabaseConnectionFactory, DbConnection> getConnectionFromFactory, string?connectionString = null,
                                                                                                              string?databaseAndSchemaName = null)
            where TDatabaseConnectionFactory : class
        {
            // Register a custom table name to use
            options.Services.AddSingleton(new ApplicationInstanceIdCustomTableName(SqlServerApplicationInstanceIdRenter.DefaultTableName));

            // Register an IDbConnectionFactory
            ApplicationInstanceIdSourceDbConnectionFactory.Register(options.Services, getConnectionFromFactory, connectionString);

            // Register an IApplicationInstanceIdSourceTransactionalExecutor that uses the IDbConnectionFactory
            options.Services.AddTransient <IApplicationInstanceIdSourceTransactionalExecutor, SqlTransactionalExecutor>();

            // Register the IApplicationInstanceIdRenter that uses all of the above
            options.Services.AddTransient(CreateInstance);

            return(options);

            // Local function that creates a new instance
            IApplicationInstanceIdRenter CreateInstance(IServiceProvider serviceProvider)
            {
                var instance = new SqlServerApplicationInstanceIdRenter(serviceProvider, databaseAndSchemaName);

                return(instance);
            }
        }
        /// <summary>
        /// <para>
        /// Registers an implementation based on SQLite. Use the options to specify the database connection.
        /// </para>
        /// <para>
        /// This overload takes a generic dependency to inject, and a function to get a <see cref="DbConnection"/> from that dependency.
        /// The dependency must be registered separately.
        /// </para>
        /// <para>
        /// The type parameter determines the database connection factory to get from the service provider, which should be registered separately.
        /// </para>
        /// <para>
        /// The implementation will throw if the database does not exist.
        /// The table, however, is created automatically, because different instances of the table may be used in various databases or bounded contexts.
        /// </para>
        /// <para>
        /// To use an in-memory SQLite database, supply a factory that returns an <strong>open</strong> connection, and that always returns the same connection instance.
        /// </para>
        /// </summary>
        /// <param name="getConnectionFromFactory">A function that gets a new <see cref="DbConnection"/> from the registered connection factory.</param>
        /// <param name="connectionString">Written onto produced <see cref="DbConnection"/> objects, if given. Required only if a <see cref="DbConnection"/> is produced without a connection string.</param>
        /// <param name="databaseName">If the connection factory's connection string does not specify the database name, specify it here instead.</param>
        public static ApplicationInstanceIdSourceExtensions.Options UseSqlite <TDatabaseConnectionFactory>(this ApplicationInstanceIdSourceExtensions.Options options,
                                                                                                           Func <TDatabaseConnectionFactory, DbConnection> getConnectionFromFactory, string?connectionString = null,
                                                                                                           string?databaseName = null)
            where TDatabaseConnectionFactory : class
        {
            var firstConnectionIsResolvedSuccessfully = false;

            // Register an IDbConnectionFactory
            ApplicationInstanceIdSourceDbConnectionFactory.Register(options.Services, (Func <TDatabaseConnectionFactory, DbConnection>)CreateDbConnection, connectionString);

            // Register an IApplicationInstanceIdSourceTransactionalExecutor that uses the IDbConnectionFactory
            options.Services.AddTransient <IApplicationInstanceIdSourceTransactionalExecutor, SqlTransactionalExecutor>();

            // Register the IApplicationInstanceIdRenter that uses all of the above
            options.Services.AddTransient(CreateInstance);

            return(options);

            // Local function that creates a new instance
            IApplicationInstanceIdRenter CreateInstance(IServiceProvider serviceProvider)
            {
                var instance = new SqliteApplicationInstanceIdRenter(serviceProvider, databaseName);

                return(instance);
            }

            // Local function that creates a DbConnection from the factory
            DbConnection CreateDbConnection(TDatabaseConnectionFactory factory)
            {
                var connection = getConnectionFromFactory(factory);

                if (!firstConnectionIsResolvedSuccessfully)
                {
                    if (connection.ConnectionString.Contains(":memory:", StringComparison.OrdinalIgnoreCase))
                    {
                        if (connection.State != ConnectionState.Open)
                        {
                            throw new Exception($"To use an in-memory SQLite database, configure a fixed and preopened connection.");
                        }

                        // Not perfect since we cannot use a scope, but we verify what we can
                        var secondConnection = getConnectionFromFactory(factory);
                        if (!ReferenceEquals(connection, secondConnection))
                        {
                            throw new Exception($"To use an in-memory SQLite database, configure a fixed and preopened connection.");
                        }
                    }
                    firstConnectionIsResolvedSuccessfully = true;
                }

                return(connection);
            }
        }
Пример #3
0
        public void RentId_WithOnePriorInvocation_ShouldReturnId2()
        {
            var exceptionHandler      = new OptionalExceptionHandler(null);
            var connectionFactory     = new ApplicationInstanceIdSourceDbConnectionFactory(this.Host.Services, _ => this.CreateDbConnection());
            var transactionalExecutor = new SqlTransactionalExecutor(connectionFactory);
            var otherSource           = new StandardSqlApplicationInstanceIdRenter(this.Host.Services, databaseName: null);

            otherSource.RentId();

            var id = this.Renter.RentId();

            Assert.Equal(2, id);
        }
        /// <summary>
        /// <para>
        /// Registers an implementation based on Standard SQL, which should work with most SQL implementations.
        /// Use the options to specify the database connection.
        /// </para>
        /// <para>
        /// A vendor-specific implementation is preferred over this.
        /// Unlike this one, such an implementation may be able to create the table if it does not exist, and it may be optimized for that specific database.
        /// </para>
        /// <para>
        /// This overload takes a generic dependency to inject, and a function to get a <see cref="DbConnection"/> from that dependency.
        /// The dependency must be registered separately.
        /// </para>
        /// <para>
        /// The type parameter determines the database connection factory to get from the service provider, which should be registered separately.
        /// </para>
        /// <para>
        /// The implementation will throw if the database does not exist, or if the table does not exist.
        /// </para>
        /// </summary>
        /// <param name="getConnectionFromFactory">A function that gets a new <see cref="DbConnection"/> from the registered connection factory.</param>
        /// <param name="connectionString">Written onto produced <see cref="DbConnection"/> objects, if given. Required only if a <see cref="DbConnection"/> is produced without a connection string.</param>
        /// <param name="databaseName">If the connection factory's connection string does not specify the database name, specify it here instead.</param>
        public static ApplicationInstanceIdSourceExtensions.Options UseStandardSql <TDatabaseConnectionFactory>(this ApplicationInstanceIdSourceExtensions.Options options,
                                                                                                                Func <TDatabaseConnectionFactory, DbConnection> getConnectionFromFactory, string?connectionString = null,
                                                                                                                string?databaseName = null)
            where TDatabaseConnectionFactory : class
        {
            // Register an IDbConnectionFactory
            ApplicationInstanceIdSourceDbConnectionFactory.Register(options.Services, getConnectionFromFactory, connectionString);

            // Register an IApplicationInstanceIdSourceTransactionalExecutor that uses the IDbConnectionFactory
            options.Services.AddTransient <IApplicationInstanceIdSourceTransactionalExecutor, SqlTransactionalExecutor>();

            // Register the IApplicationInstanceIdRenter that uses all of the above
            options.Services.AddTransient(CreateInstance);

            return(options);

            // Local function that creates a new instance
            IApplicationInstanceIdRenter CreateInstance(IServiceProvider serviceProvider)
            {
                var instance = new StandardSqlApplicationInstanceIdRenter(serviceProvider, databaseName);

                return(instance);
            }
        }