Ejemplo n.º 1
0
        /// <summary>
        /// Get Microsoft EntityFramework Core options
        /// </summary>
        /// <param name="optAct"></param>
        /// <returns></returns>
        public static EfCoreOptions CreateOptions(Action <EfCoreOptions> optAct = null)
        {
            var opt = new EfCoreOptions();

            optAct?.Invoke(opt);
            return(opt);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Use EntityFramework Core with MySql
        /// </summary>
        /// <param name="context"></param>
        /// <param name="optAct"></param>
        /// <typeparam name="TContextService"></typeparam>
        /// <typeparam name="TContextImplementation"></typeparam>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        public static IDbContextConfig UseEfCoreWithMySql <TContextService, TContextImplementation>(
            this DbContextConfig context, Action <EfCoreOptions> optAct = null)
            where TContextService : IEfContext
            where TContextImplementation : DbContext, TContextService
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var opt = new EfCoreOptions();

            optAct?.Invoke(opt);

            context.RegisterDbContext(services =>
            {
                if (!string.IsNullOrWhiteSpace(opt.ConnectionString))
                {
                    services.AddDbContext <TContextService, TContextImplementation>(o => o.UseMySql(opt.ConnectionString));
                }
                else if (!string.IsNullOrWhiteSpace(opt.ConnectionName))
                {
                    services.AddDbContext <TContextService, TContextImplementation>((p, o) =>
                                                                                    o.UseMySql(p.GetService <IConfigurationRoot>().GetConnectionString(opt.ConnectionName)));
                }
                else
                {
                    throw new InvalidOperationException("Connection name or string cannot be empty.");
                }
            });

            return(context);
        }
        /// <summary>
        /// Try get
        /// </summary>
        /// <param name="options"></param>
        /// <typeparam name="TContext"></typeparam>
        /// <returns></returns>
        public static bool TryGet <TContext>(out EfCoreOptions options)
            where TContext : DbContext, IEfContext
        {
            var type = typeof(TContext);

            return(_efCoreOptionsCache.TryGetValue(type, out options));
        }
        /// <summary>
        /// Register
        /// </summary>
        /// <param name="options"></param>
        /// <typeparam name="TContext"></typeparam>
        /// <exception cref="ArgumentException"></exception>
        public static void Register <TContext>(EfCoreOptions options)
            where TContext : DbContext, IEfContext
        {
            var type = typeof(TContext);

            if (_efCoreOptionsCache.ContainsKey(type))
            {
                throw new ArgumentException($"Options has been registered for '{type}'");
            }
            _efCoreOptionsCache.TryAdd(type, options);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Guard Microsoft EntityFramework Core options options
        /// </summary>
        /// <param name="options"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public static void GuardOptions(EfCoreOptions options)
        {
            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (string.IsNullOrWhiteSpace(options.ConnectionString))
            {
                throw new ArgumentNullException(nameof(options.ConnectionString));
            }
        }
Ejemplo n.º 6
0
        public static IServiceCollection AddEfCore <TContext>(this IServiceCollection services, IConfiguration Configuration)
            where TContext : DbContext
        {
            var option = new EfCoreOptions();

            Configuration.GetSection(nameof(EfCoreOptions)).Bind(option);

            services.AddDbContext <TContext>(
                options =>
                options.UseSqlServer(
                    option.ConnectionString
                    ));

            services.AddScoped <IEfCoreService, EfCoreService <TContext> >();
            return(services);
        }
Ejemplo n.º 7
0
        public static IServiceCollection AddEfCore <TContext>(this IServiceCollection services)
            where TContext : DbContext
        {
            if (EasySharpServices.IsInitialized == false)
            {
                EasySharpServices.Services = services;
                EasySharpServices.Initialize();
            }

            Configuration = EasySharpServices.Builder();

            var option = new EfCoreOptions();

            Configuration.GetSection(nameof(EfCoreOptions)).Bind(option);

            services.AddDbContext <TContext>(
                options =>
                options.UseSqlServer(
                    option.ConnectionString
                    ));

            return(services);
        }
Ejemplo n.º 8
0
        public static IServiceCollection AddOutbox(this IServiceCollection services)
        {
            Configuration = EasySharpServices.Builder();

            var options = new OutboxOptions();

            Configuration.GetSection(nameof(OutboxOptions)).Bind(options);
            services.Configure <OutboxOptions>(Configuration.GetSection(nameof(OutboxOptions)));

            var dbContextOptions = new EfCoreOptions();

            Configuration.GetSection(nameof(EfCoreOptions)).Bind(dbContextOptions);

            if (options.Enable == false)
            {
                return(services);
            }

            switch (options.OutboxType.ToLowerInvariant())
            {
            case "efcore":
            case "ef":
                services.AddEfCoreOutboxStore(opts =>
                                              opts.UseSqlServer(
                                                  dbContextOptions.ConnectionString
                                                  ));
                break;

            default:
                throw new Exception($"Outbox type '{options.OutboxType}' is not supported");
            }

            services.AddScoped <IOutboxListener, OutboxListener>();
            services.AddHostedService <OutboxProcessor>();

            return(services);
        }
Ejemplo n.º 9
0
        public static IServiceCollection AddEventStore <TAggregate>(this IServiceCollection services) where TAggregate : IAggregate
        {
            Configuration = EasySharpServices.Builder();

            var options = new EventStoresOptions();

            Configuration.GetSection(nameof(EventStoresOptions)).Bind(options);
            services.Configure <EventStoresOptions>(Configuration.GetSection(nameof(EventStoresOptions)));

            var dbContextOptions = new EfCoreOptions();

            Configuration.GetSection(nameof(EfCoreOptions)).Bind(dbContextOptions);

            if (options.Enable == false)
            {
                return(services);
            }

            switch (options.EventStoreType.ToLowerInvariant())
            {
            case "efcore":
            case "ef":
                services.AddEfCoreEventStore(opts =>
                                             opts.UseSqlServer(
                                                 dbContextOptions.ConnectionString
                                                 ));
                break;

            default:
                throw new Exception($"Event store type '{options.EventStoreType}' is not supported");
            }

            services.AddScoped <IRepository <TAggregate>, Repository <TAggregate> >();
            services.AddScoped <IEventStore, EventStore>();

            return(services);
        }