/// <summary> /// Use unit of work /// </summary> public static ServicesBuilderOptions UseUnitOfWork(this ServicesBuilderOptions options, Action <UnitOfWorkOptions> uowOptions = null) { var defaultUowOptins = new UnitOfWorkOptions(); uowOptions?.Invoke(defaultUowOptins); options.Services.AddSingleton(defaultUowOptins); options.Services.OnRegistred(context => { if (context.ImplementationType.IsDefined(typeof(UnitOfWorkAttribute), true)) { context.Interceptors.Add <UnitOfWorkInterceptor>(); return; } var methods = context.ImplementationType.GetMethods( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (methods.Any(m => m.IsDefined(typeof(UnitOfWorkAttribute), true))) { context.Interceptors.Add <UnitOfWorkInterceptor>(); return; } if (defaultUowOptins.ConventionalUowSelectors.Any(selector => selector(context.ImplementationType))) { context.Interceptors.Add <UnitOfWorkInterceptor>(); return; } }); options.Services.RegisterAssemblyByBasicInterface(typeof(UowServicesBuilderExtension).Assembly); return(options); }
/// <inheritdoc /> public override void RegisterAssemblyByBasicInterface(ServicesBuilderOptions servicesBuilderOptions, Assembly assembly) { var assemblyTypes = assembly.GetTypes(); assemblyTypes .Where(type => type.IsClass && !type.IsAbstract && type.GetInterfaces().Any(i => i.GetTypeInfo() == typeof(ILocalEventHandler))).ToList() .ForEach(x => { if (!servicesBuilderOptions.LocalEventBusOptions.Handlers.Contains(x)) { servicesBuilderOptions.LocalEventBusOptions.Handlers.Add(x); } }); assemblyTypes .Where(type => type.IsClass && !type.IsAbstract && type.GetInterfaces().Any(i => i.GetTypeInfo() == typeof(IBackgroundJob))).ToList() .ForEach(x => { servicesBuilderOptions.BackgroundJobOptions.TryAddJob(x); }); var transientTypes = assemblyTypes.Where(type => type.GetInterfaces().Any(i => i.GetTypeInfo() == typeof(ITransientDependency)) && !type.IsAbstract && !type.GetTypeInfo().IsGenericTypeDefinition) .ToArray(); foreach (var transientType in transientTypes) { _builder.RegisterType(transientType).AsImplementedInterfaces().AddLifeStyle(DependencyLifeStyle.Transient); _builder.RegisterType(transientType).AddLifeStyle(DependencyLifeStyle.Transient); } var scopedTypes = assemblyTypes.Where(type => type.GetInterfaces().Any(i => i.GetTypeInfo() == typeof(IScopedDependency)) && !type.IsAbstract && !type.GetTypeInfo().IsGenericTypeDefinition) .ToArray(); foreach (var scopedType in scopedTypes) { _builder.RegisterType(scopedType).AsImplementedInterfaces().AddLifeStyle(DependencyLifeStyle.Scoped); _builder.RegisterType(scopedType).AddLifeStyle(DependencyLifeStyle.Scoped); } var singletonTypes = assemblyTypes.Where(type => type.GetInterfaces().Any(i => i.GetTypeInfo() == typeof(ISingletonDependency)) && !type.IsAbstract && !type.GetTypeInfo().IsGenericTypeDefinition) .ToArray(); foreach (var singletonType in singletonTypes) { _builder.RegisterType(singletonType).AsImplementedInterfaces().AddLifeStyle(DependencyLifeStyle.Singleton); _builder.RegisterType(singletonType).AddLifeStyle(DependencyLifeStyle.Singleton); } }
/// <summary> /// Use EfCore Module /// </summary> public static ServicesBuilderOptions UseEfCore(this ServicesBuilderOptions builder) { builder.IocRegister.Register( typeof(IRepository <,>), typeof(RepositoryBase <,>), lifeStyle: DependencyLifeStyle.Transient); builder.IocRegister.RegisterAssemblyByBasicInterface(typeof(EfCoreServicesBuilderExtension).Assembly); return(builder); }
/// <summary> /// Use EfCore Module /// </summary> public static ServicesBuilderOptions UseEfCore(this ServicesBuilderOptions options, Action <UnitOfWorkOptions> uowOptions = null) { options.UseUnitOfWork(uowOptions); options.Services.AddTransient(typeof(IRepository <,>), typeof(RepositoryBase <,>)); options.Services.AddTransient(typeof(IDbContextProvider), typeof(DbContextProvider)); options.Services.RegisterAssemblyByBasicInterface(typeof(EfCoreServicesBuilderExtension).Assembly); return(options); }
/// <summary> /// Use Dapper as the Orm framework /// </summary> public static ServicesBuilderOptions UseDapper(this ServicesBuilderOptions builder) { builder.IocRegister.Register( typeof(IRepository <,>), typeof(RepositoryBase <,>), lifeStyle: DependencyLifeStyle.Transient); builder.IocRegister.RegisterAssemblyByBasicInterface(typeof(DapperServicesBuilderExtension).Assembly); DapperExtensions.DapperExtensions.DefaultMapper = typeof(PluralizedAutoClassMapper <>); return(builder); }
/// <summary> /// Use Dapper as the Orm framework /// </summary> public static ServicesBuilderOptions UseDapper(this ServicesBuilderOptions options, Action <UnitOfWorkOptions> uowOptions = null) { options.UseUnitOfWork(uowOptions); options.Services.AddTransient(typeof(IRepository <,>), typeof(RepositoryBase <,>)); options.Services.AddTransient(typeof(IDatabaseProvider), typeof(DatabaseProvider)); options.Services.RegisterAssemblyByBasicInterface(typeof(DapperServicesBuilderExtension).Assembly); DapperExtensions.DapperExtensions.DefaultMapper = typeof(PluralizedAutoClassMapper <>); return(options); }
/// <summary> /// Add application framework service /// </summary> public static IServiceProvider AddCreekdream(this IServiceCollection services, Action <ServicesBuilderOptions> options = null) { if (services == null) { throw new ArgumentNullException(nameof(services)); } var builder = new ServicesBuilderOptions(); options?.Invoke(builder); return(builder.Build(services)); }
/// <inheritdoc /> public override void RegisterAssemblyByBasicInterface(ServicesBuilderOptions servicesBuilderOptions, Assembly assembly) { var assemblyTypes = assembly.GetTypes(); assemblyTypes .Where(type => !type.IsAbstract && !type.IsClass).ToList() .ForEach(x => { if (!servicesBuilderOptions.LocalEventBusOptions.Handlers.Contains(x)) { servicesBuilderOptions.LocalEventBusOptions.Handlers.Add(x); } }); assemblyTypes .Where(type => type.IsClass && !type.IsAbstract && type.GetInterfaces().Any(i => i.GetTypeInfo() == typeof(IBackgroundJob))).ToList() .ForEach(x => { servicesBuilderOptions.BackgroundJobOptions.TryAddJob(x); }); _container.Register(Classes.FromAssembly(assembly) .IncludeNonPublicTypes() .BasedOn <IScopedDependency>() .If(type => !type.GetTypeInfo().IsGenericTypeDefinition) .WithService.Self() .WithService.DefaultInterfaces() .LifestyleScoped <LocalLifetimeScopeAccessor>() ); _container.Register(Classes.FromAssembly(assembly) .IncludeNonPublicTypes() .BasedOn <ITransientDependency>() .If(type => !type.GetTypeInfo().IsGenericTypeDefinition) .WithService.Self() .WithService.DefaultInterfaces() .LifestyleTransient() ); _container.Register(Classes.FromAssembly(assembly) .IncludeNonPublicTypes() .BasedOn <ISingletonDependency>() .If(type => !type.GetTypeInfo().IsGenericTypeDefinition) .WithService.Self() .WithService.DefaultInterfaces() .LifestyleSingleton() ); }
/// <summary> /// Add an SimpleDemoApplication module /// </summary> public static ServicesBuilderOptions AddSimpleDemoApplication(this ServicesBuilderOptions builder) { builder.Services.OnRegistred(context => { if (context.ImplementationType.IsDefined(typeof(AuditLogAttribute), true)) { context.Interceptors.Add <AuditLogInterceptor>(); return; } var methods = context.ImplementationType.GetMethods( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (methods.Any(m => m.IsDefined(typeof(AuditLogAttribute), true))) { context.Interceptors.Add <AuditLogInterceptor>(); return; } }); builder.Services.AddAutoMapper(typeof(BookProfile), typeof(UserProfile)); builder.Services.RegisterAssemblyByBasicInterface(typeof(SimpleDemoApplicationServicesBuilderExtension).Assembly); return(builder); }
/// <summary> /// General interface implementation in the registration assembly /// </summary> public abstract void RegisterAssemblyByBasicInterface(ServicesBuilderOptions servicesBuilderOptions, Assembly assembly);
/// <summary> /// Add a ProjectName core module /// </summary> public static ServicesBuilderOptions AddProjectNameCore(this ServicesBuilderOptions builder) { builder.Services.RegisterAssemblyByBasicInterface(typeof(ProjectNameCoreServicesBuilderExtension).Assembly); return(builder); }
/// <summary> /// Add a SimpleDemo core module /// </summary> public static ServicesBuilderOptions AddSimpleDemoCore(this ServicesBuilderOptions builder) { builder.IocRegister.RegisterAssemblyByBasicInterface(typeof(SimpleDemoCoreServicesBuilderExtension).Assembly); return(builder); }
/// <summary> /// Add a core demo module /// </summary> public static ServicesBuilderOptions AddDemoCore(this ServicesBuilderOptions servicesBuilderOptions) { servicesBuilderOptions.IocRegister.RegisterAssemblyByBasicInterface(servicesBuilderOptions, typeof(CoreServicesBuilderOptionsExtension).Assembly); return(servicesBuilderOptions); }
/// <summary> /// Use Autofac as an injection container /// </summary> public static ServicesBuilderOptions UseAutofac(this ServicesBuilderOptions builder) { builder.IocRegister = new AutofacIocRegister(); builder.IocRegister.Register <IIocResolver, AutofacIocResolver>(DependencyLifeStyle.Transient); return(builder); }
/// <summary> /// Use Castle Windsor as an injection container /// </summary> public static ServicesBuilderOptions UseWindsor(this ServicesBuilderOptions servicesBuilderOptions) { servicesBuilderOptions.IocRegister = new WindsorIocRegister(); servicesBuilderOptions.IocRegister.Register <IIocResolver, WindsorIocResolver>(DependencyLifeStyle.Transient); return(servicesBuilderOptions); }
/// <summary> /// Add a ProjectName application module /// </summary> public static ServicesBuilderOptions AddProjectNameApplication(this ServicesBuilderOptions builder) { builder.Services.AddAutoMapper(typeof(BookProfile)); builder.Services.RegisterAssemblyByBasicInterface(typeof(ProjectNameApplicationServicesBuilderExtension).Assembly); return(builder); }