public void When_the_same_type_is_registered_multiple_times_then_the_last_register_wins()
        {
            var container = new PocketContainer();

            container.Register(c => 1);
            container.Register(c => 2);

            container.Resolve <int>().Should().Be(2);
        }
Beispiel #2
0
        public void Multiple_registrations_of_T_can_be_resolved_as_an_IEnumerable_of_T()
        {
            var container = new PocketContainer()
                            .AccumulateRegistrations();

            container.Register(c => "one");
            container.Register(c => "two");

            container.Resolve <IEnumerable <string> >()
            .Should()
            .BeEquivalentTo("one", "two");
        }
        public void Can_resolve_Func_of_unregistered_concrete_type_by_choosing_the_most_verbose_ctor_and_resolving_recursively()
        {
            var container = new PocketContainer();

            container.Register(c => "hello");
            container.Register(c => new HasOneParamCtor <string>("there"));

            var result = container.Resolve <Func <HasTwoParamCtor <string, HasOneParamCtor <string> > > >();

            result().Should().NotBeNull();
            result().Value1.Should().Be("hello");
            result().Value2.Value1.Should().Be("there");
        }
        public void Can_resolve_type_not_known_at_compile_time()
        {
            var container = new PocketContainer();

            container.Register(c => "hello");
            container.Register(c => new HasOneParamCtor <int>(42));

            var result = container.Resolve(typeof(HasTwoParamCtor <string, HasOneParamCtor <int> >)) as HasTwoParamCtor <string, HasOneParamCtor <int> >;

            result.Should().NotBeNull();

            result.Value1.Should().Be("hello");
            result.Value2.Value1.Should().Be(42);
        }
        public void When_a_registration_made_using_Register_already_exists_then_TryRegister_T_does_not_overwrite_it()
        {
            var container = new PocketContainer();

            container.Register(c => "one");
            container.TryRegister(c => "two");

            container.Resolve <string>().Should().Be("one");
        }
        public void Can_resolve_a_registered_delegate_type()
        {
            var container = new PocketContainer();

            var f = new SomeDelegateType(Can_resolve_a_registered_delegate_type);

            container.Register(c => f);

            container.Resolve <SomeDelegateType>().Should().Be(f);
        }
        public void An_open_generic_interface_can_be_registered_to_an_open_generic_type_and_resolved_correctly()
        {
            var container = new PocketContainer();

            container
                .Register<List<string>>(c => new List<string>())
                .RegisterGeneric(variantsOf: typeof (IEnumerable<>), to: typeof (List<>));

            container.Resolve<IEnumerable<string>>().Should().BeOfType<List<string>>();
        }
Beispiel #8
0
        public void ConfigureApplication(IAppBuilder app)
        {
            var config = new HttpConfiguration();
            var pocket = new PocketContainer();

            config.ResolveDependenciesUsing(pocket);
            pocket.Register(c => thisServersNode);
            config.MapHttpAttributeRoutes();
            app.UseWebApi(config);
        }
        public void Lazy_is_implicitly_registered_when_registering_a_type()
        {
            var container = new PocketContainer();

            container.Register(c => "oh hai");

            var obj = container.Resolve <HasOneParamCtor <Lazy <string> > >();

            obj.Value1.Value.Should().Be("oh hai");
        }
        public void Lazy_is_resolvable_for_unregistered_types()
        {
            var container = new PocketContainer();

            container.Register(c => 123);

            var obj = container.Resolve <HasOneParamCtor <Lazy <int> > >();

            obj.Value1.Value.Should().Be(123);
        }
        public void Can_resolve_Func_of_unregistered_concrete_type_with_dependencies_when_it_has_only_a_default_ctor()
        {
            var container = new PocketContainer();

            var result = container
                         .Register(c => "hello")
                         .Resolve <Func <HasOneParamCtor <string> > >();

            result().Value1.Should().Be("hello");
        }
Beispiel #12
0
        public void An_open_generic_interface_can_be_registered_to_an_open_generic_type_and_resolved_correctly()
        {
            var container = new PocketContainer();

            container
            .Register <List <string> >(c => new List <string>())
            .RegisterGeneric(variantsOf: typeof(IEnumerable <>), to: typeof(List <>));

            container.Resolve <IEnumerable <string> >().Should().BeOfType <List <string> >();
        }
Beispiel #13
0
        public TestApi()
        {
            HttpConfiguration = new HttpConfiguration
            {
                IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always
            }.ResolveDependenciesUsing(Container);

            HttpConfiguration.MapRoutesFor <T>();

            Container.Register <IEventSourcedRepository <T> >(c => new SqlEventSourcedRepository <T>());
        }
        public void When_nonframework_dependency_constructor_throws_then_exception_is_thrown()
        {
            var container = new PocketContainer();

            container.Register <IMyDependency>(c => new DependencyThatThrowOnConstruction());
            var sut = new PocketContainerDependencyResolver(container);

            Action resolveFrameworkDependency =
                () => sut.GetService(typeof(IMyDependency));

            resolveFrameworkDependency.ShouldThrow <HttpParseException>("because GetService is resolving our own interface");
        }
        public void When_framework_dependency_constructor_throws_then_exception_is_thrown()
        {
            var container = new PocketContainer();
            container.Register<IContentNegotiator>(c => new DependencyThatThrowOnConstruction());
            var sut = new PocketContainerDependencyResolver(container);

            Action resolveFrameworkDependency =
                () => sut.GetService(typeof (IContentNegotiator));

            resolveFrameworkDependency.ShouldThrow<HttpParseException>(
                "because framework service implementation constructor threw an exception");
        }
        public void Dependencies_are_resolved_from_the_primary_container_when_registered_after_the_override_was_created()
        {
            var primary = new PocketContainer();

            var @override = primary.CreateOverrideContainer();
            primary.Register(c => "from primary");

            @override.Resolve<HasOneParamCtor<string>>()
                     .Value1
                     .Should()
                     .Be("from primary");
        }
        public void When_a_strategy_on_the_clone_results_in_a_new_registration_then_the_original_contanier_is_not_affected()
        {
            var original = new PocketContainer();
            var clone    = original
                           .Register(c => new List <string>())
                           .Clone()
                           .RegisterGeneric(typeof(IEnumerable <>), to: typeof(List <>));

            clone.Resolve <IEnumerable <string> >();

            original.Count().Should().Be(clone.Count() - 1);
        }
Beispiel #18
0
        public TestTargetRegistry Add(string environment,
                                      string application,
                                      Uri baseAddress,
                                      Action <TestDependencyRegistry> testDependencies = null)
        {
            if (environment == null)
            {
                throw new ArgumentNullException("environment");
            }
            if (application == null)
            {
                throw new ArgumentNullException("application");
            }
            if (baseAddress == null)
            {
                throw new ArgumentNullException("baseAddress");
            }
            if (!baseAddress.IsAbsoluteUri)
            {
                throw new ArgumentException("Base address must be an absolute URI.");
            }

            var testTarget = new TestTarget
            {
                Application = application,
                Environment = environment,
                BaseAddress = baseAddress
            };

            var container = new PocketContainer()
                            .Register(c => new HttpClient())
                            .Register(c => testTarget);

            if (testDependencies != null)
            {
                testDependencies(new TestDependencyRegistry((t, func) => container.Register(t, c => func())));
            }

            container.AfterResolve <HttpClient>((c, client) =>
            {
                if (client.BaseAddress == null)
                {
                    client.BaseAddress = testTarget.BaseAddress;
                }
                return(client);
            });

            testTarget.ResolveDependency = container.Resolve;

            targets.Add(environment + ":" + application, testTarget);

            return(this);
        }
        public void Lazy_registrations_in_the_override_do_not_modify_the_parent()
        {
            var primary = new PocketContainer();

            var @override = primary.CreateOverrideContainer();

            primary.Register(c => "from primary");

            @override.Resolve <HasDefaultCtor>();

            primary.Count(reg => reg.Key == typeof(HasDefaultCtor)).Should().Be(0);
        }
        public void When_framework_dependency_constructor_throws_then_exception_is_thrown()
        {
            var container = new PocketContainer();

            container.Register <IContentNegotiator>(c => new DependencyThatThrowOnConstruction());
            var sut = new PocketContainerDependencyResolver(container);

            Action resolveFrameworkDependency =
                () => sut.GetService(typeof(IContentNegotiator));

            resolveFrameworkDependency.ShouldThrow <HttpParseException>(
                "because framework service implementation constructor threw an exception");
        }
        public void Dependencies_are_resolved_from_the_primary_container_when_registered_after_the_override_was_created()
        {
            var primary = new PocketContainer();

            var @override = primary.CreateOverrideContainer();

            primary.Register(c => "from primary");

            @override.Resolve <HasOneParamCtor <string> >()
            .Value1
            .Should()
            .Be("from primary");
        }
        public void When_framework_dependency_constructor_cannot_be_satisfied_then_exception_is_swallowed()
        {
            var container = new PocketContainer();
            container.Register<IContentNegotiator>(c => c.Resolve<UnsastisfiableDependency>());
            var sut = new PocketContainerDependencyResolver(container);

            Action resolveFrameworkDependency =
                () => sut.GetService(typeof (IContentNegotiator));
            Action resolveFrameworkDependencies =
                () => sut.GetServices(typeof (IContentNegotiator)).ToArray();

            resolveFrameworkDependency.ShouldNotThrow("because we're resolving a framework service");
            resolveFrameworkDependencies.ShouldNotThrow("because we're resolving a framework service");
        }
        public void When_framework_dependency_constructor_cannot_be_satisfied_then_exception_is_swallowed()
        {
            var container = new PocketContainer();

            container.Register <IContentNegotiator>(c => c.Resolve <UnsastisfiableDependency>());
            var sut = new PocketContainerDependencyResolver(container);

            Action resolveFrameworkDependency =
                () => sut.GetService(typeof(IContentNegotiator));
            Action resolveFrameworkDependencies =
                () => sut.GetServices(typeof(IContentNegotiator)).ToArray();

            resolveFrameworkDependency.ShouldNotThrow("because we're resolving a framework service");
            resolveFrameworkDependencies.ShouldNotThrow("because we're resolving a framework service");
        }
        public void When_nonframework_dependency_constructor_cannot_be_satisfied_then_exception_is_thrown()
        {
            var container = new PocketContainer();

            container.Register <IMyDependency>(c => c.Resolve <UnsastisfiableDependency>());
            var sut = new PocketContainerDependencyResolver(container);

            Action resolveFrameworkDependency =
                () => sut.GetService(typeof(IMyDependency));

            resolveFrameworkDependency.ShouldThrow <ArgumentException>("because GetService is resolving our own interface")
            .And
            .Message
            .Should()
            .Contain("+IAmUnregistered");
        }
Beispiel #25
0
        public InMemorySchedulingAndHandlingTests(ITestOutputHelper output) : base(output)
        {
            virtualClock = VirtualClock.Start();

            container
            .Register(c => virtualClock)
            .RegisterSingle(c => new InMemoryCommandBus <string>(virtualClock))
            .RegisterGeneric(
                variantsOf: typeof(ICommandReceiver <>),
                to: typeof(InMemoryCommandBus <>),
                singletons: true)
            .RegisterGeneric(
                variantsOf: typeof(ICommandScheduler <>),
                to: typeof(InMemoryCommandBus <>),
                singletons: true);

            disposables.Add(virtualClock);
        }
