public IMongoDatabase GetDatabase() { var unitOfWork = _unitOfWorkManager.Current; if (unitOfWork == null) { throw new AbpException($"A {nameof(IMongoDatabase)} instance can only be created inside a unit of work!"); } var connectionString = _connectionStringResolver.Resolve <TMongoDbContext>(); var dbContextKey = $"{typeof(TMongoDbContext).FullName}_{connectionString}"; string databaseName; if (connectionString.Contains("|")) { var splitted = connectionString.Split('|'); connectionString = splitted[0]; databaseName = splitted[1]; } else { databaseName = ConnectionStringNameAttribute.GetConnStringName <TMongoDbContext>(); } //TODO: Create only single MongoDbClient per connection string in an application (extract MongoClientCache for example). var databaseApi = unitOfWork.GetOrAddDatabaseApi( dbContextKey, () => new MongoDbDatabaseApi( new MongoClient(connectionString).GetDatabase(databaseName) )); return(((MongoDbDatabaseApi)databaseApi).Database); }
public TMongoDbContext GetDbContext() { Logger.LogWarning( "UnitOfWorkDbContextProvider.GetDbContext is deprecated. Use GetDbContextAsync instead! " + "You are probably using LINQ (LINQ extensions) directly on a repository. In this case, use repository.GetQueryableAsync() method " + "to obtain an IQueryable<T> instance and use LINQ (LINQ extensions) on this object. " ); Logger.LogWarning(Environment.StackTrace.Truncate(2048)); var unitOfWork = _unitOfWorkManager.Current; if (unitOfWork == null) { throw new AbpException( $"A {nameof(IMongoDatabase)} instance can only be created inside a unit of work!"); } var connectionString = _connectionStringResolver.Resolve <TMongoDbContext>(); var dbContextKey = $"{typeof(TMongoDbContext).FullName}_{connectionString}"; var mongoUrl = new MongoUrl(connectionString); var databaseName = mongoUrl.DatabaseName; if (databaseName.IsNullOrWhiteSpace()) { databaseName = ConnectionStringNameAttribute.GetConnStringName <TMongoDbContext>(); } //TODO: Create only single MongoDbClient per connection string in an application (extract MongoClientCache for example). var databaseApi = unitOfWork.GetOrAddDatabaseApi( dbContextKey, () => new MongoDbDatabaseApi <TMongoDbContext>(CreateDbContext(unitOfWork, mongoUrl, databaseName))); return(((MongoDbDatabaseApi <TMongoDbContext>)databaseApi).DbContext); }
public TMongoDbContext GetDbContext() { var unitOfWork = _unitOfWorkManager.Current; if (unitOfWork == null) { throw new RocketException($"A {nameof(IMongoDatabase)} instance can only be created inside a unit of work!"); } var connectionString = _connectionStringResolver.Resolve <TMongoDbContext> (); var dbContextKey = $"{typeof(TMongoDbContext).FullName}_{connectionString}"; var mongoUrl = new MongoUrl(connectionString); var databaseName = mongoUrl.DatabaseName; if (databaseName.IsNullOrWhiteSpace()) { databaseName = ConnectionStringNameAttribute.GetConnStringName <TMongoDbContext> (); } //TODO: 考虑创建一个单例的客户端 MongoDbClient (除了作为示例 MongoClientCache). var databaseApi = unitOfWork.GetOrAddDatabaseApi( dbContextKey, () => { var database = new MongoClient(mongoUrl).GetDatabase(databaseName); var dbContext = unitOfWork.ServiceProvider.GetRequiredService <TMongoDbContext> (); dbContext.ToRocketMongoDbContext().InitializeDatabase(database); return(new MongoDbDatabaseApi <TMongoDbContext> (dbContext)); }); return(((MongoDbDatabaseApi <TMongoDbContext>)databaseApi).DbContext); }
public TDbContext GetDbContext() { if (UnitOfWork.EnableObsoleteDbContextCreationWarning && !UnitOfWorkManager.DisableObsoleteDbContextCreationWarning.Value) { Logger.LogWarning( "UnitOfWorkDbContextProvider.GetDbContext is deprecated. Use GetDbContextAsync instead! " + "You are probably using LINQ (LINQ extensions) directly on a repository. In this case, use repository.GetQueryableAsync() method " + "to obtain an IQueryable<T> instance and use LINQ (LINQ extensions) on this object. " ); Logger.LogWarning(Environment.StackTrace.Truncate(2048)); } var unitOfWork = _unitOfWorkManager.Current; if (unitOfWork == null) { throw new AbpException("A DbContext can only be created inside a unit of work!"); } var targetDbContextType = _options.GetReplacedTypeOrSelf(typeof(TDbContext)); var connectionStringName = ConnectionStringNameAttribute.GetConnStringName(targetDbContextType); var connectionString = ResolveConnectionString(connectionStringName); var dbContextKey = $"{targetDbContextType.FullName}_{connectionString}"; var databaseApi = unitOfWork.GetOrAddDatabaseApi( dbContextKey, () => new EfCoreDatabaseApi( CreateDbContext(unitOfWork, connectionStringName, connectionString) )); return((TDbContext)((EfCoreDatabaseApi)databaseApi).DbContext); }
/// <summary> /// Create the projection from the attribute linked to the function parameter /// </summary> /// <param name="attribute"> /// The attribute describing which projection to run /// </param> public Projection(ProjectionAttribute attribute, string connectionStringName = "") { _domainName = attribute.DomainName; _aggregateTypeName = attribute.AggregateTypeName; _aggregateInstanceKey = attribute.InstanceKey; _projectionTypeName = attribute.ProjectionTypeName; if (string.IsNullOrWhiteSpace(connectionStringName)) { _connectionStringName = ConnectionStringNameAttribute.DefaultConnectionStringName(attribute); } else { _connectionStringName = connectionStringName; } if (null == _projectionProcessor) { // TODO : Allow for different backing technologies... currently just AppendBlob _projectionProcessor = CQRSAzure.EventSourcing.Azure.Blob.Untyped.BlobEventStreamReaderUntyped.CreateProjectionProcessor(attribute, ConnectionStringNameAttribute.DefaultBlobStreamSettings(_domainName, _aggregateTypeName)); } }
public EventStream(EventStreamAttribute attribute, string connectionStringName = "", IWriteContext context = null) { _domainName = attribute.DomainName; _aggregateTypeName = attribute.AggregateTypeName; _aggregateInstanceKey = attribute.InstanceKey; if (string.IsNullOrWhiteSpace(connectionStringName)) { _connectionStringName = ConnectionStringNameAttribute.DefaultConnectionStringName(attribute); } else { _connectionStringName = connectionStringName; } // wire up the event stream writer // TODO : Cater for different backing technologies... currently just AppendBlob _writer = new CQRSAzure.EventSourcing.Azure.Blob.Untyped.BlobEventStreamWriterUntyped(attribute, connectionStringName = _connectionStringName); if (null != context) { _context = context; if (null != _writer) { _writer.SetContext(_context); } } }
/// <summary> /// 获取数据库上下文 /// </summary> /// <returns></returns> /// <exception cref="Exception"></exception> public async Task <TDbContext> GetDbContextAsync() { // 获得当前的可用工作单元。 var unitOfWork = _unitOfWorkManager.Current; if (unitOfWork == null) { throw new Exception("A DbContext can only be created inside a unit of work!"); } // 获得数据库连接上下文的连接字符串名称。 // 根据名称解析具体的连接字符串。 var targetDbContextType = _options.GetReplacedTypeOrSelf(typeof(TDbContext)); var connectionStringName = ConnectionStringNameAttribute.GetConnStringName(targetDbContextType); var connectionString = await ResolveConnectionStringAsync(connectionStringName); // 构造数据库上下文缓存 Key。 // 从工作单元的缓存当中获取数据库上下文,不存在则调用 CreateDbContext() 创建。 var dbContextKey = $"{targetDbContextType.FullName}_{connectionString}"; var databaseApi = unitOfWork.FindDatabaseApi(dbContextKey); if (databaseApi == null) { databaseApi = new EfDatabaseApi(await CreateDbContextAsync(unitOfWork, connectionStringName, connectionString)); unitOfWork.AddDatabaseApi(dbContextKey, databaseApi); } return((TDbContext)((EfDatabaseApi)databaseApi).DbContext); }
public EntityType(string entityTypeName, string domainParentName = @"", string connectionStringName = @"", string storageType = @"BlobStream", ProjectionDefinitions projectionDefinitions = null, ClassifierDefinitions classifierDefinitions = null, EventTypes eventTypes = null, IdentifierGroupDefinitions identifierGroupDefinitions = null) { _entityTypeName = entityTypeName; if (!string.IsNullOrWhiteSpace(connectionStringName)) { _connectionStringName = connectionStringName; } else { // make a default connection string to use _connectionStringName = ConnectionStringNameAttribute.DefaultConnectionStringName(domainParentName, entityTypeName); } if (!string.IsNullOrWhiteSpace(storageType)) { _storageType = storageType; } if (null == projectionDefinitions) { _projectionDefinitions = new ProjectionDefinitions(); } else { _projectionDefinitions = projectionDefinitions; } if (null == classifierDefinitions) { _classifierDefinitions = new ClassifierDefinitions(); } else { _classifierDefinitions = classifierDefinitions; } if (null == eventTypes) { _eventTypes = new EventTypes(); } else { _eventTypes = eventTypes; } if (null == identifierGroupDefinitions) { _identifierGroupDefinitions = new IdentifierGroupDefinitions(); } else { _identifierGroupDefinitions = identifierGroupDefinitions; } }
public async Task <TDbContext> GetDbContextAsync() { var unitOfWork = _unitOfWorkManager.Current; if (unitOfWork == null) { throw new AbpException("A DbContext can only be created inside a unit of work!"); } var targetDbContextType = _options.GetReplacedTypeOrSelf(typeof(TDbContext)); var connectionStringName = ConnectionStringNameAttribute.GetConnStringName(targetDbContextType); var connectionString = await ResolveConnectionStringAsync(connectionStringName); var dbContextKey = $"{targetDbContextType.FullName}_{connectionString}"; var databaseApi = unitOfWork.FindDatabaseApi(dbContextKey); if (databaseApi == null) { databaseApi = new EfCoreDatabaseApi( await CreateDbContextAsync(unitOfWork, connectionStringName, connectionString) ); unitOfWork.AddDatabaseApi(dbContextKey, databaseApi); } return((TDbContext)((EfCoreDatabaseApi)databaseApi).DbContext); }
public TMongoDbContext GetDbContext() { var unitOfWork = _unitOfWorkManager.Current; if (unitOfWork == null) { throw new AbpException( $"A {nameof(IMongoDatabase)} instance can only be created inside a unit of work!"); } var connectionString = _connectionStringResolver.Resolve <TMongoDbContext>(); var dbContextKey = $"{typeof(TMongoDbContext).FullName}_{connectionString}"; var mongoUrl = new MongoUrl(connectionString); var databaseName = mongoUrl.DatabaseName; if (databaseName.IsNullOrWhiteSpace()) { databaseName = ConnectionStringNameAttribute.GetConnStringName <TMongoDbContext>(); } //TODO: Create only single MongoDbClient per connection string in an application (extract MongoClientCache for example). var databaseApi = unitOfWork.GetOrAddDatabaseApi( dbContextKey, () => new MongoDbDatabaseApi <TMongoDbContext>(CreateDbContext(unitOfWork, mongoUrl, databaseName))); return(((MongoDbDatabaseApi <TMongoDbContext>)databaseApi).DbContext); }
public async Task <TDbContext> GetDbContextAsync() { var unitOfWork = _unitOfWorkManager.Current; if (unitOfWork == null) { throw new AbpException("A DbContext can only be created inside a unit of work!"); } var connectionStringName = ConnectionStringNameAttribute.GetConnStringName <TDbContext>(); var connectionString = await _connectionStringResolver.ResolveAsync(connectionStringName); var dbContextKey = $"{typeof(TDbContext).FullName}_{connectionString}"; var databaseApi = unitOfWork.FindDatabaseApi(dbContextKey); if (databaseApi == null) { databaseApi = new EfCoreDatabaseApi <TDbContext>( await CreateDbContextAsync(unitOfWork, connectionStringName, connectionString) ); unitOfWork.AddDatabaseApi(dbContextKey, databaseApi); } return(((EfCoreDatabaseApi <TDbContext>)databaseApi).DbContext); }
public void DomainNameAttribute_EnvironmentVariables_QueryExactMatch_Test() { string expected = "QueryTestStorageConnectionString"; string actual = "Not set"; actual = ConnectionStringNameAttribute.DefaultConnectionStringName("Query", "test type"); Assert.AreEqual(expected, actual); }
public string GetConnectionStringName(IEventStreamIdentity attribute) { CreateFronEnvironmentStringIfNotExists(attribute); if (AllSettings.ContainsKey(EventStreamSetting.MakeDomainQualifiedEntityName(attribute))) { return(AllSettings[EventStreamSetting.MakeDomainQualifiedEntityName(attribute)].ConnectionStringName); } else { return(ConnectionStringNameAttribute.DefaultConnectionStringName(attribute)); } }
/// <summary> /// The initialisation method to set up the <see cref="ITestRunner"/> for the test, and making the initial connection to the target database /// </summary> /// <returns>A task representing the asynchronous operation</returns> protected async Task BaseInit() { ConnectionStringNameAttribute attribute = GetType().GetCustomAttribute <ConnectionStringNameAttribute>(); string variable = attribute?.ConnectionStringName ?? ParameterName ?? Configuration["DefaultConnectionStringName"] ?? _defaultParameterName; string connectionString = GetConnectionStringByName(variable); TestRunner = await NewConnectionByConnectionStringAsync(connectionString); }
private static SignalRAttribute SignalRAttributeFromTarget(string target) { if (!string.IsNullOrWhiteSpace(target)) { SignalRAttribute ret = new SignalRAttribute() { // Look up the connection string from the hub name ConnectionStringSetting = ConnectionStringNameAttribute.DefaultConnectionStringName(Constants.Domain_Query, target), HubName = target }; return(ret); } return(null); }
private static SignalRAttribute SignalRAttributeFromTarget(string hookAddress) { if (!string.IsNullOrWhiteSpace(hookAddress)) { SignalRAttribute ret = new SignalRAttribute() { // Look up the connection string from the hub name ConnectionStringSetting = ConnectionStringNameAttribute.DefaultConnectionStringName(Constants.Domain_Command, hookAddress), HubName = hookAddress }; return(ret); } return(null); }
private static DbContextCreationContext GetCreationContext <TDbContext>(IServiceProvider serviceProvider) where TDbContext : ScorpioDbContext <TDbContext> { var context = DbContextCreationContext.Current; if (context != null) { return(context); } var connectionStringName = ConnectionStringNameAttribute.GetConnStringName <TDbContext>(); var connectionString = serviceProvider.GetRequiredService <IConnectionStringResolver>().Resolve(connectionStringName); return(new DbContextCreationContext( connectionString )); }
public IdentifierGroup(IdentifierGroupAttribute identifierGroup, string connectionStringName = @"") { _domainName = identifierGroup.DomainName; _aggregateTypeName = identifierGroup.AggregateTypeName; _identifierGroupName = identifierGroup.IdentifierGroupName; if (string.IsNullOrWhiteSpace(connectionStringName)) { _connectionStringName = ConnectionStringNameAttribute.DefaultConnectionStringName(identifierGroup.DomainName, identifierGroup.AggregateTypeName); } else { _connectionStringName = connectionStringName; } }
public async Task <IEnumerable <string> > GetAll(DateTime?effectiveDateTime = null) { _processor = new AzureBlobIdentifierGroupProcessorUntyped(_domainName, _aggregateTypeName, _connectionStringName, ConnectionStringNameAttribute.DefaultBlobStreamSettings(_domainName, _aggregateTypeName)); if (null == _processor) { // Cannot get the members anyway return(Enumerable.Empty <string>()); } else { return(await _processor.GetAll(effectiveDateTime)); } }
/// <summary> /// Create the classifier from the attribute assigned to it in the function /// parameters /// </summary> /// <param name="attribute"> /// The attribute that describes the classifier to use /// </param> public Classifier(ClassifierAttribute attribute, string connectionStringName = @"") { _domainName = attribute.DomainName; _aggregateTypeName = attribute.AggregateTypeName; _aggregateInstanceKey = attribute.InstanceKey; _classifierTypeName = attribute.ClassifierTypeName; if (string.IsNullOrWhiteSpace(connectionStringName)) { _connectionStringName = ConnectionStringNameAttribute.DefaultConnectionStringName(attribute); } else { _connectionStringName = connectionStringName; } }
private static DbContextCreationContext GetCreationContext <TDbContext>(IServiceProvider serviceProvider) where TDbContext : AbpDbContext <TDbContext> { var context = DbContextCreationContext.Current; if (context != null) { return(context); } var connectionStringName = ConnectionStringNameAttribute.GetConnStringName <TDbContext>(); var connectionString = ResolveConnectionString <TDbContext>(serviceProvider, connectionStringName); return(new DbContextCreationContext( connectionStringName, connectionString )); }
public TDbContext GetDbContext() { var unitOfWork = _unitOfWorkManager.Current; if (unitOfWork == null) { throw new Exception("A DbContext can only be created inside a unit of work!"); } var connectionStringName = ConnectionStringNameAttribute.GetConnStringName <TDbContext>(); var connectionString = _connectionStringResolver.Resolve(connectionStringName); var dbContextKey = $"{typeof(TDbContext).FullName}_{connectionString}"; var databaseApi = unitOfWork.GetOrAddDatabaseApi(dbContextKey, () => new EfCoreDatabaseApi <TDbContext>(CreateDbContext(unitOfWork, connectionStringName, connectionString) )); return(((EfCoreDatabaseApi <TDbContext>)databaseApi).DbContext); }
public async Task <TMongoDbContext> GetDbContextAsync(CancellationToken cancellationToken = default) { var unitOfWork = _unitOfWorkManager.Current; if (unitOfWork == null) { throw new AbpException( $"A {nameof(IMongoDatabase)} instance can only be created inside a unit of work!"); } var targetDbContextType = _options.GetReplacedTypeOrSelf(typeof(TMongoDbContext)); var connectionString = await ResolveConnectionStringAsync(targetDbContextType); var dbContextKey = $"{targetDbContextType.FullName}_{connectionString}"; var mongoUrl = new MongoUrl(connectionString); var databaseName = mongoUrl.DatabaseName; if (databaseName.IsNullOrWhiteSpace()) { databaseName = ConnectionStringNameAttribute.GetConnStringName(targetDbContextType); } //TODO: Create only single MongoDbClient per connection string in an application (extract MongoClientCache for example). var databaseApi = unitOfWork.FindDatabaseApi(dbContextKey); if (databaseApi == null) { databaseApi = new MongoDbDatabaseApi( await CreateDbContextAsync( unitOfWork, mongoUrl, databaseName, cancellationToken ) ); unitOfWork.AddDatabaseApi(dbContextKey, databaseApi); } return((TMongoDbContext)((MongoDbDatabaseApi)databaseApi).DbContext); }
private static DbContextCreationContext GetCreationContext <TDbContext>(IServiceProvider serviceProvider) where TDbContext : AbpDbContext <TDbContext> { var context = DbContextCreationContext.Current; if (context != null) { return(context); } var connectionStringName = ConnectionStringNameAttribute.GetConnStringName <TDbContext>(); //Use DefaultConnectionStringResolver.Resolve when we remove IConnectionStringResolver.Resolve #pragma warning disable 618 var connectionString = serviceProvider.GetRequiredService <IConnectionStringResolver>().Resolve(connectionStringName); #pragma warning restore 618 return(new DbContextCreationContext( connectionStringName, connectionString )); }
private static DbContextCreationContext GetCreationContext <TDbContext>(IServiceProvider serviceProvider) where TDbContext : DbContext { // 优先从一个 AsyncLocal 当中获取。 var context = DbContextCreationContext.Current; if (context != null) { return(context); } // 从 TDbContext 的 ConnectionStringName 特性获取连接字符串名称。 // 使用 IConnectionStringResolver 根据指定的名称获得连接字符串。 var connectionStringName = ConnectionStringNameAttribute.GetConnStringName <TDbContext>(); var connectionString = ResolveConnectionString <TDbContext>(serviceProvider, connectionStringName); // 构造一个新的 DbContextCreationContext 对象。 return(new DbContextCreationContext( connectionStringName, connectionString )); }
public async Task MigrateAsync() { var dbContexts = _serviceProvider.GetServices <IAbpMongoDbContext>(); var connectionStringResolver = _serviceProvider.GetRequiredService <IConnectionStringResolver>(); foreach (var dbContext in dbContexts) { var connectionString = await connectionStringResolver.ResolveAsync( 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)); } }
public static EventStreamSetting SettingsFromEnvironmentStringValue(IEventStreamIdentity eventStreamIdentity, string environmentStringValue) { EventStreamSetting ret = new EventStreamSetting(MakeDomainQualifiedEntityName(eventStreamIdentity)); if (!string.IsNullOrWhiteSpace(environmentStringValue)) { if (environmentStringValue.StartsWith("Table;")) { ret.Storage = EVENTSTREAMIMPLEMENTATIOIN_TABLE; if (environmentStringValue.Length > @"Table;".Length) { ret.ConnectionStringName = environmentStringValue.Substring(6); } } if (environmentStringValue.StartsWith("AppendBlob;")) { ret.Storage = EVENTSTREAMIMPLEMENTATION_APPENDBLOB; if (environmentStringValue.Length > @"AppendBlob;".Length) { ret.ConnectionStringName = environmentStringValue.Substring(11); } } } // If not able to make a setting from the environment string, fall back on the default if (string.IsNullOrWhiteSpace(ret.Storage)) { // Default to event blob storage ret.Storage = EVENTSTREAMIMPLEMENTATION_APPENDBLOB; } if (string.IsNullOrWhiteSpace(ret.ConnectionStringName)) { ret.ConnectionStringName = ConnectionStringNameAttribute.DefaultConnectionStringName(eventStreamIdentity); } return(ret); }
public static IServiceCollection AddKeyValueDbContext <TKeyValueDbContext>( this IServiceCollection serviceCollection, Action <KeyValueDbContextCreationOptions <TKeyValueDbContext> > builder = null) where TKeyValueDbContext : KeyValueDbContext <TKeyValueDbContext> { serviceCollection.TryAddSingleton <TKeyValueDbContext>(); serviceCollection.TryAddTransient <KeyValueDatabaseOptions <TKeyValueDbContext> >(factory => { var o = new KeyValueDatabaseOptions <TKeyValueDbContext>(); var name = ConnectionStringNameAttribute.GetConnStringName <TKeyValueDbContext>(); o.ConnectionString = factory.GetRequiredService <IConnectionStringResolver>().Resolve(name); return(o); }); var options = new KeyValueDbContextCreationOptions <TKeyValueDbContext>(serviceCollection); builder?.Invoke(options); return(serviceCollection); }
public TVfpContext GetDbContext() { var unitOfWork = _unitOfWorkManager.Current; if (unitOfWork == null) { throw new AbpException($"A {nameof(Database)} instance can only be created inside a unit of work!"); } var connectionString = _connectionStringResolver.Resolve <TVfpContext>(); var dbContextKey = $"{typeof(TVfpContext).FullName}_{connectionString}"; //var mongoUrl = new MongoUrl(connectionString); string databaseName = @"D:\GitHub\dados\SincaTeste.dbc"; //mongoUrl.DatabaseName; if (databaseName.IsNullOrWhiteSpace()) { databaseName = ConnectionStringNameAttribute.GetConnStringName <TVfpContext>(); } //TODO: Create only single MongoDbClient per connection string in an application (extract MongoClientCache for example). var databaseApi = unitOfWork.GetOrAddDatabaseApi( dbContextKey, () => { var database = new DbContext(new VfpConnection(@"D:\GitHub\dados\SincaTeste.dbc"), true); var dbContext = unitOfWork.ServiceProvider.GetRequiredService <TVfpContext>(); dbContext.ToAbpVfpContext().InitializeDatabase(database.Database); return(new VfpDatabaseApi <TVfpContext>(dbContext)); }); return(((VfpDatabaseApi <TVfpContext>)databaseApi).DbContext); }
public void DomainNameAttribute_EnvironmentVariables_Test() { string expected = ConnectionStringNameAttribute.DefaultConnectionStringName("Test", "test type"); Assert.IsNotEmpty(expected, "No default connection"); }