diff --git a/JiShe.CollectBus.sln b/JiShe.CollectBus.sln index c26f3da..9c67aae 100644 --- a/JiShe.CollectBus.sln +++ b/JiShe.CollectBus.sln @@ -37,6 +37,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JiShe.CollectBus.IoTDBProvi EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JiShe.CollectBus.Protocol.Test", "src\JiShe.CollectBus.Protocol.Test\JiShe.CollectBus.Protocol.Test.csproj", "{A377955E-7EA1-6F29-8CF7-774569E93925}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JiShe.CollectBus.Cassandra", "src\JiShe.CollectBus.Cassandra\JiShe.CollectBus.Cassandra.csproj", "{443B4549-0AC0-4493-8F3E-49C83225DD76}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -107,6 +109,10 @@ Global {A377955E-7EA1-6F29-8CF7-774569E93925}.Debug|Any CPU.Build.0 = Debug|Any CPU {A377955E-7EA1-6F29-8CF7-774569E93925}.Release|Any CPU.ActiveCfg = Release|Any CPU {A377955E-7EA1-6F29-8CF7-774569E93925}.Release|Any CPU.Build.0 = Release|Any CPU + {443B4549-0AC0-4493-8F3E-49C83225DD76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {443B4549-0AC0-4493-8F3E-49C83225DD76}.Debug|Any CPU.Build.0 = Debug|Any CPU + {443B4549-0AC0-4493-8F3E-49C83225DD76}.Release|Any CPU.ActiveCfg = Release|Any CPU + {443B4549-0AC0-4493-8F3E-49C83225DD76}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -128,6 +134,7 @@ Global {F0288175-F0EC-48BD-945F-CF1512850943} = {649A3FFA-182F-4E56-9717-E6A9A2BEC545} {A3F3C092-0A25-450B-BF6A-5983163CBEF5} = {649A3FFA-182F-4E56-9717-E6A9A2BEC545} {A377955E-7EA1-6F29-8CF7-774569E93925} = {649A3FFA-182F-4E56-9717-E6A9A2BEC545} + {443B4549-0AC0-4493-8F3E-49C83225DD76} = {649A3FFA-182F-4E56-9717-E6A9A2BEC545} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4324B3B4-B60B-4E3C-91D8-59576B4E26DD} diff --git a/src/JiShe.CollectBus.Cassandra/BaseCassandraRepository.cs b/src/JiShe.CollectBus.Cassandra/BaseCassandraRepository.cs new file mode 100644 index 0000000..67252e4 --- /dev/null +++ b/src/JiShe.CollectBus.Cassandra/BaseCassandraRepository.cs @@ -0,0 +1,87 @@ +using Cassandra.Mapping; +using Cassandra; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JiShe.CollectBus.Cassandra +{ + /// + /// Cassandra数据库的基础仓储类 + /// 提供了通用的CRUD操作方法,所有具体的仓储类都应该继承此类 + /// + /// 实体类型 + public abstract class BaseCassandraRepository where T : class + { + /// + /// Cassandra数据库会话 + /// 用于执行CQL查询和操作 + /// + protected readonly ISession Session; + + /// + /// Cassandra映射器 + /// 用于对象关系映射(ORM),简化实体与数据库表之间的转换 + /// + protected readonly IMapper Mapper; + + /// + /// 构造函数 + /// 初始化数据库会话和映射器 + /// + /// Cassandra连接工厂 + protected BaseCassandraRepository(ICassandraService cassandraService) + { + Session = cassandraService.GetSession(); + Mapper = new Mapper(Session); + } + + /// + /// 根据ID获取单个实体 + /// + /// 实体ID + /// 返回找到的实体,如果未找到则返回null + public async Task GetByIdAsync(string id) + { + return await Mapper.SingleOrDefaultAsync($"WHERE id = ?", id); + } + + /// + /// 获取所有实体 + /// + /// 返回实体集合 + public async Task> GetAllAsync() + { + return await Mapper.FetchAsync(); + } + + /// + /// 插入新实体 + /// + /// 要插入的实体 + public async Task InsertAsync(T entity) + { + await Mapper.InsertAsync(entity); + } + + /// + /// 更新现有实体 + /// + /// 要更新的实体 + public async Task UpdateAsync(T entity) + { + await Mapper.UpdateAsync(entity); + } + + /// + /// 根据ID删除实体 + /// + /// 要删除的实体ID + public async Task DeleteAsync(string id) + { + await Mapper.DeleteAsync($"WHERE id = ?", id); + } + } +} diff --git a/src/JiShe.CollectBus.Cassandra/CassandraConfiguration.cs b/src/JiShe.CollectBus.Cassandra/CassandraConfiguration.cs new file mode 100644 index 0000000..8fea3c1 --- /dev/null +++ b/src/JiShe.CollectBus.Cassandra/CassandraConfiguration.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JiShe.CollectBus.Cassandra +{ + /// + /// Cassandra数据库配置类 + /// 用于存储和管理Cassandra数据库的连接配置信息 + /// + public class CassandraConfiguration + { + /// + /// Cassandra集群的节点地址列表 + /// 可以配置多个节点地址,用于实现高可用和负载均衡 + /// + public string[] ContactPoints { get; set; } + + /// + /// Cassandra的键空间名称 + /// 键空间是Cassandra中数据组织的最高级别容器 + /// + public string Keyspace { get; set; } + + /// + /// Cassandra数据库的用户名 + /// 用于身份验证 + /// + public string Username { get; set; } + + /// + /// Cassandra数据库的密码 + /// 用于身份验证 + /// + public string Password { get; set; } + + /// + /// Cassandra数据库的端口号 + /// 默认值为9042,这是Cassandra的默认CQL端口 + /// + public int Port { get; set; } = 9042; + } +} diff --git a/src/JiShe.CollectBus.Cassandra/CassandraService.cs b/src/JiShe.CollectBus.Cassandra/CassandraService.cs new file mode 100644 index 0000000..8836235 --- /dev/null +++ b/src/JiShe.CollectBus.Cassandra/CassandraService.cs @@ -0,0 +1,40 @@ +using System.Diagnostics.Metrics; +using Cassandra; +using Microsoft.Extensions.Options; +using Volo.Abp.DependencyInjection; + +namespace JiShe.CollectBus.Cassandra +{ + public class CassandraService : ICassandraService, ISingletonDependency + { + private readonly CassandraConfiguration _configuration; + public ISession Instance { get; set; } = default; + + /// + /// CassandraService + /// + /// + public CassandraService(IOptions configuration) + { + _configuration = configuration.Value; + GetSession(); + } + + /// + /// 获取Cassandra数据库会话 + /// + /// + public ISession GetSession() + { + var cluster = Cluster.Builder() + .AddContactPoints(_configuration.ContactPoints) + .WithPort(_configuration.Port) + .WithCredentials(_configuration.Username, _configuration.Password) + .Build(); + + Instance = cluster.Connect(_configuration.Keyspace); + + return Instance; + } + } +} diff --git a/src/JiShe.CollectBus.Cassandra/CollectBusCassandraModule.cs b/src/JiShe.CollectBus.Cassandra/CollectBusCassandraModule.cs new file mode 100644 index 0000000..e7c2c69 --- /dev/null +++ b/src/JiShe.CollectBus.Cassandra/CollectBusCassandraModule.cs @@ -0,0 +1,13 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Modularity; + +namespace JiShe.CollectBus.Cassandra +{ + public class CollectBusCassandraModule : AbpModule + { + public override void ConfigureServices(ServiceConfigurationContext context) + { + context.Services.Configure(context.Services.GetConfiguration().GetSection("Cassandra")); + } + } +} diff --git a/src/JiShe.CollectBus.Cassandra/ICassandraService.cs b/src/JiShe.CollectBus.Cassandra/ICassandraService.cs new file mode 100644 index 0000000..70eb5d6 --- /dev/null +++ b/src/JiShe.CollectBus.Cassandra/ICassandraService.cs @@ -0,0 +1,9 @@ +using Cassandra; + +namespace JiShe.CollectBus.Cassandra +{ + public interface ICassandraService + { + ISession GetSession(); + } +} diff --git a/src/JiShe.CollectBus.Cassandra/JiShe.CollectBus.Cassandra.csproj b/src/JiShe.CollectBus.Cassandra/JiShe.CollectBus.Cassandra.csproj new file mode 100644 index 0000000..5a8b22d --- /dev/null +++ b/src/JiShe.CollectBus.Cassandra/JiShe.CollectBus.Cassandra.csproj @@ -0,0 +1,19 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + +