/// <summary>
        /// Configures LightBDD to use Autofac container described by <paramref name="builder"/>.
        /// </summary>
        /// <param name="configuration">Configuration.</param>
        /// <param name="builder">Autofac container builder.</param>
        public static DependencyContainerConfiguration UseAutofac(
            this DependencyContainerConfiguration configuration,
            ContainerBuilder builder)
        {
            var container = new AutofacContainer();

            container.AutofacScope = container.RegisterSelf(builder).Build();

            return(configuration.UseContainer(container));
        }
Beispiel #2
0
        /// <summary>
        /// Configures LightBDD to use provided <paramref name="container"/> Autofac container.<br/>
        /// Please note that <paramref name="container"/> will not be disposed by LightBDD after test run, but rather treat it as externally owned instance
        /// - use <see cref="UseAutofac(DependencyContainerConfiguration,ContainerBuilder)"/> if container should be fully managed by LightBDD.
        /// </summary>
        /// <param name="configuration">Configuration.</param>
        /// <param name="container">Container to use.</param>
        public static DependencyContainerConfiguration UseAutofac(
            this DependencyContainerConfiguration configuration,
            ILifetimeScope container)
        {
            var autofacScope = new AutofacContainer();

            autofacScope.AutofacScope = container.BeginLifetimeScope(builder => autofacScope.RegisterSelf(builder));

            configuration.UseContainer(autofacScope);
            return(configuration);
        }
        /// <summary>
        /// Configures LightBDD to use provided <paramref name="container"/> Autofac container, where <paramref name="takeOwnership"/> specifies if LightBDD should control container disposal or not.<br/>
        /// Please note that the new scope will be created to handle injections for LightBDD.<br/>
        /// </summary>
        /// <param name="configuration">Configuration.</param>
        /// <param name="container">Container to use.</param>
        /// <param name="takeOwnership">If true, the container will be disposed by LightBDD after tests are finished.</param>
        public static DependencyContainerConfiguration UseAutofac(
            this DependencyContainerConfiguration configuration,
            ILifetimeScope container,
            bool takeOwnership)
        {
            var autofacContainer = new AutofacContainer();

            autofacContainer.AutofacScope = container.BeginLifetimeScope(builder => autofacContainer.RegisterSelf(builder));
            if (takeOwnership)
            {
                autofacContainer.ParentScope = container;
            }

            return(configuration.UseContainer(autofacContainer));
        }
Beispiel #4
0
        /// <summary>
        /// Configures LightBDD to use provided <paramref name="serviceProvider"/> provider, where <paramref name="takeOwnership"/> specifies if LightBDD should control provider disposal or not.<br/>
        /// Please note that the new scope will be created to handle injections for LightBDD.<br/>
        /// </summary>
        /// <param name="configuration">Configuration.</param>
        /// <param name="serviceProvider">Service provider to use.</param>
        /// <param name="takeOwnership">If true, the provider will be disposed by LightBDD after tests are finished.</param>
        public static DependencyContainerConfiguration UseContainer(
            this DependencyContainerConfiguration configuration, IServiceProvider serviceProvider, bool takeOwnership)
        {
            var container = new DiContainer(serviceProvider.CreateScope(), new ContainerOverrides());

            if (takeOwnership)
            {
                if (serviceProvider is IDisposable disposableParent)
                {
                    container.AddDisposable(disposableParent);
                }
                else
                {
                    throw new ArgumentException($"The provided {serviceProvider.GetType().FullName} is not {nameof(IDisposable)} and LightBDD cannot take proper ownership of the provider. Please consider specifying {nameof(takeOwnership)} parameter to be false and manual disposal of the provider.", nameof(serviceProvider));
                }
            }

            return(configuration.UseContainer(container));
        }
Beispiel #5
0
        /// <summary>
        /// Configures LightBDD to use DI container described by <paramref name="serviceProvider"/> provider, and allows customizing container behaviour with <paramref name="options"/> delegate.<br/>
        /// </summary>
        public static DependencyContainerConfiguration UseContainer(
            this DependencyContainerConfiguration configuration, IServiceProvider serviceProvider, Action <DiContainerOptions> options)
        {
            var containerOptions = new DiContainerOptions();

            options?.Invoke(containerOptions);

            var scope     = new NestingContainerScope(serviceProvider.CreateScope(), containerOptions.ShouldEnableScopeNestingWithinScenarios);
            var container = new DiContainer(scope, new ContainerOverrides());

            if (containerOptions.ShouldTakeOwnership)
            {
                if (serviceProvider is IDisposable disposableParent)
                {
                    container.AddDisposable(disposableParent);
                }
                else
                {
                    throw new ArgumentException($"The provided {serviceProvider.GetType().FullName} is not {nameof(IDisposable)} and LightBDD cannot take proper ownership of the provider. Please consider disabling takeOwnership configuration flag and manual disposal of the provider.", nameof(serviceProvider));
                }
            }

            return(configuration.UseContainer(container));
        }
 /// <summary>
 /// Configures LightBDD to use DI container described by <paramref name="serviceProvider"/>.
 /// Please note that the new scope will be created to handle injections for LightBDD.
 /// </summary>
 /// <param name="configuration">Configuration.</param>
 /// <param name="serviceProvider">Service provider instance.</param>
 public static DependencyContainerConfiguration UseContainer(
     this DependencyContainerConfiguration configuration, IServiceProvider serviceProvider)
 {
     return(configuration.UseContainer(new DiContainer(serviceProvider.CreateScope(), new ContainerOverrides())));
 }