Example #1
0
        public static DapperContextOptionsBuilder UseSqlServerUserSecrets(this DapperContextOptionsBuilder pOptionsBuilder, IConfiguration pConfiguration, string pConnection = "DefaultConnection",
                                                                          Action <SqlConnectionStringBuilder> optionsAction = null)
        {
            var xConnectionString = pConfiguration.GetConnectionString(pConnection);

            if (string.IsNullOrWhiteSpace(xConnectionString))
            {
                throw new KeyNotFoundException(pConnection + " em IConfiguration não encontrada");
            }

            var xConnection = pConnection + "Password";
            var xPassword   = pConfiguration[xConnection];

            if (string.IsNullOrWhiteSpace(xPassword))
            {
                throw new KeyNotFoundException(xConnection + " em IConfiguration não encontrada");
            }

            var xConnectionStringBuilder = new SqlConnectionStringBuilder(xConnectionString)
            {
                Password = xPassword,
                MultipleActiveResultSets = true
            };

            return(pOptionsBuilder.UseSqlServer(xConnectionStringBuilder, optionsAction));
        }
        public static IServiceCollection AddDapperDbContext <TDapperContext>(
            [NotNull] this IServiceCollection serviceCollection,
            [NotNull] Action <DapperContextOptionsBuilder> optionsAction,
            ServiceLifetime contextLifetime = ServiceLifetime.Scoped)
            where TDapperContext : DapperContext
        {
            var xBuilder = new DapperContextOptionsBuilder <TDapperContext>(new DapperContextOptions <TDapperContext>(new Dictionary <Type, dynamic>()));

            optionsAction.Invoke(xBuilder);

            serviceCollection.Add(new ServiceDescriptor(typeof(DapperContextOptions <TDapperContext>), p => xBuilder.DapperContextOptions, contextLifetime));
            serviceCollection.Add(new ServiceDescriptor(typeof(TDapperContext), typeof(TDapperContext), contextLifetime));
            return(serviceCollection);
        }