diff --git a/src/Cnblogs.Architecture.Ddd.Cqrs.Abstractions/Cnblogs.Architecture.Ddd.Cqrs.Abstractions.csproj b/src/Cnblogs.Architecture.Ddd.Cqrs.Abstractions/Cnblogs.Architecture.Ddd.Cqrs.Abstractions.csproj index 1113372..c53022e 100644 --- a/src/Cnblogs.Architecture.Ddd.Cqrs.Abstractions/Cnblogs.Architecture.Ddd.Cqrs.Abstractions.csproj +++ b/src/Cnblogs.Architecture.Ddd.Cqrs.Abstractions/Cnblogs.Architecture.Ddd.Cqrs.Abstractions.csproj @@ -26,8 +26,8 @@ - - + + diff --git a/src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore.csproj b/src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore.csproj index 7dbbbcf..3f50fb9 100644 --- a/src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore.csproj +++ b/src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore.csproj @@ -8,15 +8,15 @@ - + - + - + diff --git a/src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/CqrsResult.cs b/src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/CqrsResult.cs index 8dfe755..1338254 100644 --- a/src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/CqrsResult.cs +++ b/src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/CqrsResult.cs @@ -6,7 +6,8 @@ namespace Cnblogs.Architecture.Ddd.Cqrs.AspNetCore; /// /// Send object as json and append X-Cqrs-Version header /// -/// +/// Response body. +/// to use. public class CqrsResult(object commandResponse, JsonSerializerOptions? options = null) : IResult { /// diff --git a/src/Cnblogs.Architecture.Ddd.Cqrs.Dapper.Clickhouse/ClickhouseDbConnectionFactory.cs b/src/Cnblogs.Architecture.Ddd.Cqrs.Dapper.Clickhouse/ClickhouseDbConnectionFactory.cs index 6021841..e2e43b7 100644 --- a/src/Cnblogs.Architecture.Ddd.Cqrs.Dapper.Clickhouse/ClickhouseDbConnectionFactory.cs +++ b/src/Cnblogs.Architecture.Ddd.Cqrs.Dapper.Clickhouse/ClickhouseDbConnectionFactory.cs @@ -1,5 +1,5 @@ using System.Data; -using ClickHouse.Client.ADO; +using ClickHouse.Client; using Cnblogs.Architecture.Ddd.Infrastructure.Dapper; namespace Cnblogs.Architecture.Ddd.Cqrs.Dapper.Clickhouse; @@ -7,22 +7,11 @@ namespace Cnblogs.Architecture.Ddd.Cqrs.Dapper.Clickhouse; /// /// Clickhouse connection factory. /// -public class ClickhouseDbConnectionFactory : IDbConnectionFactory +public class ClickhouseDbConnectionFactory(IClickHouseDataSource dataSource) : IDbConnectionFactory { - private readonly string _connectionString; - - /// - /// Create a clickhouse connection factory. - /// - /// The connection string. - public ClickhouseDbConnectionFactory(string connectionString) - { - _connectionString = connectionString; - } - /// public IDbConnection CreateDbConnection() { - return new ClickHouseConnection(_connectionString); + return dataSource.CreateConnection(); } } diff --git a/src/Cnblogs.Architecture.Ddd.Cqrs.Dapper.Clickhouse/DapperConfigurationBuilderExtension.cs b/src/Cnblogs.Architecture.Ddd.Cqrs.Dapper.Clickhouse/DapperConfigurationBuilderExtension.cs index 590eace..bf5d921 100644 --- a/src/Cnblogs.Architecture.Ddd.Cqrs.Dapper.Clickhouse/DapperConfigurationBuilderExtension.cs +++ b/src/Cnblogs.Architecture.Ddd.Cqrs.Dapper.Clickhouse/DapperConfigurationBuilderExtension.cs @@ -21,7 +21,8 @@ public static void UseClickhouse( string connectionString) where TContext : ClickhouseDapperContext { - builder.UseDbConnectionFactory(new ClickhouseDbConnectionFactory(connectionString)); + builder.UseDbConnectionFactory(); + builder.Services.AddClickHouseDataSource(connectionString); builder.Services.AddSingleton(new ClickhouseContextOptions(connectionString)); builder.Services.Configure(x => x.Add()); builder.Services.AddHostedService(); diff --git a/src/Cnblogs.Architecture.Ddd.Cqrs.Dapper.SqlServer/Cnblogs.Architecture.Ddd.Cqrs.Dapper.SqlServer.csproj b/src/Cnblogs.Architecture.Ddd.Cqrs.Dapper.SqlServer/Cnblogs.Architecture.Ddd.Cqrs.Dapper.SqlServer.csproj index 2385ae8..d5ac1be 100644 --- a/src/Cnblogs.Architecture.Ddd.Cqrs.Dapper.SqlServer/Cnblogs.Architecture.Ddd.Cqrs.Dapper.SqlServer.csproj +++ b/src/Cnblogs.Architecture.Ddd.Cqrs.Dapper.SqlServer/Cnblogs.Architecture.Ddd.Cqrs.Dapper.SqlServer.csproj @@ -11,7 +11,7 @@ - + diff --git a/src/Cnblogs.Architecture.Ddd.Cqrs.Dapper/DapperConfigurationBuilder.cs b/src/Cnblogs.Architecture.Ddd.Cqrs.Dapper/DapperConfigurationBuilder.cs index 95cb79a..62a3542 100644 --- a/src/Cnblogs.Architecture.Ddd.Cqrs.Dapper/DapperConfigurationBuilder.cs +++ b/src/Cnblogs.Architecture.Ddd.Cqrs.Dapper/DapperConfigurationBuilder.cs @@ -29,10 +29,23 @@ public DapperConfigurationBuilder(IServiceCollection services) /// 工厂对象。 /// 工厂类型。 public void UseDbConnectionFactory(TFactory factory) - where TFactory : IDbConnectionFactory + where TFactory : class, IDbConnectionFactory { + Services.AddSingleton(factory); Services.Configure( - c => c.AddDbConnectionFactory(_dapperContextTypeName, factory)); + c => c.AddDbConnectionFactory(_dapperContextTypeName, typeof(TFactory))); + } + + /// + /// Add as and get instance from DI when used. + /// + /// The factory type. + public void UseDbConnectionFactory() + where TFactory : class, IDbConnectionFactory + { + Services.AddSingleton(); + Services.Configure( + c => c.AddDbConnectionFactory(_dapperContextTypeName, typeof(TFactory))); } /// diff --git a/src/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent.csproj b/src/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent.csproj index 090e93e..f135b69 100644 --- a/src/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent.csproj +++ b/src/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent.csproj @@ -16,7 +16,7 @@ - + diff --git a/src/Cnblogs.Architecture.Ddd.Domain.Abstractions/IUnitOfWork.cs b/src/Cnblogs.Architecture.Ddd.Domain.Abstractions/IUnitOfWork.cs index 85e2897..d993245 100644 --- a/src/Cnblogs.Architecture.Ddd.Domain.Abstractions/IUnitOfWork.cs +++ b/src/Cnblogs.Architecture.Ddd.Domain.Abstractions/IUnitOfWork.cs @@ -20,7 +20,6 @@ public interface IUnitOfWork /// 添加实体,调用 后才会写入数据库。 /// /// 要添加实体。 - /// 实体类型。 /// 被添加的实体。 TEntity Add(TEntity entity); @@ -28,7 +27,6 @@ public interface IUnitOfWork /// 更新实体,调用 后才会写入数据库。 /// /// 要更新的实体。 - /// 实体类型。 /// 被更新的实体。 TEntity Update(TEntity entity); @@ -36,7 +34,6 @@ public interface IUnitOfWork /// 删除实体,调用 后才会写入数据库。 /// /// 要删除的实体。 - /// 实体类型。 /// TEntity Delete(TEntity entity); @@ -53,4 +50,4 @@ public interface IUnitOfWork /// 。 /// 提交是否成功。 Task SaveEntitiesAsync(CancellationToken cancellationToken = default); -} \ No newline at end of file +} diff --git a/src/Cnblogs.Architecture.Ddd.Infrastructure.CacheProviders.InMemory/Cnblogs.Architecture.Ddd.Infrastructure.CacheProviders.InMemory.csproj b/src/Cnblogs.Architecture.Ddd.Infrastructure.CacheProviders.InMemory/Cnblogs.Architecture.Ddd.Infrastructure.CacheProviders.InMemory.csproj index 32e6024..e7d4b1e 100644 --- a/src/Cnblogs.Architecture.Ddd.Infrastructure.CacheProviders.InMemory/Cnblogs.Architecture.Ddd.Infrastructure.CacheProviders.InMemory.csproj +++ b/src/Cnblogs.Architecture.Ddd.Infrastructure.CacheProviders.InMemory/Cnblogs.Architecture.Ddd.Infrastructure.CacheProviders.InMemory.csproj @@ -16,7 +16,7 @@ - + diff --git a/src/Cnblogs.Architecture.Ddd.Infrastructure.CacheProviders.Redis/Cnblogs.Architecture.Ddd.Infrastructure.CacheProviders.Redis.csproj b/src/Cnblogs.Architecture.Ddd.Infrastructure.CacheProviders.Redis/Cnblogs.Architecture.Ddd.Infrastructure.CacheProviders.Redis.csproj index 318a3c0..d45e4c7 100644 --- a/src/Cnblogs.Architecture.Ddd.Infrastructure.CacheProviders.Redis/Cnblogs.Architecture.Ddd.Infrastructure.CacheProviders.Redis.csproj +++ b/src/Cnblogs.Architecture.Ddd.Infrastructure.CacheProviders.Redis/Cnblogs.Architecture.Ddd.Infrastructure.CacheProviders.Redis.csproj @@ -12,7 +12,7 @@ - + diff --git a/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper.Clickhouse/ClickhouseDapperContext.cs b/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper.Clickhouse/ClickhouseDapperContext.cs index 5d648ba..d95ec15 100644 --- a/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper.Clickhouse/ClickhouseDapperContext.cs +++ b/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper.Clickhouse/ClickhouseDapperContext.cs @@ -15,10 +15,12 @@ public abstract class ClickhouseDapperContext : DapperContext /// /// The underlying collection. /// The options used for this context. + /// The service provider to use. protected ClickhouseDapperContext( IOptions dbConnectionFactoryCollection, - ClickhouseContextOptions options) - : base(dbConnectionFactoryCollection) + ClickhouseContextOptions options, + IServiceProvider serviceProvider) + : base(dbConnectionFactoryCollection, serviceProvider) { _options = options; } diff --git a/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper.Clickhouse/Cnblogs.Architecture.Ddd.Infrastructure.Dapper.Clickhouse.csproj b/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper.Clickhouse/Cnblogs.Architecture.Ddd.Infrastructure.Dapper.Clickhouse.csproj index bfd4182..c4692f5 100644 --- a/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper.Clickhouse/Cnblogs.Architecture.Ddd.Infrastructure.Dapper.Clickhouse.csproj +++ b/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper.Clickhouse/Cnblogs.Architecture.Ddd.Infrastructure.Dapper.Clickhouse.csproj @@ -13,7 +13,7 @@ - + diff --git a/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper/Cnblogs.Architecture.Ddd.Infrastructure.Dapper.csproj b/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper/Cnblogs.Architecture.Ddd.Infrastructure.Dapper.csproj index 9be1738..e2f158b 100644 --- a/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper/Cnblogs.Architecture.Ddd.Infrastructure.Dapper.csproj +++ b/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper/Cnblogs.Architecture.Ddd.Infrastructure.Dapper.csproj @@ -9,7 +9,7 @@ - + @@ -17,7 +17,7 @@ - + diff --git a/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper/DapperContext.cs b/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper/DapperContext.cs index 77acd16..effe5be 100644 --- a/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper/DapperContext.cs +++ b/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper/DapperContext.cs @@ -1,5 +1,5 @@ using System.Data; - +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; namespace Cnblogs.Architecture.Ddd.Infrastructure.Dapper; @@ -13,9 +13,13 @@ public abstract class DapperContext /// 创建一个 DapperContext。 /// /// 数据库连接工厂集合。 - protected DapperContext(IOptions dbConnectionFactoryCollection) + /// The service provider to get connection factory + protected DapperContext(IOptions dbConnectionFactoryCollection, IServiceProvider sp) { - DbConnectionFactory = dbConnectionFactoryCollection.Value.GetFactory(GetType().Name); + var type = dbConnectionFactoryCollection.Value.GetFactory(GetType().Name); + DbConnectionFactory = sp.GetRequiredService(type) as IDbConnectionFactory + ?? throw new InvalidOperationException( + $"No DbConnectionFactory(type: {type.Name}) configured."); } /// diff --git a/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper/DbConnectionFactoryCollection.cs b/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper/DbConnectionFactoryCollection.cs index 3b25a93..9af85ef 100644 --- a/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper/DbConnectionFactoryCollection.cs +++ b/src/Cnblogs.Architecture.Ddd.Infrastructure.Dapper/DbConnectionFactoryCollection.cs @@ -5,7 +5,7 @@ /// public class DbConnectionFactoryCollection { - private readonly Dictionary _factories = new(); + private readonly Dictionary _factories = new(); /// /// 添加数据库连接工厂。 @@ -13,12 +13,12 @@ public class DbConnectionFactoryCollection /// 名称。 /// 工厂示例。 /// Throw when already been registered with other factory. - public void AddDbConnectionFactory(string name, IDbConnectionFactory factory) + public void AddDbConnectionFactory(string name, Type factory) { if (_factories.TryGetValue(name, out var value)) { throw new InvalidOperationException( - $"The dapper context already configured with db connection factory: {value.GetType().Name}"); + $"The dapper context already configured with db connection factory: {value.Name}"); } _factories.Add(name, factory); @@ -29,7 +29,7 @@ public void AddDbConnectionFactory(string name, IDbConnectionFactory factory) /// /// 名称。 /// 工厂示例。 - public IDbConnectionFactory GetFactory(string name) + public Type GetFactory(string name) { return _factories[name]; } diff --git a/src/Cnblogs.Architecture.Ddd.Infrastructure.EntityFramework/Cnblogs.Architecture.Ddd.Infrastructure.EntityFramework.csproj b/src/Cnblogs.Architecture.Ddd.Infrastructure.EntityFramework/Cnblogs.Architecture.Ddd.Infrastructure.EntityFramework.csproj index dca9848..afb3999 100644 --- a/src/Cnblogs.Architecture.Ddd.Infrastructure.EntityFramework/Cnblogs.Architecture.Ddd.Infrastructure.EntityFramework.csproj +++ b/src/Cnblogs.Architecture.Ddd.Infrastructure.EntityFramework/Cnblogs.Architecture.Ddd.Infrastructure.EntityFramework.csproj @@ -9,7 +9,7 @@ - + diff --git a/src/Cnblogs.Architecture.Ddd.Infrastructure.MongoDb/Cnblogs.Architecture.Ddd.Infrastructure.MongoDb.csproj b/src/Cnblogs.Architecture.Ddd.Infrastructure.MongoDb/Cnblogs.Architecture.Ddd.Infrastructure.MongoDb.csproj index f4e4317..a294ad9 100644 --- a/src/Cnblogs.Architecture.Ddd.Infrastructure.MongoDb/Cnblogs.Architecture.Ddd.Infrastructure.MongoDb.csproj +++ b/src/Cnblogs.Architecture.Ddd.Infrastructure.MongoDb/Cnblogs.Architecture.Ddd.Infrastructure.MongoDb.csproj @@ -10,7 +10,7 @@ - + diff --git a/test/Cnblogs.Architecture.IntegrationTests/Cnblogs.Architecture.IntegrationTests.csproj b/test/Cnblogs.Architecture.IntegrationTests/Cnblogs.Architecture.IntegrationTests.csproj index 685c378..5c8fb03 100644 --- a/test/Cnblogs.Architecture.IntegrationTests/Cnblogs.Architecture.IntegrationTests.csproj +++ b/test/Cnblogs.Architecture.IntegrationTests/Cnblogs.Architecture.IntegrationTests.csproj @@ -1,23 +1,17 @@ - - - + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all - - - - - - - + + + @@ -29,14 +23,4 @@ - - - - - - - - - - diff --git a/test/Cnblogs.Architecture.TestShared/Cnblogs.Architecture.TestShared.csproj b/test/Cnblogs.Architecture.TestShared/Cnblogs.Architecture.TestShared.csproj index e633a27..2022736 100644 --- a/test/Cnblogs.Architecture.TestShared/Cnblogs.Architecture.TestShared.csproj +++ b/test/Cnblogs.Architecture.TestShared/Cnblogs.Architecture.TestShared.csproj @@ -1,6 +1,16 @@ - - - - - \ No newline at end of file + + + + + + + + + + + + + + + diff --git a/test/Cnblogs.Architecture.UnitTests/Cnblogs.Architecture.UnitTests.csproj b/test/Cnblogs.Architecture.UnitTests/Cnblogs.Architecture.UnitTests.csproj index 3880bba..77632a1 100644 --- a/test/Cnblogs.Architecture.UnitTests/Cnblogs.Architecture.UnitTests.csproj +++ b/test/Cnblogs.Architecture.UnitTests/Cnblogs.Architecture.UnitTests.csproj @@ -1,14 +1,12 @@ - - - - + runtime; build; native; contentfiles; analyzers; buildtransitive all - + + runtime; build; native; contentfiles; analyzers; buildtransitive all