Пример #1
0
        // private static Assembly[] GetListOfEntryAssemblyWithReferences()
        // {
        //     var listOfAssemblies = new List<Assembly>();
        //     var mainAsm = Assembly.GetEntryAssembly();
        //     listOfAssemblies.Add(mainAsm);
        //
        //     if (mainAsm is { })
        //         listOfAssemblies.AddRange(mainAsm.GetReferencedAssemblies().Select(Assembly.Load));
        //
        //     return listOfAssemblies.ToArray();
        // }

        public static WrapperizerContext AddHandlers(this WrapperizerContext wrapperizerContext,
                                                     Action <CqrsContext> configure  = null,
                                                     ServiceLifetime serviceLifetime = ServiceLifetime.Transient, params Assembly[] assemblies
                                                     )
        {
            if (wrapperizerContext is null)
            {
                throw new ArgumentNullException(nameof(wrapperizerContext));
            }

            if (assemblies == null || assemblies.Length == 0)
            {
                assemblies = AppDomain.CurrentDomain.GetAssemblies();
            }

            var cqrsContext = new CqrsContext(wrapperizerContext.Services, serviceLifetime);

            configure?.Invoke(cqrsContext);

            var ass = new List <Assembly> {
                typeof(IMediator).Assembly, typeof(IQuery <>).Assembly
            };

            ass.AddRange(assemblies);

            wrapperizerContext.Services.AddMediatR(ass, configuration => configuration = cqrsContext.ServiceLifetime switch
            {
                ServiceLifetime.Singleton => configuration.AsSingleton(),
                ServiceLifetime.Scoped => configuration.AsScoped(),
                _ => configuration.AsTransient()
            });
        public static WrapperizerContext AddUnitOfWork <T>(this WrapperizerContext wrapperizerBuilder,
                                                           ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
            where T : IUnitOfWork
        {
            wrapperizerBuilder.Services.Add(
                new ServiceDescriptor(
                    typeof(IUnitOfWork), provider => provider.GetRequiredService <T>(),
                    serviceLifetime));

            return(wrapperizerBuilder);
        }
        public static WrapperizerContext AddCrudRepositories <TU>
            (this WrapperizerContext wrapperizerContext,
            Action <IServiceProvider, DbContextOptionsBuilder> optionsAction,
            ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
            where TU : DbContext
        {
            wrapperizerContext.Services
            .AddDbContext <DbContext, TU>(optionsAction ?? throw new ArgumentNullException(nameof(optionsAction)));

            wrapperizerContext.Services.Add(
                new ServiceDescriptor(typeof(ICrudRepository <>), typeof(EfCoreCrudRepository <>),
                                      serviceLifetime));

            return(wrapperizerContext);
        }
        public static WrapperizerContext AddModelDbContext <TIContext, TDbContext>(this WrapperizerContext ringanaContext,
                                                                                   Action <IServiceProvider, DbContextOptionsBuilder> optionsAction,
                                                                                   ServiceLifetime contextLifetime = ServiceLifetime.Scoped,
                                                                                   ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) where TDbContext : DbContext
        {
            if (ringanaContext == null)
            {
                throw new ArgumentNullException(nameof(ringanaContext));
            }

            ringanaContext.Services.AddDbContext <TDbContext>(optionsAction, contextLifetime, optionsLifetime);
            ringanaContext.Services.Add(new ServiceDescriptor(typeof(TIContext), typeof(TDbContext), contextLifetime));

            return(ringanaContext);
        }
        public static WrapperizerContext AddMessageRelayServices(
            this WrapperizerContext builder,
            Action <DbContextOptionsBuilder> optionsBuilder)
        {
            if (_outboxServicesEnabled)
            {
                throw new InvalidOperationException(InvalidOperationExceptionMessage);
            }

            builder.Services.AddDbContext <OutboxEventContext>(optionsBuilder, Singleton, Singleton);
            builder.Services.AddSingleton <IOutboxEventService, OutboxEventService>();
            builder.Services.AddSingleton <IOutboxMessageRelay, OutboxMessageRelay>();

            _messageRelayServicesEnabled = true;
            return(builder); //.Services;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="optionsBuilder">This should point to the same database as the one for UnitOfWork</param>
        /// <param name="configure"></param>
        /// <param name="enableAutoMigration"></param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"></exception>
        public static WrapperizerContext AddOutboxServices(
            this WrapperizerContext builder,
            Action <DbContextOptionsBuilder> optionsBuilder, Action <TransactionalOutboxConfiguration> configure = null,
            bool enableAutoMigration = true)
        {
            if (_messageRelayServicesEnabled)
            {
                throw new InvalidOperationException(InvalidOperationExceptionMessage);
            }

            var configuration = new TransactionalOutboxConfiguration {
                AutoPublish = false
            };

            configure?.Invoke(configuration);

            builder.Services.AddSingleton(configuration);

            builder.Services.AddDbContext <OutboxEventContext>(optionsBuilder);

            // ToDo: Change this to accept db from outside services not only the connection
            // and bear in mind that this one is a factory method
            builder.Services.AddTransient <Func <IOutboxEventService> >(
                services => () =>
            {
                var outboxEventContext = services.GetRequiredService <OutboxEventContext>();
                // new OutboxEventContext(
                // new DbContextOptionsBuilder<OutboxEventContext>()
                //     .UseSqlServer(dbConnection)
                //     .Options);

                return(new OutboxEventService(outboxEventContext));
            });

            builder.Services.AddTransient <ITransactionalOutboxService, TransactionalOutboxService>();

            builder.Services.AddScoped <IOutboxMessageRelay, OutboxMessageRelay>();

            if (enableAutoMigration)
            {
                builder.Services.AddHostedService <OutboxMigratorHostedService>();
            }

            _outboxServicesEnabled = true;
            return(builder); //.Services;
        }
Пример #7
0
        public static WrapperizerContext AddRepositories(this WrapperizerContext wrapperizerBuilder,
                                                         ServiceLifetime lifetime = ServiceLifetime.Scoped)
        {
            var repositories = GetRepositoryTypes();
            var services     = wrapperizerBuilder.Services;

            repositories.ForEach(definition =>
            {
                services.AddScoped(definition.Implementation);
                definition.Interfaces.ForEach(@interface =>
                {
                    services.AddScoped(@interface,
                                       provider => provider.GetRequiredService(definition.Implementation));
                });
            });

            return(wrapperizerBuilder);
        }
Пример #8
0
 public static WrapperizerContext AddConsul(this WrapperizerContext builder,
                                            Action <ConsulClientConfiguration> configurator)
 {
     builder.Services.AddSingleton <IConsulClient, ConsulClient>(p => new ConsulClient(configurator));
     return(builder);
 }