Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dameng database support to the CAP project. #1658

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CAP.sln
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.Dashboard.Jwt", "sam
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.Dashboard.K8s", "src\DotNetCore.CAP.Dashboard.K8s\DotNetCore.CAP.Dashboard.K8s.csproj", "{48655118-CEC3-4BD9-B510-64C1195C2729}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetCore.CAP.DM", "src\DotNetCore.CAP.DM\DotNetCore.CAP.DM.csproj", "{2959A553-C3CB-434E-8BF2-7AAFEB3B14DF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.RabbitMQ.DM", "samples\Sample.RabbitMQ.DM\Sample.RabbitMQ.DM.csproj", "{2DB75BFA-3663-48E1-9CA1-5E24459F907A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -225,6 +229,14 @@ Global
{48655118-CEC3-4BD9-B510-64C1195C2729}.Debug|Any CPU.Build.0 = Debug|Any CPU
{48655118-CEC3-4BD9-B510-64C1195C2729}.Release|Any CPU.ActiveCfg = Release|Any CPU
{48655118-CEC3-4BD9-B510-64C1195C2729}.Release|Any CPU.Build.0 = Release|Any CPU
{2959A553-C3CB-434E-8BF2-7AAFEB3B14DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2959A553-C3CB-434E-8BF2-7AAFEB3B14DF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2959A553-C3CB-434E-8BF2-7AAFEB3B14DF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2959A553-C3CB-434E-8BF2-7AAFEB3B14DF}.Release|Any CPU.Build.0 = Release|Any CPU
{2DB75BFA-3663-48E1-9CA1-5E24459F907A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2DB75BFA-3663-48E1-9CA1-5E24459F907A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2DB75BFA-3663-48E1-9CA1-5E24459F907A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2DB75BFA-3663-48E1-9CA1-5E24459F907A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -262,6 +274,8 @@ Global
{D9681967-DAC2-43EF-999F-3727F1046711} = {C09CDAB0-6DD4-46E9-B7F3-3EF2A4741EA0}
{F739A8C9-565F-4B1D-8F91-FEE056C03FBD} = {3A6B6931-A123-477A-9469-8B468B5385AF}
{48655118-CEC3-4BD9-B510-64C1195C2729} = {9B2AE124-6636-4DE9-83A3-70360DABD0C4}
{2959A553-C3CB-434E-8BF2-7AAFEB3B14DF} = {9B2AE124-6636-4DE9-83A3-70360DABD0C4}
{2DB75BFA-3663-48E1-9CA1-5E24459F907A} = {3A6B6931-A123-477A-9469-8B468B5385AF}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2E70565D-94CF-40B4-BFE1-AC18D5F736AB}
Expand Down
80 changes: 80 additions & 0 deletions samples/Sample.RabbitMQ.DM/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Dapper;
using Dm;
using DotNetCore.CAP;
using Microsoft.AspNetCore.Mvc;

namespace Sample.RabbitMQ.DM.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
private readonly ICapPublisher _capBus;

public ValuesController(ICapPublisher capPublisher)
{
_capBus = capPublisher;
}

[Route("~/control/start")]
public async Task<IActionResult> Start([FromServices] IBootstrapper bootstrapper)
{
await bootstrapper.BootstrapAsync();
return Ok();
}

[Route("~/control/stop")]
public async Task<IActionResult> Stop([FromServices] IBootstrapper bootstrapper)
{
await bootstrapper.DisposeAsync();
return Ok();
}

[Route("~/without/transaction")]
public async Task<IActionResult> WithoutTransactionAsync()
{
await _capBus.PublishAsync("sample.rabbitmq.dm", DateTime.Now, cancellationToken: HttpContext.RequestAborted);

return Ok();
}

[Route("~/delay/{delaySeconds:int}")]
public async Task<IActionResult> Delay(int delaySeconds)
{
await _capBus.PublishDelayAsync(TimeSpan.FromSeconds(delaySeconds), "sample.rabbitmq.test", $"publish time:{DateTime.Now}, delay seconds:{delaySeconds}");

return Ok();
}

[Route("~/adonet/transaction")]
public async Task<IActionResult> AdonetWithTransaction()
{
using (var connection = new DmConnection(Startup.ConnectionString))
{
using var transaction = await connection.BeginTransactionAsync(_capBus, true);
await connection.ExecuteAsync(@"insert into ""cap"".TEST(NAME) values('test')", transaction: transaction);
await _capBus.PublishAsync("sample.rabbitmq.dm", DateTime.Now);
}
return Ok();
}

//[Route("~/ef/transaction")]
//public async Task<IActionResult> EntityFrameworkWithTransaction([FromServices] AppDbContext dbContext)
//{
// using (var trans = await dbContext.Database.BeginTransactionAsync(_capBus, autoCommit: false))
// {
// await dbContext.Persons.AddAsync(new Person() { Name = "ef.transaction" });
// await _capBus.PublishAsync("sample.rabbitmq.dm", DateTime.Now);
// await dbContext.SaveChangesAsync();
// await trans.CommitAsync();
// }
// return Ok();
//}

[NonAction]
[CapSubscribe("sample.rabbitmq.dm")]
public void Subscriber(DateTime time)
{
Console.WriteLine("Publishing time:" + time);
}
}
}
17 changes: 17 additions & 0 deletions samples/Sample.RabbitMQ.DM/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Sample.RabbitMQ.DM
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
21 changes: 21 additions & 0 deletions samples/Sample.RabbitMQ.DM/Sample.RabbitMQ.DM.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.66" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\DotNetCore.CAP.Dashboard.K8s\DotNetCore.CAP.Dashboard.K8s.csproj" />
<ProjectReference Include="..\..\src\DotNetCore.CAP.Dashboard\DotNetCore.CAP.Dashboard.csproj" />
<ProjectReference Include="..\..\src\DotNetCore.CAP.DM\DotNetCore.CAP.DM.csproj" />
<ProjectReference Include="..\..\src\DotNetCore.CAP.RabbitMQ\DotNetCore.CAP.RabbitMQ.csproj" />
<ProjectReference Include="..\..\src\DotNetCore.CAP\DotNetCore.CAP.csproj" />
</ItemGroup>