Beispiel #26
0
        public void Configuration_can_specify_a_dependency_resolver_which_is_then_used_when_resolving_handlers()
        {
            var container = new PocketContainer();

            container.Register(c => "hello");

            var configuration = new Configuration()
                                .UseSqlEventStore()
                                .UseDependency(resolve: resolve => (IEventBus)resolve(typeof(EventBusWithDependencies)))
                                .UseDependency <IEnumerable <string> >(_ => new[] { "hello" });

            var bus = configuration.EventBus;

            bus.Should().BeOfType <EventBusWithDependencies>();

            var busWithDependencies = bus as EventBusWithDependencies;

            busWithDependencies.StringValues.Single().Should().Be("hello");
        }
        public ICommandSchedulerDispatcher InitializeScheduler(
            IObserver <ICommandSchedulerActivity> subject,
            PocketContainer container,
            Func <IEvent, string> getClockName)
        {
            var binder = container.Resolve <SqlCommandSchedulerBinder <TAggregate> >();

            var scheduler = binder.Scheduler;

            scheduler.GetClockName = getClockName;
            scheduler.Activity     = subject;

            var schedulerType = typeof(ICommandScheduler <TAggregate>);

            if (container.All(t => t.Key != schedulerType))
            {
                container.Register(schedulerType, c => scheduler);
            }

            return(binder);
        }
        public void Registering_is_invoked_when_registering_using_Register()
        {
            var receivedDelegates = new List <Delegate>();

            var container = new PocketContainer();

            container.Registering += (type, f) =>
            {
                receivedDelegates.Add(f);
                return(f);
            };

            container.Register(typeof(string), c => "hello");

            container.Resolve <string>();

            receivedDelegates
            .Should()
            .HaveCount(1);
            receivedDelegates
            .Single()
            .Should()
            .BeOfType <Func <PocketContainer, string> >();
        }
        public void Lazy_registrations_in_the_override_do_not_modify_the_parent()
        {
            var primary = new PocketContainer();

            var @override = primary.CreateOverrideContainer();
            primary.Register(c => "from primary");

            @override.Resolve<HasDefaultCtor>();

            primary.Count(reg => reg.Key == typeof (HasDefaultCtor)).Should().Be(0);
        }
        public void When_nonframework_dependency_constructor_throws_then_exception_is_thrown()
        {
            var container = new PocketContainer();
            container.Register<IMyDependency>(c => new DependencyThatThrowOnConstruction());
            var sut = new PocketContainerDependencyResolver(container);

            Action resolveFrameworkDependency =
                () => sut.GetService(typeof (IMyDependency));

            resolveFrameworkDependency.ShouldThrow<HttpParseException>("because GetService is resolving our own interface");
        }
        public void When_nonframework_dependency_constructor_cannot_be_satisfied_then_exception_is_thrown()
        {
            var container = new PocketContainer();
            container.Register<IMyDependency>(c => c.Resolve<UnsastisfiableDependency>());
            var sut = new PocketContainerDependencyResolver(container);

            Action resolveFrameworkDependency =
                () => sut.GetService(typeof (IMyDependency));

            resolveFrameworkDependency.ShouldThrow<ArgumentException>("because GetService is resolving our own interface")
                .And
                .Message
                .Should()
                .Contain("+IAmUnregistered");
        }
Beispiel #32
0
        public TestTargetRegistry Add(
            string environment,
            string application,
            Uri baseAddress,
            Action <TestDependencyRegistry> testDependencies = null)
        {
            if (baseAddress == null)
            {
                throw new ArgumentNullException(nameof(baseAddress));
            }
            if (!baseAddress.IsAbsoluteUri)
            {
                throw new ArgumentException("Base address must be an absolute URI.");
            }
            if (string.IsNullOrWhiteSpace(environment))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(environment));
            }
            if (string.IsNullOrWhiteSpace(application))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(application));
            }

            var container = new PocketContainer()
                            .Register(c => new HttpClient())
                            .RegisterSingle(c => new TestTarget(c.Resolve)
            {
                Application = application,
                Environment = environment,
                BaseAddress = baseAddress
            });

            if (services != null)
            {
                // fall back to application's IServiceProvider
                container.AddStrategy(type =>
                {
                    if (typeof(IPeakyTest).IsAssignableFrom(type))
                    {
                        return(null);
                    }

                    return(c => services.GetRequiredService(type));
                });
            }

            testDependencies?.Invoke(new TestDependencyRegistry((t, func) => container.Register(t, c => func())));

            container.AfterCreating <HttpClient>(client =>
            {
                if (client.BaseAddress == null)
                {
                    client.BaseAddress = baseAddress;
                }
                return(client);
            });

            targets.Add($"{environment}:{application}",
                        new Lazy <TestTarget>(() => container.Resolve <TestTarget>()));

            return(this);
        }