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>();
        }
Example #2
0
		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)
     {
         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);
 }