Example #1
0
        public BitServiceProviderFactory(Action <IDependencyManager, IServiceCollection, ContainerBuilder> configureAction)
        {
            _inner = new AutofacServiceProviderFactory(containerBuilder =>
            {
                if (_services == null)
                {
                    throw new InvalidOperationException("services is null");
                }

                if (containerBuilder == null)
                {
                    throw new InvalidOperationException("containerBuilder is null");
                }

                containerBuilder.Properties["services"] = _services;

                AutofacDependencyManager dependencyManager = new AutofacDependencyManager();
                dependencyManager.UseContainerBuilder(containerBuilder);
                ((IServiceCollectionAccessor)dependencyManager).ServiceCollection = _services;

                containerBuilder.Properties["dependencyManager"] = dependencyManager;

                containerBuilder.Register(c => _container).SingleInstance();

                configureAction?.Invoke(dependencyManager, _services, containerBuilder);
            });
        }
Example #2
0
        protected sealed override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            ContainerBuilder containerBuilder = containerRegistry.GetBuilder();

            IServiceCollection services = new BitServiceCollection();

            containerBuilder.Properties[nameof(services)]          = services;
            containerBuilder.Properties[nameof(containerRegistry)] = containerRegistry;

            AutofacDependencyManager dependencyManager = new AutofacDependencyManager();

            dependencyManager.UseContainerBuilder(containerBuilder);
            ((IServiceCollectionAccessor)dependencyManager).ServiceCollection = services;

            RegisterTypes(dependencyManager, containerRegistry, containerBuilder, services);

            containerBuilder.Populate(services);
        }
        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            ContainerBuilder containerBuilder = containerRegistry.GetBuilder();

            AutofacDependencyManager?dependencyManager;

            if (containerBuilder.Properties.TryGetValue(nameof(dependencyManager), out object?dependencyManagerObj))
            {
                dependencyManager = (AutofacDependencyManager)dependencyManagerObj !;
            }
            else
            {
                dependencyManager = new AutofacDependencyManager();
                dependencyManager.UseContainerBuilder(containerBuilder);
                containerBuilder.Properties[nameof(dependencyManager)] = dependencyManager;
            }

            IServiceCollection?services;

            if (containerBuilder.Properties.TryGetValue(nameof(services), out object?servicesObj))
            {
                services = (IServiceCollection)servicesObj !;
            }
            else
            {
                services = new BitServiceCollection();
                containerBuilder.Properties[nameof(services)] = services;
                ((IServiceCollectionAccessor)dependencyManager).ServiceCollection = services;
            }

            containerBuilder.Properties[nameof(containerRegistry)] = containerRegistry;

#if Android
            containerBuilder.Register(c => (Activity)_activity).SingleInstance().PreserveExistingDefaults();
            containerBuilder.Register(c => (Context)_activity).SingleInstance().PreserveExistingDefaults();
#endif

            RegisterTypes(dependencyManager, containerRegistry, containerBuilder, services);
        }