/// <summary> /// Use MongoDb with a one or multiple server urls. /// Multiples urls are usefull when a replica set has been created. /// </summary> /// <param name="bootstrapper">Bootstrapper instance.</param> /// <param name="options">Options to bootstrap MongoDb as Event Store.</param> /// <returns>Bootstrapper instance.</returns> public static Bootstrapper UseMongoDbAsEventStore(this Bootstrapper bootstrapper, MongoEventStoreOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } var service = new MongoDbEventStoreBootstrappService (ctx => { BsonSerializer.RegisterSerializer(typeof(Type), new TypeSerializer()); BsonSerializer.RegisterSerializer(typeof(Guid), new GuidSerializer()); BsonSerializer.RegisterSerializer(typeof(object), new ObjectSerializer()); EventStoreManager.Options = options; if (options.SnapshotBehaviorProvider != null) { if (ctx.IsServiceRegistered(BootstrapperServiceType.IoC)) { bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(options.SnapshotBehaviorProvider, typeof(ISnapshotBehaviorProvider))); bootstrapper.AddIoCRegistration(new FactoryRegistration( () => new MongoDbEventStore(options.SnapshotBehaviorProvider, options.SnapshotEventsArchiveBehavior), typeof(MongoDbEventStore), typeof(IWriteEventStore))); } } EventStoreManager.Activate(); } ); bootstrapper.AddService(service); return(bootstrapper); }
public static Bootstrapper UseEFCoreAsMainRepository(this Bootstrapper bootstrapper, BaseDbContext dbContext, EFCoreOptions options = null) { if (dbContext == null) { throw new ArgumentNullException(nameof(dbContext)); } InitializeBootstrapperService( bootstrapper, (ctx) => { if (ctx.IsServiceRegistered(BootstrapperServiceType.IoC)) { var entities = ReflectionTools.GetAllTypes() .Where(t => typeof(IPersistableEntity).IsAssignableFrom(t) && !t.IsAbstract && t.IsClass).ToList(); foreach (var item in entities) { var efRepoType = typeof(EFRepository <>).MakeGenericType(item); var dataReaderRepoType = typeof(IDataReaderRepository <>).MakeGenericType(item); var databaseRepoType = typeof(IDatabaseRepository <>).MakeGenericType(item); var dataUpdateRepoType = typeof(IDataUpdateRepository <>).MakeGenericType(item); bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(dbContext, dbContext.GetType())); bootstrapper .AddIoCRegistration(new FactoryRegistration(() => efRepoType.CreateInstance(dbContext), efRepoType, dataUpdateRepoType, databaseRepoType, dataReaderRepoType)); } } }, options); return(bootstrapper); }
/// <summary> /// Use RabbitMQ as Pub/Sub system. /// </summary> /// <param name="bootstrapper">Bootstrapper instance to configure</param> /// <returns>Configured bootstrapper instance</returns> public static Bootstrapper UseRabbitMQ( this Bootstrapper bootstrapper, RabbitConnectionInfos connectionInfos, RabbitNetworkInfos networkInfos, Action <RabbitSubscriberConfiguration> subscriberConfiguration = null, Action <RabbitPublisherConfiguration> publisherConfiguration = null) { var service = RabbitMQBootstrappService.Instance; var subscriberConf = new RabbitSubscriberConfiguration { ConnectionInfos = connectionInfos, NetworkInfos = networkInfos }; subscriberConfiguration?.Invoke(subscriberConf); service.BootstrappAction += (ctx) => { var publisherConf = new RabbitPublisherConfiguration() { ConnectionInfos = connectionInfos, NetworkInfos = networkInfos }; publisherConfiguration?.Invoke(publisherConf); bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(subscriberConf, typeof(RabbitSubscriberConfiguration))); bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(publisherConf, typeof(RabbitPublisherConfiguration))); bootstrapper.AddIoCRegistration(new TypeRegistration(typeof(RabbitPublisher), true)); bootstrapper.AddIoCRegistration(new TypeRegistration(typeof(RabbitSubscriber), true)); if (publisherConf.RoutingKeyFactory != null) { bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(publisherConf.RoutingKeyFactory, typeof(IRoutingKeyFactory))); } }; bootstrapper.AddService(service); bootstrapper.OnPostBootstrapping += (c) => { ILoggerFactory loggerFactory = null; IScopeFactory scopeFactory = null; if (c.Scope != null) { loggerFactory = c.Scope.Resolve <ILoggerFactory>(); scopeFactory = c.Scope.Resolve <IScopeFactory>(); } if (loggerFactory == null) { loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(new DebugLoggerProvider()); } RabbitMQBootstrappService.RabbitSubscriber = new RabbitSubscriber( loggerFactory, subscriberConf, scopeFactory); RabbitMQBootstrappService.RabbitSubscriber.Start(); }; return(bootstrapper); }
internal static void ConfigureInMemoryEventBus(Bootstrapper bootstrapper, InMemoryEventBusConfiguration?configuration, string[] excludedEventsDLLs, BootstrappingContext ctx) { InMemoryEventBus.InitHandlersCollection(excludedEventsDLLs); if (ctx.IsServiceRegistered(BootstrapperServiceType.IoC)) { bootstrapper.AddIoCRegistration(new TypeRegistration(typeof(InMemoryEventBus), typeof(IDomainEventBus), typeof(InMemoryEventBus))); if (configuration != null) { bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(configuration, typeof(InMemoryEventBusConfiguration))); } } bootstrapper.AddNotifications(PerformEventChecksAccordingToBootstrapperParameters(ctx, configuration)); }
public static Bootstrapper UseMongoDbAsMainRepository(this Bootstrapper bootstrapper, MongoDbOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } var service = new MongoDbDALBootstrapperService { BootstrappAction = (ctx) => { if (BsonSerializer.SerializerRegistry.GetSerializer <Type>() == null) { BsonSerializer.RegisterSerializer(typeof(Type), new TypeSerializer()); } if (BsonSerializer.SerializerRegistry.GetSerializer <Guid>() == null) { BsonSerializer.RegisterSerializer(typeof(Guid), new GuidSerializer()); } MongoDbContext.DatabaseName = options.DatabaseName; MongoDbContext.MongoClient = new MongoDB.Driver.MongoClient(options.Url); var pack = new ConventionPack(); pack.Add(new IgnoreExtraElementsConvention(true)); ConventionRegistry.Register("CQELight conventions", pack, _ => true); if (ctx.IsServiceRegistered(BootstrapperServiceType.IoC)) { bootstrapper.AddIoCRegistration(new TypeRegistration <MongoDataReaderAdapter>(true)); bootstrapper.AddIoCRegistration(new TypeRegistration <MongoDataWriterAdapter>(true)); var entities = ReflectionTools.GetAllTypes() .Where(t => typeof(IPersistableEntity).IsAssignableFrom(t)).ToList(); foreach (var item in entities) { var mongoRepoType = typeof(MongoRepository <>).MakeGenericType(item); var dataReaderRepoType = typeof(IDataReaderRepository <>).MakeGenericType(item); var databaseRepoType = typeof(IDatabaseRepository <>).MakeGenericType(item); var dataUpdateRepoType = typeof(IDataUpdateRepository <>).MakeGenericType(item); bootstrapper .AddIoCRegistration(new FactoryRegistration(() => mongoRepoType.CreateInstance(), mongoRepoType, dataUpdateRepoType, databaseRepoType, dataReaderRepoType)); } } } }; bootstrapper.AddService(service); return(bootstrapper); }
public static Bootstrapper UseMongoDbAsMainRepository(this Bootstrapper bootstrapper, MongoDbOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } var service = new MongoDbDALBootstrapperService (ctx => { InitiMongoDbStaticStuff(); MongoDbContext.DatabaseName = options.DatabaseName; MongoDbContext.MongoClient = new MongoDB.Driver.MongoClient(options.Url); if (ctx.IsServiceRegistered(BootstrapperServiceType.IoC)) { bootstrapper.AddIoCRegistration(new TypeRegistration <MongoDataReaderAdapter>(true)); bootstrapper.AddIoCRegistration(new TypeRegistration <MongoDataWriterAdapter>(true)); bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(MongoDbContext.MongoClient, RegistrationLifetime.Singleton, typeof(MongoClient))); var entities = ReflectionTools.GetAllTypes() .Where(t => typeof(IPersistableEntity).IsAssignableFrom(t)).ToList(); foreach (var item in entities) { var mongoRepoType = typeof(MongoRepository <>).MakeGenericType(item); var dataReaderRepoType = typeof(IDataReaderRepository <>).MakeGenericType(item); var databaseRepoType = typeof(IDatabaseRepository <>).MakeGenericType(item); var dataUpdateRepoType = typeof(IDataUpdateRepository <>).MakeGenericType(item); bootstrapper .AddIoCRegistration(new FactoryRegistration(() => mongoRepoType.CreateInstance(), mongoRepoType, dataUpdateRepoType, databaseRepoType, dataReaderRepoType)); } } } ); bootstrapper.AddService(service); return(bootstrapper); }
/// <summary> /// Use AzureServiceBus to publish events. /// </summary> /// <param name="bootstrapper">Bootstrapper instance</param> /// <param name="configuration">Azure service bus configuration</param> /// <returns>Bootstrapper instance</returns> public static Bootstrapper UseAzureServiceBus(this Bootstrapper bootstrapper, AzureServiceBusClientConfiguration configuration) { if (configuration == null) { throw new ArgumentException("Bootstrapper.UseAzureServiceBus() : Configuration should be provided.", nameof(configuration)); } bootstrapper.AddIoCRegistration(new InstanceTypeRegistration( configuration, typeof(AzureServiceBusClientConfiguration))); bootstrapper.AddIoCRegistration(new TypeRegistration( typeof(AzureServiceBusClient), typeof(AzureServiceBusClient), typeof(IDomainEventBus))); bootstrapper.AddIoCRegistration(new FactoryRegistration(() => new QueueClient(configuration.ConnectionString, "CQELight"), typeof(QueueClient), typeof(IQueueClient))); return(bootstrapper); }
/// <summary> /// Configure the bootstrapper to use InMemory buses for dispatching events. /// </summary> /// <param name="bootstrapper">Instance of boostrapper.</param> /// <param name="configuration">Configuration to use for in memory event bus.</param> /// <param name="excludedEventsDLLs">DLLs name to exclude from auto-configuration into IoC /// (IAutoRegisterType will be ineffective).</param> public static Bootstrapper UseInMemoryEventBus(this Bootstrapper bootstrapper, InMemoryEventBusConfiguration configuration = null, params string[] excludedEventsDLLs) { var service = InMemoryBusesBootstrappService.Instance; service.BootstrappAction += (ctx) => { InMemoryEventBus.InitHandlersCollection(excludedEventsDLLs); if (ctx.IsServiceRegistered(BootstrapperServiceType.IoC)) { bootstrapper.AddIoCRegistration(new TypeRegistration(typeof(InMemoryEventBus), typeof(IDomainEventBus), typeof(InMemoryEventBus))); if (configuration != null) { bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(configuration, typeof(InMemoryEventBusConfiguration))); } } bootstrapper.AddNotifications(PerformEventChecksAccordingToBootstrapperParameters(ctx, configuration)); }; if (!bootstrapper.RegisteredServices.Any(s => s == service)) { bootstrapper.AddService(service); } return(bootstrapper); }
/// <summary> /// Use EF Core as EventStore for system, with the provided connection string. /// This is a usable case for common cases, in system that not have a huge amount of events, or for test/debug purpose. /// This is not recommanded for real-world big applications case. /// </summary> /// <param name="bootstrapper">Bootstrapper instance.</param> /// <param name="options">Options to use to configure event store.</param> /// <returns>Bootstrapper instance</returns> public static Bootstrapper UseEFCoreAsEventStore(this Bootstrapper bootstrapper, EFEventStoreOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } var service = new EFEventStoreBootstrappService { BootstrappAction = (ctx) => { if (ctx.IsServiceRegistered(BootstrapperServiceType.IoC)) { bootstrapper.AddIoCRegistration(new FactoryRegistration(() => new EventStoreDbContext(options.DbContextOptions, options.ArchiveBehavior), typeof(EventStoreDbContext))); bootstrapper.AddIoCRegistration(new TypeRegistration(typeof(EFEventStore), true)); bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(options, typeof(EFEventStoreOptions))); if (options.SnapshotBehaviorProvider != null) { bootstrapper.AddIoCRegistration( new InstanceTypeRegistration(options.SnapshotBehaviorProvider, typeof(ISnapshotBehaviorProvider))); } if (options.ArchiveBehavior == EventStore.SnapshotEventsArchiveBehavior.StoreToNewDatabase && options.ArchiveDbContextOptions != null) { bootstrapper.AddIoCRegistration(new FactoryRegistration(() => new ArchiveEventStoreDbContext(options.ArchiveDbContextOptions), typeof(ArchiveEventStoreDbContext))); } } EventStoreManager.s_Options = options; EventStoreManager.Activate(); } }; bootstrapper.AddService(service); return(bootstrapper); }
/// <summary> /// Configure EF Core as repository implementation. /// This methods uses a single database configuration and create dynamically all context /// from every concerned assembly. /// </summary> /// <param name="bootstrapper">Bootstrapper instance</param> /// <param name="optionsBuilderCfg">Options builder configuration lambda.</param> /// <param name="options">Custom options to use of using EF.</param> public static Bootstrapper UseEFCoreAsMainRepository(this Bootstrapper bootstrapper, Action <DbContextOptionsBuilder> optionsBuilderCfg, EFCoreOptions options = null) { if (optionsBuilderCfg == null) { throw new ArgumentNullException(nameof(optionsBuilderCfg)); } InitializeBootstrapperService( bootstrapper, (ctx) => { if (ctx.IsServiceRegistered(BootstrapperServiceType.IoC)) { var dbContextOptionsBuilder = new DbContextOptionsBuilder(); optionsBuilderCfg(dbContextOptionsBuilder); var customDbContexts = ReflectionTools.GetAllTypes().Where(t => t.IsInHierarchySubClassOf(typeof(BaseDbContext)) && !t.IsAbstract && t.IsClass && t != typeof(BaseDbContext)); if (customDbContexts.Any()) { foreach (var customDbContextType in customDbContexts) { bootstrapper.AddIoCRegistration(new TypeRegistration(customDbContextType, true)); var customDbContextOptionsType = typeof(DbContextOptions <>).MakeGenericType(customDbContextType); bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(dbContextOptionsBuilder.Options, typeof(DbContextOptions), customDbContextOptionsType)); bootstrapper.AddIoCRegistration(new FactoryRegistration((scope) => { return(new EFCoreDataReaderAdapter(scope.Resolve(customDbContextType) as BaseDbContext, options)); }, typeof(EFCoreDataReaderAdapter), typeof(IDataReaderAdapter))); bootstrapper.AddIoCRegistration(new FactoryRegistration((scope) => { return(new EFCoreDataWriterAdapter(scope.Resolve(customDbContextType) as BaseDbContext, options)); }, typeof(EFCoreDataWriterAdapter), typeof(IDataWriterAdapter))); } } else { bootstrapper.AddIoCRegistration(new TypeRegistration(typeof(BaseDbContext), typeof(BaseDbContext))); bootstrapper.AddIoCRegistration(new TypeRegistration <EFCoreDataReaderAdapter>(true)); bootstrapper.AddIoCRegistration(new TypeRegistration <EFCoreDataWriterAdapter>(true)); bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(dbContextOptionsBuilder.Options, typeof(DbContextOptions), typeof(DbContextOptions <BaseDbContext>))); } if (options != null) { bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(options, typeof(EFCoreOptions))); } foreach (var item in ReflectionTools.GetAllTypes().Where(t => typeof(IPersistableEntity).IsAssignableFrom(t) && !t.IsAbstract && t.IsClass).ToList()) { var efRepoType = typeof(EFRepository <>).MakeGenericType(item); var dataReaderRepoType = typeof(IDataReaderRepository <>).MakeGenericType(item); var databaseRepoType = typeof(IDatabaseRepository <>).MakeGenericType(item); var dataUpdateRepoType = typeof(IDataUpdateRepository <>).MakeGenericType(item); Type ctxType = null; if (s_ContextTypesPerAssembly.ContainsKey(item.Assembly.FullName)) { ctxType = s_ContextTypesPerAssembly[item.Assembly.FullName]; } else { ctxType = ReflectionTools .GetAllTypes() .Where(t => t.Assembly.FullName == item.Assembly.FullName) .FirstOrDefault(c => c.IsSubclassOf(typeof(BaseDbContext))); s_ContextTypesPerAssembly[item.Assembly.FullName] = ctxType; bootstrapper .AddIoCRegistration(new FactoryRegistration(() => ctxType.CreateInstance(dbContextOptionsBuilder.Options), ctxType)); } if (ctxType == null) { throw new InvalidOperationException("Bootstrapper.UseEFCoreAsMainRepository() : " + $"No DbContext found for assembly {item.Assembly.FullName}, but this assembly contains a " + "some persistence entities. You need to create a specific class that inherits from BaseDbContext in this assembly to use this configuration method."); } bootstrapper .AddIoCRegistration(new FactoryRegistration(() => { var dbCtx = ctxType.CreateInstance(dbContextOptionsBuilder.Options); return(efRepoType.CreateInstance(dbCtx)); }, efRepoType, dataUpdateRepoType, databaseRepoType, dataReaderRepoType)); } } else { bootstrapper.AddNotification(new Bootstrapping.Notifications.BootstrapperNotification(Bootstrapping.Notifications.BootstrapperNotificationType.Warning, "No IoC has been configured in your system, it means that all EF DAL will no works 'automatically'," + " you will need to use it with EFCoreDataReaderAdapter and EFCoreDataWriterAdapter with the RepositoryBase class. " + "While this will work, it's not a recommended option and configuring IoC should be strongly considered.")); } }, options); return(bootstrapper); }
private static void RegisterRabbitClientWithinContainer(Bootstrapper bootstrapper) { bootstrapper.AddIoCRegistration(new TypeRegistration <RabbitMQClient>(true)); }
/// <summary> /// Configure EF Core as repository implementation. /// This methods uses a single database configuration and create dynamically all context /// from every concerned assembly. /// </summary> /// <param name="bootstrapper">Bootstrapper instance</param> /// <param name="optionsBuilderCfg">Options builder configuration lambda.</param> /// <param name="options">Custom options to use of using EF.</param> public static Bootstrapper UseEFCoreAsMainRepository(this Bootstrapper bootstrapper, Action <DbContextOptionsBuilder> optionsBuilderCfg, EFCoreOptions options = null) { if (optionsBuilderCfg == null) { throw new ArgumentNullException(nameof(optionsBuilderCfg)); } InitializeBootstrapperService( bootstrapper, (ctx) => { if (ctx.IsServiceRegistered(BootstrapperServiceType.IoC)) { var dbContextOptionsBuilder = new DbContextOptionsBuilder(); optionsBuilderCfg(dbContextOptionsBuilder); foreach (var item in ReflectionTools.GetAllTypes().Where(t => t.IsSubclassOf(typeof(BasePersistableEntity)) && !t.IsAbstract && t.IsClass).ToList()) { var efRepoType = typeof(EFRepository <>).MakeGenericType(item); var dataReaderRepoType = typeof(IDataReaderRepository <>).MakeGenericType(item); var databaseRepoType = typeof(IDatabaseRepository <>).MakeGenericType(item); var dataUpdateRepoType = typeof(IDataUpdateRepository <>).MakeGenericType(item); Type ctxType = null; if (s_ContextTypesPerAssembly.ContainsKey(item.Assembly.FullName)) { ctxType = s_ContextTypesPerAssembly[item.Assembly.FullName]; } else { ctxType = ReflectionTools .GetAllTypes() .Where(t => t.Assembly.FullName == item.Assembly.FullName) .FirstOrDefault(c => c.IsSubclassOf(typeof(BaseDbContext))); s_ContextTypesPerAssembly[item.Assembly.FullName] = ctxType; } if (ctxType == null) { throw new InvalidOperationException("Bootstrapper.UseEFCoreAsMainRepository() : " + $"No DbContext found for assembly {item.Assembly.FullName}, but this assembly contains a " + "some persistence entities. You need to create a specific class that inherits from BaseDbContext in this assembly to use this configuration method."); } bootstrapper .AddIoCRegistration(new FactoryRegistration(() => ctxType.CreateInstance(dbContextOptionsBuilder.Options), ctxType)); bootstrapper .AddIoCRegistration(new FactoryRegistration(() => { var dbCtx = ctxType.CreateInstance(dbContextOptionsBuilder.Options); return(efRepoType.CreateInstance(dbCtx)); }, efRepoType, dataUpdateRepoType, databaseRepoType, dataReaderRepoType)); } } }, options); return(bootstrapper); }