Skip to content

fixes #2532 Added cosmosdb provider #2553

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

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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
1,805 changes: 906 additions & 899 deletions framework/Volo.Abp.sln

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using Volo.Abp;
// Microsoft.EntityFrameworkCore.Cosmos doesn't have single connectionString parameter DbContextOptionsBuilder in {ApplicationName}.EntityFrameworkCore.DbMigrations\{ApplicationName}MigrationsDbContextFactory
namespace Microsoft.EntityFrameworkCore
{
public static class CosmosDbContextOptionsExtensions
{
public static DbContextOptionsBuilder<TContext> UseCosmos<TContext>(
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder,
[NotNull] string connectionString,
[CanBeNull] Action<CosmosDbContextOptionsBuilder> cosmosOptionsAction = null)
where TContext : DbContext
=> (DbContextOptionsBuilder<TContext>)UseCosmos(
(DbContextOptionsBuilder)optionsBuilder,
connectionString,
cosmosOptionsAction
);

public static DbContextOptionsBuilder UseCosmos(
[NotNull] this DbContextOptionsBuilder optionsBuilder,
[NotNull] string connectionString,
[CanBeNull] Action<CosmosDbContextOptionsBuilder> cosmosOptionsAction = null)
{
var connStringParts = connectionString.ParseConnectionString();
var accountEndpoint = connStringParts["AccountEndpoint"];
if (string.IsNullOrEmpty(accountEndpoint))
throw new AbpException($"CosmosDb AccountName can not be null or empty!");

var accountKey = connStringParts["AccountKey"];
if (string.IsNullOrEmpty(accountEndpoint))
throw new AbpException($"CosmosDb AccountKey can not be null or empty!");

var databaseName = connStringParts["DatabaseName"];
if (string.IsNullOrEmpty(accountEndpoint))
throw new AbpException($"CosmosDb DatabaseName can not be null or empty!");

return optionsBuilder.UseCosmos(accountEndpoint: accountEndpoint, accountKey: accountKey, databaseName: databaseName, cosmosOptionsAction);
}

static Dictionary<string, string> ParseConnectionString(this string connString)
{
return connString.Split(';')
.Select(t => t.Split(new char[] { '=' }, 2))
.ToDictionary(t => t[0].Trim(), t => t[1].Trim(), StringComparer.InvariantCultureIgnoreCase);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\..\..\common.props" />

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<AssemblyName>Volo.Abp.EntityFrameworkCore.CosmosDB</AssemblyName>
<PackageId>Volo.Abp.EntityFrameworkCore.CosmosDB</PackageId>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Volo.Abp.EntityFrameworkCore\Volo.Abp.EntityFrameworkCore.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Cosmos" Version="3.1.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using System;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Volo.Abp.EntityFrameworkCore.DependencyInjection;
using System.Linq;
using System.Collections.Generic;

namespace Volo.Abp.EntityFrameworkCore
{
public static class AbpDbContextConfigurationContextCosmosDBExtensions
{
public static DbContextOptionsBuilder UseCosmos(
[NotNull] this AbpDbContextConfigurationContext context,
[CanBeNull] Action<CosmosDbContextOptionsBuilder> cosmosOptionsAction = null)
{
//context.ConnectionStringName = "AbpBackgroundJobs" hits all the time
if (context.ExistingConnection != null)
{
var connStringParts = context.ExistingConnection.ConnectionString.ParseConnectionString();

var accountEndpoint = connStringParts["AccountEndpoint"];
if (string.IsNullOrEmpty(accountEndpoint))
throw new AbpException($"CosmosDb AccountName can not be null or empty!");

var accountKey = connStringParts["AccountKey"];
if (string.IsNullOrEmpty(accountEndpoint))
throw new AbpException($"CosmosDb AccountKey can not be null or empty!");

var databaseName = connStringParts["DatabaseName"];
if (string.IsNullOrEmpty(accountEndpoint))
throw new AbpException($"CosmosDb DatabaseName can not be null or empty!");

return context.DbContextOptions.UseCosmos(accountEndpoint: accountEndpoint, accountKey: accountKey, databaseName: databaseName, cosmosOptionsAction);
}
else
{
var connStringParts = context.ConnectionString.ParseConnectionString();

var accountEndpoint = connStringParts["AccountEndpoint"];
if (string.IsNullOrEmpty(accountEndpoint))
throw new AbpException($"CosmosDb AccountName can not be null or empty!");

var accountKey = connStringParts["AccountKey"];
if (string.IsNullOrEmpty(accountEndpoint))
throw new AbpException($"CosmosDb AccountKey can not be null or empty!");

var databaseName = connStringParts["DatabaseName"];
if (string.IsNullOrEmpty(accountEndpoint))
throw new AbpException($"CosmosDb DatabaseName can not be null or empty!");

return context.DbContextOptions.UseCosmos(accountEndpoint: accountEndpoint, accountKey: accountKey, databaseName: databaseName, cosmosOptionsAction);
}
}

static Dictionary<string, string> ParseConnectionString(this string connString)
{
return connString.Split(';')
.Select(t => t.Split(new char[] { '=' }, 2))
.ToDictionary(t => t[0].Trim(), t => t[1].Trim(), StringComparer.InvariantCultureIgnoreCase);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using JetBrains.Annotations;
using System;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace Volo.Abp.EntityFrameworkCore
{
public static class AbpDbContextOptionsCosmosExtensions
{
public static void UseCosmos(
[NotNull] this AbpDbContextOptions options,
[CanBeNull] Action<CosmosDbContextOptionsBuilder> cosmosOptionsAction = null)
{
options.Configure(context =>
{
context.UseCosmos(cosmosOptionsAction);
});
}

public static void UseCosmos<TDbContext>(
[NotNull] this AbpDbContextOptions options,
[CanBeNull] Action<CosmosDbContextOptionsBuilder> cosmosOptionsAction = null)
where TDbContext : AbpDbContext<TDbContext>
{
options.Configure<TDbContext>(context =>
{
context.UseCosmos(cosmosOptionsAction);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Volo.Abp.Modularity;

namespace Volo.Abp.EntityFrameworkCore.Cosmos
{
[DependsOn(
typeof(AbpEntityFrameworkCoreModule)
)]
public class AbpEntityFrameworkCoreCosmosModule : AbpModule
{
}
}