Пример #1
0
        private void MockGenerator()
        {
            var optionsMock = new Mock <IOptions <DapperDatabaseOptions> >();
            var options     = new DapperDatabaseOptions
            {
                DefaultConnectionName = "default",
                ConnectionStrings     = new Dictionary <string, string>
                {
                    { "default", helper.ConnectionString }
                }
            };
            var builder = new DapperDataFeatureBuilder(options);

            builder.Conventions(convention =>
            {
                convention
                .AutoGenerateKey <TimeTest>(x => x.Id)
                .Ignore <TimeTest>(x => x.Time)
                .Types(s => s == typeof(TimeTest))
                .IsEntity();
            });
            builder.Build();
            optionsMock.Setup(o => o.Value).Returns(options);
            var loggerFactory = new LoggerFactory();

            loggerFactory.AddDebug(LogLevel.Trace);
            _testContext = new DapperContext(new DapperRuntime(optionsMock.Object,
                                                               new IDapperMetadataProvider[] { new DapperMetadataProviderInstance(), })
                                             , loggerFactory);
            _dapper  = new DapperRepository <DapperTestEntity>(_testContext);
            _dapper1 = new DapperRepository <TimeTest>(_testContext);
        }
Пример #2
0
        /// <summary>
        /// 添加以 Dapper 作为持久化的数据层特性。
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="setup"></param>
        /// <returns></returns>
        public static SchubertServicesBuilder AddDapperDataFeature(this SchubertServicesBuilder builder, Action <DapperDataFeatureBuilder> setup = null)
        {
            DapperDatabaseOptions dbOptions = new DapperDatabaseOptions();

            if (builder.AddedModules.Add(_module))
            {
                //修改dapper的默认映射规则,让其支持下划线列名到C#实体驼峰命名属性
                Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;

                var configuration = builder.Configuration.GetSection("Schubert:Data") as IConfiguration ?? new ConfigurationBuilder().Build();

                builder.ServiceCollection.Configure <DapperDatabaseOptions>(configuration);

                var schubertDataSetup = new ConfigureFromConfigurationOptions <DapperDatabaseOptions>(configuration);
                schubertDataSetup.Configure(dbOptions);
            }

            DapperDataFeatureBuilder featureBuilder = new DapperDataFeatureBuilder(dbOptions);

            setup?.Invoke(featureBuilder);


            featureBuilder.Build();
            builder.ServiceCollection.Configure(featureBuilder.Configure);

            builder.ServiceCollection.AddSmart(DapperServices.GetServices(dbOptions));
            return(builder);
        }
Пример #3
0
        public static IEnumerable <SmartServiceDescriptor> GetServices(DapperDatabaseOptions options)
        {
            Guard.ArgumentNotNull(options, nameof(options));

            yield return(ServiceDescriber.Scoped <DapperContext, DapperContext> ());

            yield return(ServiceDescriber.Scoped <IDatabaseContext>(s => s.GetRequiredService <DapperContext>()));

            yield return(ServiceDescriber.Singleton <DapperRuntime, DapperRuntime>());

            //yield return ServiceDescriber.Scoped<IRepository<ShellDescriptorRecord>, Repository<ShellDescriptorRecord, ShellDescriptorDbContext>>();
            //yield return ServiceDescriber.Scoped<IRepository<SettingRecord>, Repository<SettingRecord, ShellDescriptorDbContext>>();
            yield return(ServiceDescriber.Scoped(typeof(IRepository <>), typeof(DapperRepository <>)));

            //yield return ServiceDescriber.Transient<IShellDescriptorManager, ShellDescriptorManager>();
            //yield return ServiceDescriber.Scoped<ILanguageService, LanguageService>();
        }
Пример #4
0
 public DapperDataFeatureBuilder(DapperDatabaseOptions options = null)
 {
     this.DataProviders = new Dictionary <string, Type>();
     _options           = options;
 }