public static IServiceLocator Configure(Database database, string connectionString, bool withAspects, bool externalConfiguration) { var state = new SystemState(); var builder = new ContainerBuilder(); builder.RegisterInstance(state).As<ISystemState>(); SetupExtensibility(builder, withAspects, externalConfiguration); if (database == Core.Database.Postgres) SetupPostgres(builder, connectionString); //else //SetupOracle(builder, connectionString); SetupPatterns(builder); SetupSerialization(builder); builder.RegisterType<PermissionManager>().As<IPermissionManager>().SingleInstance(); builder.RegisterType<OnContainerBuild>().As<IStartable>(); if (externalConfiguration) builder.RegisterModule(new ConfigurationSettingsReader("autofacConfiguration")); var factory = builder.Build().Resolve<IObjectFactory>(); state.IsBooting = false; state.Started(factory); return factory.Resolve<IServiceLocator>(); }
/// <summary> /// Apply the module to the component registry. /// </summary> /// <param name="componentRegistry">Component registry to apply configuration to.</param> public void Configure(IComponentRegistry componentRegistry) { if (componentRegistry == null) throw new ArgumentNullException("componentRegistry"); var moduleBuilder = new ContainerBuilder(); Load(moduleBuilder); moduleBuilder.Update(componentRegistry); AttachToRegistrations(componentRegistry); AttachToSources(componentRegistry); }
protected override void Load(Extensibility.Autofac.ContainerBuilder builder) { var cs = ConfigurationManager.AppSettings["Revenj.ConnectionString"] ?? ConfigurationManager.AppSettings["ConnectionString"]; if (string.IsNullOrEmpty(cs)) { throw new ConfigurationErrorsException(@"ConnectionString is missing from configuration. Add ConnectionString to <appSettings> Example: <add key=""ConnectionString"" value=""server=postgres.localhost;port=5432;database=MyDatabase;user=postgres;password=123456;encoding=unicode"" />"); } Revenj.DatabasePersistence.Postgres.Setup.ConfigurePostgres(builder, cs); base.Load(builder); }
protected override void Load(Extensibility.Autofac.ContainerBuilder builder) { var cs = ConfigurationManager.AppSettings["Revenj.ConnectionString"] ?? ConfigurationManager.AppSettings["ConnectionString"]; if (string.IsNullOrEmpty(cs)) { throw new ConfigurationErrorsException(@"ConnectionString is missing from configuration. Add ConnectionString to <appSettings> Example: <add key=""ConnectionString"" value=""Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyOracleHost)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=xe)));User Id=oracle;Password=123456;"" />"); } Revenj.DatabasePersistence.Oracle.Setup.ConfigureOracle(builder, cs); base.Load(builder); }
/// <summary> /// Override to add registrations to the container. /// </summary> /// <remarks> /// Note that the ContainerBuilder parameter is unique to this module. /// </remarks> /// <param name="builder">The builder through which components can be /// registered.</param> protected virtual void Load(ContainerBuilder builder) { }
private void BuildScopeIfRequired() { if (!ShouldBuildScope) return; lock (sync) { if (!ShouldBuildScope) return; RegistrationCache.Clear(); var cb = new ContainerBuilder(); RegisterNew(cb); cb.Update(CurrentScope.ComponentRegistry); FactoryBuilders.Clear(); AutofacBuilders.Clear(); SimpleCache.Clear(); ServiceCache.Clear(); CacheWithArguments.Clear(); ShouldBuildScope = false; } }
internal static void RegisterToContainer(ContainerBuilder cb, IObjectFactoryBuilder rb, ConcurrentDictionary<Type, bool> cache) { foreach (var item in rb.Types.Where(i => i.IsGeneric == false)) { if (cache != null) { if (item.AsType != null) foreach (var t in item.AsType) cache.TryAdd(t, true); else cache.TryAdd(item.Type, true); } switch (item.Scope) { case InstanceScope.Transient: if (item.AsType == null || item.AsType.Length == 0) cb.RegisterType(item.Type); else cb.RegisterType(item.Type).As(item.AsType); break; case InstanceScope.Singleton: if (item.AsType == null || item.AsType.Length == 0) cb.RegisterType(item.Type).SingleInstance(); else cb.RegisterType(item.Type).As(item.AsType).SingleInstance(); break; default: if (item.AsType == null || item.AsType.Length == 0) cb.RegisterType(item.Type).InstancePerLifetimeScope(); else cb.RegisterType(item.Type).As(item.AsType).InstancePerLifetimeScope(); break; } } foreach (var item in rb.Types.Where(i => i.IsGeneric)) { if (cache != null) { if (item.AsType != null) foreach (var t in item.AsType) cache.TryAdd(t, true); else cache.TryAdd(item.Type, true); } switch (item.Scope) { case InstanceScope.Transient: if (item.AsType == null || item.AsType.Length == 0) cb.RegisterGeneric(item.Type); else cb.RegisterGeneric(item.Type).As(item.AsType); break; case InstanceScope.Singleton: if (item.AsType == null || item.AsType.Length == 0) cb.RegisterGeneric(item.Type).SingleInstance(); else cb.RegisterGeneric(item.Type).As(item.AsType).SingleInstance(); break; default: if (item.AsType == null || item.AsType.Length == 0) cb.RegisterGeneric(item.Type).InstancePerLifetimeScope(); else cb.RegisterGeneric(item.Type).As(item.AsType).InstancePerLifetimeScope(); break; } } foreach (var item in rb.Instances) { var type = item.AsType ?? item.Instance.GetType(); if (cache != null) cache.TryAdd(type, true); cb.RegisterInstance(item.Instance).As(type); } foreach (var it in rb.Funcs) { var item = it; if (item.AsType == null || item.AsType.Length == 0) throw new NotSupportedException("Result type must be defined. Declared Func result is not defined"); if (cache != null) { foreach (var t in item.AsType) cache.TryAdd(t, true); } switch (item.Scope) { case InstanceScope.Transient: cb.Register(c => item.Func(c.Resolve<IObjectFactory>())).As(item.AsType); break; case InstanceScope.Singleton: cb.Register(c => item.Func(c.Resolve<IObjectFactory>())).As(item.AsType).SingleInstance(); break; default: cb.Register(c => item.Func(c.Resolve<IObjectFactory>())).As(item.AsType).InstancePerLifetimeScope(); break; } } }
private void RegisterNew(ContainerBuilder cb) { foreach (var rb in FactoryBuilders) RegisterToContainer(cb, rb, RegistrationCache); foreach (var builder in AutofacBuilders) builder(cb); }
private void RegisterNew(ContainerBuilder cb) { foreach (var rb in FactoryBuilders) { foreach (var item in rb.Types.Where(i => i.IsGeneric == false)) { RegistrationCache.TryAdd(item.AsType ?? item.Type, true); switch (item.Scope) { case InstanceScope.Transient: if (item.AsType == null) cb.RegisterType(item.Type); else cb.RegisterType(item.Type).As(item.AsType); break; case InstanceScope.Singleton: if (item.AsType == null) cb.RegisterType(item.Type).SingleInstance(); else cb.RegisterType(item.Type).As(item.AsType).SingleInstance(); break; default: if (item.AsType == null) cb.RegisterType(item.Type).InstancePerLifetimeScope(); else cb.RegisterType(item.Type).As(item.AsType).InstancePerLifetimeScope(); break; } } foreach (var item in rb.Types.Where(i => i.IsGeneric)) { RegistrationCache.TryAdd(item.AsType ?? item.Type, true); switch (item.Scope) { case InstanceScope.Transient: if (item.AsType == null) cb.RegisterGeneric(item.Type); else cb.RegisterGeneric(item.Type).As(item.AsType); break; case InstanceScope.Singleton: if (item.AsType == null) cb.RegisterGeneric(item.Type).SingleInstance(); else cb.RegisterGeneric(item.Type).As(item.AsType).SingleInstance(); break; default: if (item.AsType == null) cb.RegisterGeneric(item.Type).InstancePerLifetimeScope(); else cb.RegisterGeneric(item.Type).As(item.AsType).InstancePerLifetimeScope(); break; } } foreach (var item in rb.Instances) { var type = item.AsType ?? item.Instance.GetType(); RegistrationCache.TryAdd(type, true); cb.RegisterInstance(item.Instance).As(type); } foreach (var item in rb.Funcs) { if (item.AsType == null) throw new NotSupportedException("Result type must be defined. Declared Func result is not defined"); RegistrationCache.TryAdd(item.AsType, true); switch (item.Scope) { case InstanceScope.Transient: cb.Register(c => item.Func(c.Resolve<IObjectFactory>())).As(item.AsType); break; case InstanceScope.Singleton: cb.Register(c => item.Func(c.Resolve<IObjectFactory>())).As(item.AsType).SingleInstance(); break; default: cb.Register(c => item.Func(c.Resolve<IObjectFactory>())).As(item.AsType).InstancePerLifetimeScope(); break; } } } foreach (var builder in AutofacBuilders) builder(cb); }