Ejemplo n.º 1
0
        internal static Func <PocketContainer, object> InMemoryEventSourcedRepositoryStrategy(Type type, PocketContainer container)
        {
            if (type.IsGenericType &&
                (type.GetGenericTypeDefinition() == typeof(IEventSourcedRepository <>) ||
                 type.GetGenericTypeDefinition() == typeof(InMemoryEventSourcedRepository <>)))
            {
                var aggregateType  = type.GenericTypeArguments.Single();
                var repositoryType = typeof(InMemoryEventSourcedRepository <>).MakeGenericType(aggregateType);

                var streamName = AggregateType.EventStreamName(aggregateType);

                // get the single registered event stream instance
                var stream = container.Resolve <ConcurrentDictionary <string, IEventStream> >()
                             .GetOrAdd(streamName,
                                       name => container.Clone()
                                       .Register(_ => name)
                                       .Resolve <IEventStream>());

                return(c => Activator.CreateInstance(repositoryType, stream, c.Resolve <IEventBus>()));
            }

            if (type == typeof(IEventStream))
            {
                return(c => c.Resolve <InMemoryEventStream>());
            }

            return(null);
        }
        public void Registrations_on_the_clone_override_original_when_a_type_is_registered_in_both()
        {
            var original = new PocketContainer()
                           .Register(c => "from original");
            var clone = original.Clone().Register(c => "from clone");

            clone.Resolve <string>().Should().Be("from clone");
        }
        public void Top_level_instances_are_resolved_from_the_clone_when_it_has_a_registration()
        {
            var original = new PocketContainer();

            var clone = original.Clone().Register(c => "from clone");

            clone.Resolve <string>().Should().Be("from clone");
        }
        public void Registrations_on_the_clone_override_original_when_a_type_is_registered_in_both()
        {
            var original = new PocketContainer()
                .Register(c => "from original");
            var clone = original.Clone().Register(c => "from clone");

            clone.Resolve<string>().Should().Be("from clone");
        }
        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.Clone().AutoMockInterfacesAndAbstractClasses();

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

            original.Count().Should().Be(clone.Count() - 1);
        }
        public void A_strategy_on_the_clone_is_not_added_to_the_original()
        {
            var original = new PocketContainer();
            original.Clone().AutoMockInterfacesAndAbstractClasses();

            Action resolveFromOriginal = () =>
                original.Resolve<IEnumerable<string>>();

            resolveFromOriginal.ShouldThrow<ArgumentException>();
        }
        public void Strategies_are_cloned()
        {
            var original = new PocketContainer().AutoMockInterfacesAndAbstractClasses();

            var clone = original.Clone();

            clone.Resolve <IEnumerable <string> >()
            .Should()
            .NotBeNull();
        }
        public void Dependencies_are_resolved_from_the_clone_when_it_has_a_registration()
        {
            var original = new PocketContainer();

            var clone = original.Clone().Register(c => "from clone");

            clone.Resolve<HasOneParamCtor<string>>()
                .Value1
                .Should()
                .Be("from clone");
        }
        public void Dependencies_are_resolved_from_the_clone_when_it_has_a_registration()
        {
            var original = new PocketContainer();

            var clone = original.Clone().Register(c => "from clone");

            clone.Resolve <HasOneParamCtor <string> >()
            .Value1
            .Should()
            .Be("from clone");
        }
        public void A_strategy_on_the_clone_is_not_added_to_the_original()
        {
            var original = new PocketContainer();

            original.Clone().AutoMockInterfacesAndAbstractClasses();

            Action resolveFromOriginal = () =>
                                         original.Resolve <IEnumerable <string> >();

            resolveFromOriginal.ShouldThrow <ArgumentException>();
        }
        public void Strategies_are_cloned()
        {
            var original = new PocketContainer()
                           .Register(c => new List <string>())
                           .RegisterGeneric(typeof(IEnumerable <>), to: typeof(List <>));

            var clone = original.Clone();

            clone.Resolve <IEnumerable <string> >()
            .Should()
            .NotBeNull();
        }
        public void A_strategy_on_the_clone_is_not_added_to_the_original()
        {
            var original = new PocketContainer();

            original.Clone()
            .RegisterGeneric(typeof(IEnumerable <>), to: typeof(List <>));

            Action resolveFromOriginal = () =>
                                         original.Resolve <IEnumerable <string> >();

            resolveFromOriginal.ShouldThrow <ArgumentException>();
        }
        public void When_a_singleton_registered_in_the_original_is_resolved_first_from_the_clone_and_then_from_the_original_it_is_the_same_instance()
        {
            var original = new PocketContainer();

            original.RegisterSingle(c => new object());

            var clone = original.Clone();

            var resolvedFromClone    = clone.Resolve <object>();
            var resolvedFromOriginal = original.Resolve <object>();

            resolvedFromOriginal.Should().BeSameAs(resolvedFromClone);
        }
        public void Dependencies_of_a_registration_from_the_original_can_be_resolved_from_the_clone()
        {
            var original = new PocketContainer()
                           .Register <IAmAnInterface>(c => c.Resolve <HasOneParamCtor <string> >());

            var clone = original.Clone()
                        .Register(c => "from clone");

            var obj = clone.Resolve <HasTwoParamCtor <string, IAmAnInterface> >();

            obj.Value1.Should().Be("from clone");
            obj.Value2.Should().BeOfType <HasOneParamCtor <string> >();
            ((HasOneParamCtor <string>)obj.Value2).Value1.Should().Be("from clone");
        }
        public void Dependencies_of_a_registration_from_the_original_can_be_resolved_from_the_clone()
        {
            var original = new PocketContainer()
                .Register<IAmAnInterface>(c => c.Resolve<HasOneParamCtor<string>>());

            var clone = original.Clone()
                .Register(c => "from clone");

            var obj = clone.Resolve<HasTwoParamCtor<string, IAmAnInterface>>();

            obj.Value1.Should().Be("from clone");
            obj.Value2.Should().BeOfType<HasOneParamCtor<string>>();
            ((HasOneParamCtor<string>) obj.Value2).Value1.Should().Be("from clone");
        }
        public void When_RegisterSingle_is_called_on_the_clone_it_does_not_register_the_singleton_in_the_original()
        {
            var original = new PocketContainer();

            original.RegisterSingle(c => new object());

            var clone = original.Clone();

            clone.RegisterSingle(c => new object());

            var resolvedFromClone    = clone.Resolve <object>();
            var resolvedFromOriginal = original.Resolve <object>();

            resolvedFromOriginal.Should()
            .NotBeSameAs(resolvedFromClone);
        }
        public void Strategies_are_cloned()
        {
            var original = new PocketContainer().AutoMockInterfacesAndAbstractClasses();

            var clone = original.Clone();

            clone.Resolve<IEnumerable<string>>()
                .Should()
                .NotBeNull();
        }
        public void Top_level_instances_are_resolved_from_the_original_container_when_the_clone_has_no_registration()
        {
            var original = new PocketContainer()
                .Register(c => "from original");

            var clone = original.Clone();

            clone.Resolve<string>()
                .Should()
                .Be("from original");
        }
        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.Clone().AutoMockInterfacesAndAbstractClasses();

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

            original.Count().Should().Be(clone.Count() - 1);
        }