测试迁入
This commit is contained in:
parent
1b1c4e5683
commit
83b7de52d5
@ -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}
|
||||
|
||||
87
src/JiShe.CollectBus.Cassandra/BaseCassandraRepository.cs
Normal file
87
src/JiShe.CollectBus.Cassandra/BaseCassandraRepository.cs
Normal file
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Cassandra数据库的基础仓储类
|
||||
/// 提供了通用的CRUD操作方法,所有具体的仓储类都应该继承此类
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类型</typeparam>
|
||||
public abstract class BaseCassandraRepository<T> where T : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Cassandra数据库会话
|
||||
/// 用于执行CQL查询和操作
|
||||
/// </summary>
|
||||
protected readonly ISession Session;
|
||||
|
||||
/// <summary>
|
||||
/// Cassandra映射器
|
||||
/// 用于对象关系映射(ORM),简化实体与数据库表之间的转换
|
||||
/// </summary>
|
||||
protected readonly IMapper Mapper;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// 初始化数据库会话和映射器
|
||||
/// </summary>
|
||||
/// <param name="cassandraService">Cassandra连接工厂</param>
|
||||
protected BaseCassandraRepository(ICassandraService cassandraService)
|
||||
{
|
||||
Session = cassandraService.GetSession();
|
||||
Mapper = new Mapper(Session);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据ID获取单个实体
|
||||
/// </summary>
|
||||
/// <param name="id">实体ID</param>
|
||||
/// <returns>返回找到的实体,如果未找到则返回null</returns>
|
||||
public async Task<T> GetByIdAsync(string id)
|
||||
{
|
||||
return await Mapper.SingleOrDefaultAsync<T>($"WHERE id = ?", id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有实体
|
||||
/// </summary>
|
||||
/// <returns>返回实体集合</returns>
|
||||
public async Task<IEnumerable<T>> GetAllAsync()
|
||||
{
|
||||
return await Mapper.FetchAsync<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插入新实体
|
||||
/// </summary>
|
||||
/// <param name="entity">要插入的实体</param>
|
||||
public async Task InsertAsync(T entity)
|
||||
{
|
||||
await Mapper.InsertAsync(entity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新现有实体
|
||||
/// </summary>
|
||||
/// <param name="entity">要更新的实体</param>
|
||||
public async Task UpdateAsync(T entity)
|
||||
{
|
||||
await Mapper.UpdateAsync(entity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据ID删除实体
|
||||
/// </summary>
|
||||
/// <param name="id">要删除的实体ID</param>
|
||||
public async Task DeleteAsync(string id)
|
||||
{
|
||||
await Mapper.DeleteAsync<T>($"WHERE id = ?", id);
|
||||
}
|
||||
}
|
||||
}
|
||||
45
src/JiShe.CollectBus.Cassandra/CassandraConfiguration.cs
Normal file
45
src/JiShe.CollectBus.Cassandra/CassandraConfiguration.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JiShe.CollectBus.Cassandra
|
||||
{
|
||||
/// <summary>
|
||||
/// Cassandra数据库配置类
|
||||
/// 用于存储和管理Cassandra数据库的连接配置信息
|
||||
/// </summary>
|
||||
public class CassandraConfiguration
|
||||
{
|
||||
/// <summary>
|
||||
/// Cassandra集群的节点地址列表
|
||||
/// 可以配置多个节点地址,用于实现高可用和负载均衡
|
||||
/// </summary>
|
||||
public string[] ContactPoints { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Cassandra的键空间名称
|
||||
/// 键空间是Cassandra中数据组织的最高级别容器
|
||||
/// </summary>
|
||||
public string Keyspace { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Cassandra数据库的用户名
|
||||
/// 用于身份验证
|
||||
/// </summary>
|
||||
public string Username { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Cassandra数据库的密码
|
||||
/// 用于身份验证
|
||||
/// </summary>
|
||||
public string Password { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Cassandra数据库的端口号
|
||||
/// 默认值为9042,这是Cassandra的默认CQL端口
|
||||
/// </summary>
|
||||
public int Port { get; set; } = 9042;
|
||||
}
|
||||
}
|
||||
40
src/JiShe.CollectBus.Cassandra/CassandraService.cs
Normal file
40
src/JiShe.CollectBus.Cassandra/CassandraService.cs
Normal file
@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// CassandraService
|
||||
/// </summary>
|
||||
/// <param name="configuration"></param>
|
||||
public CassandraService(IOptions<CassandraConfiguration> configuration)
|
||||
{
|
||||
_configuration = configuration.Value;
|
||||
GetSession();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取Cassandra数据库会话
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/JiShe.CollectBus.Cassandra/CollectBusCassandraModule.cs
Normal file
13
src/JiShe.CollectBus.Cassandra/CollectBusCassandraModule.cs
Normal file
@ -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<CassandraConfiguration>(context.Services.GetConfiguration().GetSection("Cassandra"));
|
||||
}
|
||||
}
|
||||
}
|
||||
9
src/JiShe.CollectBus.Cassandra/ICassandraService.cs
Normal file
9
src/JiShe.CollectBus.Cassandra/ICassandraService.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using Cassandra;
|
||||
|
||||
namespace JiShe.CollectBus.Cassandra
|
||||
{
|
||||
public interface ICassandraService
|
||||
{
|
||||
ISession GetSession();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CassandraCSharpDriver" Version="3.22.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
|
||||
<PackageReference Include="Volo.Abp.Core" Version="8.3.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Models\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
x
Reference in New Issue
Block a user