</Project>
29 changes: 29 additions & 0 deletions samples/Sample.RabbitMQ.DM/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace Sample.RabbitMQ.DM
{
public class Startup
{
public const string ConnectionString = "";

public void ConfigureServices(IServiceCollection services)
{
services.AddCap(x =>
{
//x.UseStorageLock=true;
x.UseDM(ConnectionString);
x.UseRabbitMQ("localhost");
x.UseDashboard();
});

services.AddControllers();
}

public void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
8 changes: 8 additions & 0 deletions samples/Sample.RabbitMQ.DM/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions samples/Sample.RabbitMQ.DM/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
27 changes: 27 additions & 0 deletions src/DotNetCore.CAP.DM/CAP.DmCapOptionsExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using DotNetCore.CAP.DM;
using DotNetCore.CAP.Persistence;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;


namespace DotNetCore.CAP
{
internal class DMCapOptionsExtension : ICapOptionsExtension
{
private readonly Action<DMOptions> _configure;

public DMCapOptionsExtension(Action<DMOptions> configure)
{
_configure = configure;
}

public void AddServices(IServiceCollection services)
{
services.AddSingleton(new CapStorageMarkerService("DM"));
services.AddSingleton<IDataStorage, DMDataStorage>();
services.AddSingleton<IStorageInitializer, DMStorageInitializer>();
services.Configure(_configure);
services.AddSingleton<IConfigureOptions<DMOptions>, ConfigureDMOptions>();
}
}
}
41 changes: 41 additions & 0 deletions src/DotNetCore.CAP.DM/CAP.DmOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using DotNetCore.CAP.Internal;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;


namespace DotNetCore.CAP
{
public class DMOptions: EFOptions
{
/// <summary>
/// Gets or sets the database's connection string that will be used to store database entities.
/// </summary>
public string ConnectionString { get; set; } = default!;
}
internal class ConfigureDMOptions : IConfigureOptions<DMOptions>
{
private readonly IServiceScopeFactory _serviceScopeFactory;

public ConfigureDMOptions(IServiceScopeFactory serviceScopeFactory)
{
_serviceScopeFactory = serviceScopeFactory;
}

public void Configure(DMOptions options)
{
if (options.DbContextType == null) return;

if (Helper.IsUsingType<ICapPublisher>(options.DbContextType))
throw new InvalidOperationException(
"We detected that you are using ICapPublisher in DbContext, please change the configuration to use the storage extension directly to avoid circular references! eg: x.UseDM()");

using var scope = _serviceScopeFactory.CreateScope();
var provider = scope.ServiceProvider;
using var dbContext = (DbContext)provider.GetRequiredService(options.DbContextType);
var connectionString = dbContext.Database.GetConnectionString();
if (string.IsNullOrEmpty(connectionString)) throw new ArgumentNullException(connectionString);
options.ConnectionString = connectionString;
}
}
}
25 changes: 25 additions & 0 deletions src/DotNetCore.CAP.DM/CAP.EFOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

namespace DotNetCore.CAP
{
public class EFOptions
{
public const string DefaultSchema = "cap";

/// <summary>
/// Gets or sets the schema to use when creating database objects.
/// Default is <see cref="DefaultSchema" />.
/// </summary>
public string Schema { get; set; } = DefaultSchema;

/// <summary>
/// EF db context type.
/// </summary>
internal Type? DbContextType { get; set; }

/// <summary>
/// Data version
/// </summary>
internal string Version { get; set; } = default!;

}
}
45 changes: 45 additions & 0 deletions src/DotNetCore.CAP.DM/CAP.Options.Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using DotNetCore.CAP;
using Microsoft.EntityFrameworkCore;

namespace Microsoft.Extensions.DependencyInjection
{
public static class CAPOptionsExtensions
{
public static CapOptions UseDM(this CapOptions options, string connectionString)
{
return options.UseDM(opt => { opt.ConnectionString = connectionString; });
}

public static CapOptions UseDM(this CapOptions options, Action<DMOptions> configure)
{
if (configure == null) throw new ArgumentNullException(nameof(configure));

configure += x => x.Version = options.Version;

options.RegisterExtension(new DMCapOptionsExtension(configure));

return options;
}

public static CapOptions UseEntityFramework<TContext>(this CapOptions options)
where TContext : DbContext
{
return options.UseEntityFramework<TContext>(opt => { });
}

public static CapOptions UseEntityFramework<TContext>(this CapOptions options, Action<EFOptions> configure)
where TContext : DbContext
{
if (configure == null) throw new ArgumentNullException(nameof(configure));

options.RegisterExtension(new DMCapOptionsExtension(x =>
{
configure(x);
x.Version = options.Version;
x.DbContextType = typeof(TContext);
}));

return options;
}
}
}
19 changes: 19 additions & 0 deletions src/DotNetCore.CAP.DM/DotNetCore.CAP.DM.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Authors>findersky</Authors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DM.DmProvider" Version="8.3.1.28188" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.17" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DotNetCore.CAP\DotNetCore.CAP.csproj" />
</ItemGroup>

</Project>
Loading