/// <summary> /// 注入仓储结构 By 程序集 /// </summary> /// <param name="builder"></param> /// <param name="setupOptions"></param> /// <returns></returns> public static SmartSqlDIBuilder AddRepositoryFromAssembly(this SmartSqlDIBuilder builder, Action <AssemblyAutoRegisterOptions> setupOptions) { builder.AddRepositoryFactory(); var options = new AssemblyAutoRegisterOptions { Filter = (type) => type.IsInterface }; setupOptions(options); ScopeTemplateParser templateParser = new ScopeTemplateParser(options.ScopeTemplate); var allTypes = TypeScan.Scan(options); foreach (var type in allTypes) { builder.Services.AddSingleton(type, sp => { ISqlMapper sqlMapper = sp.GetRequiredService <ISqlMapper>();; if (!String.IsNullOrEmpty(options.SmartSqlAlias)) { sqlMapper = sp.EnsureSmartSql(options.SmartSqlAlias).SqlMapper; } var factory = sp.GetRequiredService <IRepositoryFactory>(); var scope = string.Empty; if (!String.IsNullOrEmpty(options.ScopeTemplate)) { scope = templateParser.Parse(type.Name); } return(factory.CreateInstance(type, sqlMapper, scope)); }); } return(builder); }
public static SmartSqlDIBuilder AddCapRepository(this SmartSqlDIBuilder builder) { builder.AddRepositoryFromAssembly(options => options.AssemblyString = typeof(CapRepositoryExtensions).Assembly.GetName().Name); return(builder); }
public static SmartSqlDIBuilder AddInvokeSync(this SmartSqlDIBuilder smartSqlDiBuilder, Action <SyncFilterOptions> configure) { SyncFilterOptions syncFilterOptions = new SyncFilterOptions(); configure?.Invoke(syncFilterOptions); smartSqlDiBuilder.Services.AddSingleton(syncFilterOptions); smartSqlDiBuilder.Services.TryAddSingleton <ISyncFilter, SyncFilter>(); smartSqlDiBuilder.Services.TryAddSingleton <ISyncService, SyncService>(); return(smartSqlDiBuilder); }
public static SmartSqlDIBuilder AddKafkaPublisher(this SmartSqlDIBuilder smartSqlDiBuilder, Action <KafkaOptions> configure) { KafkaOptions rabbitMqOptions = new KafkaOptions(); configure?.Invoke(rabbitMqOptions); smartSqlDiBuilder.Services.AddSingleton(rabbitMqOptions); smartSqlDiBuilder.Services.TryAddSingleton <IPublisher, KafkaPublisher>(); smartSqlDiBuilder.Services.TryAddSingleton <ISubscriber, KafkaSubscriber>(); return(smartSqlDiBuilder); }
public static SmartSqlDIBuilder AddRabbitMQPublisher(this SmartSqlDIBuilder smartSqlDiBuilder, Action <RabbitMQOptions> configure) { RabbitMQOptions rabbitMqOptions = new RabbitMQOptions(); configure?.Invoke(rabbitMqOptions); smartSqlDiBuilder.Services.AddSingleton(rabbitMqOptions); smartSqlDiBuilder.Services.TryAddSingleton <PersistentConnection>(); smartSqlDiBuilder.Services.TryAddSingleton <IPublisher, RabbitMQPublisher>(); smartSqlDiBuilder.Services.TryAddSingleton <ISubscriber, RabbitMQSubscriber>(); return(smartSqlDiBuilder); }
public static SmartSqlDIBuilder AddKafkaSubscriber(this SmartSqlDIBuilder smartSqlDiBuilder, Action <KafkaOptions> configure) { KafkaOptions kafkaOptions = new KafkaOptions(); configure?.Invoke(kafkaOptions); smartSqlDiBuilder.Services.AddSingleton <ISubscriber, KafkaSubscriber>(sp => { var loggerFactory = sp.GetRequiredService <ILoggerFactory>(); return(new KafkaSubscriber(kafkaOptions, loggerFactory.CreateLogger <KafkaSubscriber>())); }); return(smartSqlDiBuilder); }
/// <summary> /// 注入单个仓储接口 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="builder"></param> /// <param name="smartSqlAlias"></param> /// <param name="scope"></param> /// <returns></returns> public static SmartSqlDIBuilder AddRepository <T>(this SmartSqlDIBuilder builder, string smartSqlAlias, string scope = "") where T : class { builder.AddRepositoryFactory(); builder.Services.AddSingleton <T>(sp => { ISqlMapper sqlMapper = sp.GetRequiredService <ISqlMapper>();; if (!String.IsNullOrEmpty(smartSqlAlias)) { sqlMapper = sp.EnsureSmartSql(smartSqlAlias).SqlMapper; } var factory = sp.GetRequiredService <IRepositoryFactory>(); return(factory.CreateInstance(typeof(T), sqlMapper, scope) as T); }); return(builder); }
public static SmartSqlDIBuilder AddRabbitMQSubscriber(this SmartSqlDIBuilder smartSqlDiBuilder, Action <SubscriberOptions> configure) { SubscriberOptions rabbitMqOptions = new SubscriberOptions(); configure?.Invoke(rabbitMqOptions); smartSqlDiBuilder.Services.AddSingleton <ISubscriber, RabbitMQSubscriber>(sp => { var loggerFactory = sp.GetRequiredService <ILoggerFactory>(); return(new RabbitMQSubscriber(loggerFactory.CreateLogger <RabbitMQSubscriber>() , rabbitMqOptions , new PersistentConnection(rabbitMqOptions, loggerFactory.CreateLogger <PersistentConnection>() ))); }); return(smartSqlDiBuilder); }
/// <summary> /// 注入SmartSql仓储工厂 /// </summary> /// <param name="builder"></param> /// <param name="scope_template">Scope模板,默认:I{Scope}Repository</param> /// <param name="sqlIdNamingConvert">SqlId命名转换</param> /// <returns></returns> public static SmartSqlDIBuilder AddRepositoryFactory(this SmartSqlDIBuilder builder, string scope_template = "", Func <Type, MethodInfo, String> sqlIdNamingConvert = null) { builder.Services.TryAddSingleton <IRepositoryBuilder>((sp) => { var loggerFactory = sp.GetService <ILoggerFactory>() ?? Logging.Abstractions.NullLoggerFactory.Instance; var logger = loggerFactory.CreateLogger <EmitRepositoryBuilder>(); return(new EmitRepositoryBuilder(scope_template, sqlIdNamingConvert, logger)); }); builder.Services.TryAddSingleton <IRepositoryFactory>((sp) => { var loggerFactory = sp.GetService <ILoggerFactory>() ?? Logging.Abstractions.NullLoggerFactory.Instance; var logger = loggerFactory.CreateLogger <RepositoryFactory>(); var repositoryBuilder = sp.GetRequiredService <IRepositoryBuilder>(); return(new RepositoryFactory(repositoryBuilder, logger)); }); return(builder); }