private static void SetupExtensibility(Revenj.Extensibility.Autofac.ContainerBuilder builder, bool withAspects, bool externalConfiguration)
        {
            var dynamicProxy = new CastleDynamicProxyProvider();
            var aopRepository = new AspectRepository(dynamicProxy);

            var dllPlugins = externalConfiguration == false ? new string[0] :
                (from key in ConfigurationManager.AppSettings.AllKeys
                 where key.StartsWith("PluginsPath", StringComparison.OrdinalIgnoreCase)
                 let path = ConfigurationManager.AppSettings[key]
                 let pathRelative = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path)
                 let chosenPath = Directory.Exists(pathRelative) ? pathRelative : path
                 select chosenPath)
                .ToArray();

            var assemblies = Revenj.Utility.AssemblyScanner.GetAssemblies().Where(it => it.FullName.StartsWith("Revenj."));
            builder.RegisterInstance(new PluginsConfiguration { Assemblies = assemblies, Directories = dllPlugins });

            builder.RegisterType<SystemInitialization>();
            builder.RegisterType<AutofacObjectFactory>().As<IObjectFactory>().SingleInstance();
            builder.RegisterType<AutofacMefProvider>().As<IExtensibilityProvider>().SingleInstance();
            builder.RegisterGeneric(typeof(PluginRepository<>)).As(typeof(IPluginRepository<>)).SingleInstance();

            builder.RegisterInstance(aopRepository).As<IAspectRegistrator, IAspectComposer, IInterceptorRegistrator>();
            builder.RegisterInstance(dynamicProxy).As<IMixinProvider, IDynamicProxyProvider>();

            var types = AssemblyScanner.GetAllTypes();//TODO: force load everything
            if (withAspects)
            {
                foreach (var m in types)
                {
                    if (m.Assembly.FullName.StartsWith("Revenj."))
                        continue;
                    if (m.IsPublic && !m.IsAbstract && typeof(Revenj.Extensibility.Autofac.Module).IsAssignableFrom(m) && m.GetConstructor(new Type[0]) != null)
                        builder.RegisterModule((Revenj.Extensibility.Autofac.Module)Activator.CreateInstance(m));
                }
                builder.RegisterModule(new AspectsModule(aopRepository));
            }
        }
        private static void SetupPatterns(Revenj.Extensibility.Autofac.ContainerBuilder builder)
        {
            var serverModels =
                (from asm in Revenj.Utility.AssemblyScanner.GetAssemblies()
                 let type = asm.GetType("SystemBoot.Configuration")
                 where type != null && type.GetMethod("Initialize") != null
                 select asm)
                .ToList();

            builder.RegisterGeneratedFactory<Revenj.DomainPatterns.DomainModel.Factory>();
            builder.RegisterType<Revenj.DomainPatterns.DomainModel>();
            builder.Register(c => c.Resolve<Revenj.DomainPatterns.DomainModel.Factory>()(serverModels)).As<IDomainModel>().SingleInstance();
            builder.RegisterType<DomainTypeResolver>().As<ITypeResolver>().SingleInstance();
            builder.RegisterType<ServiceLocator>().As<IServiceLocator, IServiceProvider>().InstancePerLifetimeScope();
            builder.RegisterGeneric(typeof(WeakCache<>)).As(typeof(WeakCache<>), typeof(IDataCache<>)).InstancePerLifetimeScope();
            builder.RegisterType<DomainEventSource>().As<IDomainEventSource>().InstancePerLifetimeScope();
            builder.RegisterType<DomainEventStore>().As<IDomainEventStore>().InstancePerLifetimeScope();
            builder.RegisterType<GlobalEventStore>().SingleInstance();
            builder.RegisterGeneric(typeof(SingleDomainEventSource<>)).As(typeof(IDomainEventSource<>)).InstancePerLifetimeScope();
            builder.RegisterGeneric(typeof(RegisterChangeNotifications<>)).As(typeof(IObservable<>)).SingleInstance();
            builder.RegisterType<DataContext>().As<IDataContext>().InstancePerLifetimeScope();
        }
        private static void SetupExtensibility(Revenj.Extensibility.Autofac.ContainerBuilder builder)
        {
            var dynamicProxy = new CastleDynamicProxyProvider();
            var aopRepository = new AspectRepository(dynamicProxy);

            var dllPlugins =
                (from key in ConfigurationManager.AppSettings.AllKeys
                 where key.StartsWith("PluginsPath", StringComparison.OrdinalIgnoreCase)
                 let path = ConfigurationManager.AppSettings[key]
                 let pathRelative = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path)
                 let chosenPath = Directory.Exists(pathRelative) ? pathRelative : path
                 select chosenPath)
                .ToList();
            builder.RegisterInstance(new PluginsConfiguration { Directories = dllPlugins });

            builder.RegisterType<SystemInitialization>();
            builder.RegisterType<AutofacObjectFactory>().As<IObjectFactory>().SingleInstance();
            builder.RegisterType<AutofacMefProvider>().As<IExtensibilityProvider>().SingleInstance();
            builder.RegisterGeneric(typeof(PluginRepository<>)).As(typeof(IPluginRepository<>)).SingleInstance();

            builder.RegisterInstance(aopRepository).As<IAspectRegistrator, IAspectComposer, IInterceptorRegistrator>();
            builder.RegisterInstance(dynamicProxy).As<IMixinProvider, IDynamicProxyProvider>();

            if (ConfigurationManager.AppSettings["Revenj.AllowAspects"] == "true")
                builder.RegisterModule(new AspectsModule(aopRepository));
        }
        private static void SetupPatterns(Revenj.Extensibility.Autofac.ContainerBuilder builder)
        {
            var serverModels =
                (from key in ConfigurationManager.AppSettings.AllKeys
                 where key.StartsWith("ServerAssembly", StringComparison.OrdinalIgnoreCase)
                 select LoadAssembly(ConfigurationManager.AppSettings[key]))
                .ToList();

            if (serverModels.Count == 0)
            {
                serverModels =
                    (from asm in Revenj.Utility.AssemblyScanner.GetAssemblies()
                     let type = asm.GetType("SystemBoot.Configuration")
                     where type != null && type.GetMethod("Initialize") != null
                     select asm)
                    .ToList();
                if (serverModels.Count == 0)
                    throw new ConfigurationErrorsException(@"Server assemblies not found. When running in compiled mode, server assemblies must be deployed with other assemblies.
            Alternatively, explicitly specify sever assembly in the config file.
            Example: <add key=""ServerAssembly_Domain"" value=""AppDomainModel.dll"" />");
            }

            builder.RegisterGeneratedFactory<Revenj.DomainPatterns.DomainModel.Factory>();
            builder.RegisterType<Revenj.DomainPatterns.DomainModel>();
            builder.Register(c => c.Resolve<Revenj.DomainPatterns.DomainModel.Factory>()(serverModels)).As<IDomainModel>().SingleInstance();
            builder.RegisterType<DomainTypeResolver>().As<ITypeResolver>().SingleInstance();
            builder.RegisterType<ServiceLocator>().As<IServiceLocator, IServiceProvider>().InstancePerLifetimeScope();
            builder.RegisterGeneric(typeof(WeakCache<>)).As(typeof(WeakCache<>), typeof(IDataCache<>)).InstancePerLifetimeScope();
            builder.RegisterType<DomainEventSource>().As<IDomainEventSource>().InstancePerLifetimeScope();
            builder.RegisterType<DomainEventStore>().As<IDomainEventStore>().InstancePerLifetimeScope();
            builder.RegisterType<GlobalEventStore>().SingleInstance();
            builder.RegisterGeneric(typeof(SingleDomainEventSource<>)).As(typeof(IDomainEventSource<>)).InstancePerLifetimeScope();
            builder.RegisterGeneric(typeof(RegisterChangeNotifications<>)).As(typeof(IObservable<>)).SingleInstance();
            builder.RegisterType<DataContext>().As<IDataContext>().InstancePerLifetimeScope();
        }