mirror of https://github.com/abpframework/abp.git
committed by
GitHub
15 changed files with 453 additions and 0 deletions
@ -0,0 +1,8 @@ |
|||||
|
namespace Volo.Abp.Data; |
||||
|
|
||||
|
public class AbpConnectionStringCheckResult |
||||
|
{ |
||||
|
public bool Connected { get; set; } |
||||
|
|
||||
|
public bool DatabaseExists { get; set; } |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.Data; |
||||
|
|
||||
|
public class DefaultConnectionStringChecker : IConnectionStringChecker, ITransientDependency |
||||
|
{ |
||||
|
public Task<AbpConnectionStringCheckResult> CheckAsync(string connectionString) |
||||
|
{ |
||||
|
return Task.FromResult(new AbpConnectionStringCheckResult |
||||
|
{ |
||||
|
Connected = false, |
||||
|
DatabaseExists = false |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.Data; |
||||
|
|
||||
|
public interface IConnectionStringChecker |
||||
|
{ |
||||
|
Task<AbpConnectionStringCheckResult> CheckAsync(string connectionString); |
||||
|
} |
||||
@ -0,0 +1,40 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using MySqlConnector; |
||||
|
using Volo.Abp.Data; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.EntityFrameworkCore.ConnectionStrings; |
||||
|
|
||||
|
[Dependency(ReplaceServices = true)] |
||||
|
public class MySqlConnectionStringChecker : IConnectionStringChecker, ITransientDependency |
||||
|
{ |
||||
|
public virtual async Task<AbpConnectionStringCheckResult> CheckAsync(string connectionString) |
||||
|
{ |
||||
|
var result = new AbpConnectionStringCheckResult(); |
||||
|
var connString = new MySqlConnectionStringBuilder(connectionString) |
||||
|
{ |
||||
|
ConnectionLifeTime = 1 |
||||
|
}; |
||||
|
|
||||
|
var oldDatabaseName = connString.Database; |
||||
|
connString.Database = "mysql"; |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
await using var conn = new MySqlConnection(connString.ConnectionString); |
||||
|
await conn.OpenAsync(); |
||||
|
result.Connected = true; |
||||
|
await conn.ChangeDatabaseAsync(oldDatabaseName); |
||||
|
result.DatabaseExists = true; |
||||
|
|
||||
|
await conn.CloseAsync(); |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
catch (Exception e) |
||||
|
{ |
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,36 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Devart.Data.Oracle; |
||||
|
using Volo.Abp.Data; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.EntityFrameworkCore.ConnectionStrings; |
||||
|
|
||||
|
[Dependency(ReplaceServices = true)] |
||||
|
public class OracleDevartConnectionStringChecker : IConnectionStringChecker, ITransientDependency |
||||
|
{ |
||||
|
public virtual async Task<AbpConnectionStringCheckResult> CheckAsync(string connectionString) |
||||
|
{ |
||||
|
var result = new AbpConnectionStringCheckResult(); |
||||
|
var connString = new OracleConnectionStringBuilder(connectionString) |
||||
|
{ |
||||
|
ConnectionTimeout = 1 |
||||
|
}; |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
await using var conn = new OracleConnection(connString.ConnectionString); |
||||
|
await conn.OpenAsync(); |
||||
|
result.Connected = true; |
||||
|
result.DatabaseExists = true; |
||||
|
|
||||
|
await conn.CloseAsync(); |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
catch (Exception e) |
||||
|
{ |
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,36 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Oracle.ManagedDataAccess.Client; |
||||
|
using Volo.Abp.Data; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.EntityFrameworkCore.ConnectionStrings; |
||||
|
|
||||
|
[Dependency(ReplaceServices = true)] |
||||
|
public class OracleConnectionStringChecker : IConnectionStringChecker, ITransientDependency |
||||
|
{ |
||||
|
public virtual async Task<AbpConnectionStringCheckResult> CheckAsync(string connectionString) |
||||
|
{ |
||||
|
var result = new AbpConnectionStringCheckResult(); |
||||
|
var connString = new OracleConnectionStringBuilder(connectionString) |
||||
|
{ |
||||
|
ConnectionTimeout = 1 |
||||
|
}; |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
await using var conn = new OracleConnection(connString.ConnectionString); |
||||
|
await conn.OpenAsync(); |
||||
|
result.Connected = true; |
||||
|
result.DatabaseExists = true; |
||||
|
|
||||
|
await conn.CloseAsync(); |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
catch (Exception e) |
||||
|
{ |
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,40 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Npgsql; |
||||
|
using Volo.Abp.Data; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.EntityFrameworkCore.ConnectionStrings; |
||||
|
|
||||
|
[Dependency(ReplaceServices = true)] |
||||
|
public class NpgsqlConnectionStringChecker : IConnectionStringChecker, ITransientDependency |
||||
|
{ |
||||
|
public virtual async Task<AbpConnectionStringCheckResult> CheckAsync(string connectionString) |
||||
|
{ |
||||
|
var result = new AbpConnectionStringCheckResult(); |
||||
|
var connString = new NpgsqlConnectionStringBuilder(connectionString) |
||||
|
{ |
||||
|
Timeout = 1 |
||||
|
}; |
||||
|
|
||||
|
var oldDatabaseName = connString.Database; |
||||
|
connString.Database = "postgres"; |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
await using var conn = new NpgsqlConnection(connString.ConnectionString); |
||||
|
await conn.OpenAsync(); |
||||
|
result.Connected = true; |
||||
|
await conn.ChangeDatabaseAsync(oldDatabaseName); |
||||
|
result.DatabaseExists = true; |
||||
|
|
||||
|
await conn.CloseAsync(); |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
catch (Exception e) |
||||
|
{ |
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,40 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Data.SqlClient; |
||||
|
using Volo.Abp.Data; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.EntityFrameworkCore.ConnectionStrings; |
||||
|
|
||||
|
[Dependency(ReplaceServices = true)] |
||||
|
public class SqlServerConnectionStringChecker : IConnectionStringChecker, ITransientDependency |
||||
|
{ |
||||
|
public virtual async Task<AbpConnectionStringCheckResult> CheckAsync(string connectionString) |
||||
|
{ |
||||
|
var result = new AbpConnectionStringCheckResult(); |
||||
|
var connString = new SqlConnectionStringBuilder(connectionString) |
||||
|
{ |
||||
|
ConnectTimeout = 1 |
||||
|
}; |
||||
|
|
||||
|
var oldDatabaseName = connString.InitialCatalog; |
||||
|
connString.InitialCatalog = "master"; |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
await using var conn = new SqlConnection(connString.ConnectionString); |
||||
|
await conn.OpenAsync(); |
||||
|
result.Connected = true; |
||||
|
await conn.ChangeDatabaseAsync(oldDatabaseName); |
||||
|
result.DatabaseExists = true; |
||||
|
|
||||
|
await conn.CloseAsync(); |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
catch (Exception e) |
||||
|
{ |
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,32 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Data.Sqlite; |
||||
|
using Volo.Abp.Data; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.EntityFrameworkCore.ConnectionStrings; |
||||
|
|
||||
|
[Dependency(ReplaceServices = true)] |
||||
|
public class SqliteConnectionStringChecker : IConnectionStringChecker, ITransientDependency |
||||
|
{ |
||||
|
public virtual async Task<AbpConnectionStringCheckResult> CheckAsync(string connectionString) |
||||
|
{ |
||||
|
var result = new AbpConnectionStringCheckResult(); |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
await using var conn = new SqliteConnection(connectionString); |
||||
|
await conn.OpenAsync(); |
||||
|
result.Connected = true; |
||||
|
result.DatabaseExists = true; |
||||
|
|
||||
|
await conn.CloseAsync(); |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
catch (Exception e) |
||||
|
{ |
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,30 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using MongoDB.Driver; |
||||
|
using Volo.Abp.Data; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.MongoDB.ConnectionStrings; |
||||
|
|
||||
|
[Dependency(ReplaceServices = true)] |
||||
|
public class MongoDBConnectionStringChecker : IConnectionStringChecker, ITransientDependency |
||||
|
{ |
||||
|
public virtual Task<AbpConnectionStringCheckResult> CheckAsync(string connectionString) |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
var mongoUrl = MongoUrl.Create(connectionString); |
||||
|
var client = new MongoClient(mongoUrl); |
||||
|
client.GetDatabase(mongoUrl.DatabaseName); |
||||
|
return Task.FromResult(new AbpConnectionStringCheckResult() |
||||
|
{ |
||||
|
Connected = true, |
||||
|
DatabaseExists = true |
||||
|
}); |
||||
|
} |
||||
|
catch (Exception e) |
||||
|
{ |
||||
|
return Task.FromResult(new AbpConnectionStringCheckResult()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,39 @@ |
|||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.Uow; |
||||
|
|
||||
|
[DisableConventionalRegistration] |
||||
|
public class AlwaysDisableTransactionsUnitOfWorkManager : IUnitOfWorkManager |
||||
|
{ |
||||
|
private readonly UnitOfWorkManager _unitOfWorkManager; |
||||
|
|
||||
|
public AlwaysDisableTransactionsUnitOfWorkManager(UnitOfWorkManager unitOfWorkManager) |
||||
|
{ |
||||
|
_unitOfWorkManager = unitOfWorkManager; |
||||
|
} |
||||
|
|
||||
|
public IUnitOfWork Current => _unitOfWorkManager.Current; |
||||
|
|
||||
|
public IUnitOfWork Begin(AbpUnitOfWorkOptions options, bool requiresNew = false) |
||||
|
{ |
||||
|
options.IsTransactional = false; |
||||
|
return _unitOfWorkManager.Begin(options, requiresNew); |
||||
|
} |
||||
|
|
||||
|
public IUnitOfWork Reserve(string reservationName, bool requiresNew = false) |
||||
|
{ |
||||
|
return _unitOfWorkManager.Reserve(reservationName, requiresNew); |
||||
|
} |
||||
|
|
||||
|
public void BeginReserved(string reservationName, AbpUnitOfWorkOptions options) |
||||
|
{ |
||||
|
options.IsTransactional = false; |
||||
|
_unitOfWorkManager.BeginReserved(reservationName, options); |
||||
|
} |
||||
|
|
||||
|
public bool TryBeginReserved(string reservationName, AbpUnitOfWorkOptions options) |
||||
|
{ |
||||
|
options.IsTransactional = false; |
||||
|
return _unitOfWorkManager.TryBeginReserved(reservationName, options); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,12 @@ |
|||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.DependencyInjection.Extensions; |
||||
|
|
||||
|
namespace Volo.Abp.Uow; |
||||
|
|
||||
|
public static class UnitOfWorkCollectionExtensions |
||||
|
{ |
||||
|
public static IServiceCollection AddAlwaysDisableUnitOfWorkTransaction(this IServiceCollection services) |
||||
|
{ |
||||
|
return services.Replace(ServiceDescriptor.Singleton<IUnitOfWorkManager, AlwaysDisableTransactionsUnitOfWorkManager>()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Data; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.EntityFrameworkCore.ConnectionStrings; |
||||
|
|
||||
|
public class AbpConnectionStringChecker_Tests : EntityFrameworkCoreTestBase |
||||
|
{ |
||||
|
[Fact] |
||||
|
public async Task IsValidAsync() |
||||
|
{ |
||||
|
var connectionStringChecker = GetRequiredService<IConnectionStringChecker>(); |
||||
|
var result = await connectionStringChecker.CheckAsync(@"Data Source=:memory:"); |
||||
|
result.Connected.ShouldBeTrue(); |
||||
|
result.DatabaseExists.ShouldBeTrue(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Testing; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Uow; |
||||
|
|
||||
|
public class AlwaysDisableUnitOfWorkTransaction_Tests: AbpIntegratedTest<AbpUnitOfWorkModule> |
||||
|
{ |
||||
|
protected override void AfterAddApplication(IServiceCollection services) |
||||
|
{ |
||||
|
services.AddAlwaysDisableUnitOfWorkTransaction(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void AlwaysDisableUnitOfWorkTransaction_Test() |
||||
|
{ |
||||
|
GetService<UnitOfWorkManager>().ShouldNotBeNull(); |
||||
|
|
||||
|
var unitOfWorkManager = ServiceProvider.GetRequiredService<IUnitOfWorkManager>(); |
||||
|
unitOfWorkManager.GetType().ShouldBe(typeof(AlwaysDisableTransactionsUnitOfWorkManager)); |
||||
|
|
||||
|
using (var uow = unitOfWorkManager.Begin(isTransactional: true)) |
||||
|
{ |
||||
|
uow.Options.IsTransactional.ShouldBeFalse(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue