mirror of https://github.com/abpframework/abp.git
committed by
GitHub
27 changed files with 430 additions and 80 deletions
@ -0,0 +1,27 @@ |
|||
using System; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.MongoDB.DependencyInjection |
|||
{ |
|||
public class AbpMongoDbConventionalRegistrar : DefaultConventionalRegistrar |
|||
{ |
|||
public override void AddType(IServiceCollection services, Type type) |
|||
{ |
|||
if (!typeof(IAbpMongoDbContext).IsAssignableFrom(type) || type == typeof(AbpMongoDbContext)) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
var dependencyAttribute = GetDependencyAttributeOrNull(type); |
|||
var lifeTime = GetLifeTimeOrNull(type, dependencyAttribute); |
|||
|
|||
if (lifeTime == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
services.Add(ServiceDescriptor.Describe(typeof(IAbpMongoDbContext), type, ServiceLifetime.Transient)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,42 @@ |
|||
using System.Collections.Generic; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using MongoDB.Driver; |
|||
|
|||
namespace Volo.Abp.Uow.MongoDB |
|||
{ |
|||
public class MongoDbTransactionApi : ITransactionApi, ISupportsRollback |
|||
{ |
|||
public IClientSessionHandle SessionHandle { get; } |
|||
|
|||
public MongoDbTransactionApi(IClientSessionHandle sessionHandle) |
|||
{ |
|||
SessionHandle = sessionHandle; |
|||
} |
|||
|
|||
public async Task CommitAsync() |
|||
{ |
|||
await SessionHandle.CommitTransactionAsync(); |
|||
} |
|||
|
|||
protected void Commit() |
|||
{ |
|||
SessionHandle.CommitTransaction(); |
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
SessionHandle.Dispose(); |
|||
} |
|||
|
|||
public void Rollback() |
|||
{ |
|||
SessionHandle.AbortTransaction(); |
|||
} |
|||
|
|||
public async Task RollbackAsync(CancellationToken cancellationToken) |
|||
{ |
|||
await SessionHandle.AbortTransactionAsync(cancellationToken); |
|||
} |
|||
} |
|||
} |
|||
@ -1,25 +1,31 @@ |
|||
using System; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.Uow; |
|||
|
|||
namespace Volo.Abp.BlobStoring.Database.MongoDB |
|||
{ |
|||
[DependsOn( |
|||
typeof(BlobStoringDatabaseTestBaseModule), |
|||
typeof(BlobStoringDatabaseMongoDbModule) |
|||
)] |
|||
)] |
|||
public class BlobStoringDatabaseMongoDbTestModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
var connectionString = MongoDbFixture.ConnectionString.EnsureEndsWith('/') + |
|||
var connectionString = MongoDbFixture.ConnectionString.EnsureEndsWith('/') + |
|||
"Db_" + |
|||
Guid.NewGuid().ToString("N"); |
|||
Guid.NewGuid().ToString("N"); |
|||
|
|||
Configure<AbpDbConnectionOptions>(options => |
|||
{ |
|||
options.ConnectionStrings.Default = connectionString; |
|||
}); |
|||
|
|||
Configure<AbpUnitOfWorkDefaultOptions>(options => |
|||
{ |
|||
options.TransactionBehavior = UnitOfWorkTransactionBehavior.Disabled; |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -1,25 +1,31 @@ |
|||
using System; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.Uow; |
|||
|
|||
namespace Volo.CmsKit.MongoDB |
|||
{ |
|||
[DependsOn( |
|||
typeof(CmsKitTestBaseModule), |
|||
typeof(CmsKitMongoDbModule) |
|||
)] |
|||
)] |
|||
public class CmsKitMongoDbTestModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
var connectionString = MongoDbFixture.ConnectionString.EnsureEndsWith('/') + |
|||
var connectionString = MongoDbFixture.ConnectionString.EnsureEndsWith('/') + |
|||
"Db_" + |
|||
Guid.NewGuid().ToString("N"); |
|||
Guid.NewGuid().ToString("N"); |
|||
|
|||
Configure<AbpDbConnectionOptions>(options => |
|||
{ |
|||
options.ConnectionStrings.Default = connectionString; |
|||
}); |
|||
|
|||
Configure<AbpUnitOfWorkDefaultOptions>(options => |
|||
{ |
|||
options.TransactionBehavior = UnitOfWorkTransactionBehavior.Disabled; |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,46 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using MongoDB.Driver; |
|||
using MyCompanyName.MyProjectName.Data; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.MongoDB; |
|||
|
|||
namespace MyCompanyName.MyProjectName.MongoDB |
|||
{ |
|||
public class MongoDbMyProjectNameDbSchemaMigrator : IMyProjectNameDbSchemaMigrator , ITransientDependency |
|||
{ |
|||
private readonly IServiceProvider _serviceProvider; |
|||
|
|||
public MongoDbMyProjectNameDbSchemaMigrator(IServiceProvider serviceProvider) |
|||
{ |
|||
_serviceProvider = serviceProvider; |
|||
} |
|||
|
|||
public Task MigrateAsync() |
|||
{ |
|||
var dbContexts = _serviceProvider.GetServices<IAbpMongoDbContext>(); |
|||
var connectionStringResolver = _serviceProvider.GetService<IConnectionStringResolver>(); |
|||
|
|||
foreach (var dbContext in dbContexts) |
|||
{ |
|||
var connectionString = |
|||
connectionStringResolver.Resolve( |
|||
ConnectionStringNameAttribute.GetConnStringName(dbContext.GetType())); |
|||
var mongoUrl = new MongoUrl(connectionString); |
|||
var databaseName = mongoUrl.DatabaseName; |
|||
var client = new MongoClient(mongoUrl); |
|||
|
|||
if (databaseName.IsNullOrWhiteSpace()) |
|||
{ |
|||
databaseName = ConnectionStringNameAttribute.GetConnStringName(dbContext.GetType()); |
|||
} |
|||
|
|||
(dbContext as AbpMongoDbContext)?.InitializeCollections(client.GetDatabase(databaseName)); |
|||
} |
|||
|
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue