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

            container.RegisterSingle <IAmAnInterface>(c => new HasDefaultCtor());

            container.Resolve <IAmAnInterface>().Should().BeOfType <HasDefaultCtor>();

            container.RegisterSingle <IAmAnInterface>(c => new HasOneParamCtor <string>("second registration"));

            container.Resolve <IAmAnInterface>().Should().BeOfType <HasOneParamCtor <string> >();
        }
        public void When_a_singleton_registration_made_using_RegisterSingle_already_exists_then_TryRegister_does_not_overwrite_it()
        {
            var container = new PocketContainer();

            container.RegisterSingle(c => "one");
            container.TryRegister(typeof(string), c => "two");

            container.Resolve <string>().Should().Be("one");
        }
        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 When_a_registration_made_using_TryRegisterSingle_already_exists_but_has_not_been_resolved_then_TryRegisterSingle_does_not_trigger_its_resolution()
        {
            var container = new PocketContainer();
            var resolved  = false;

            container.RegisterSingle(c =>
            {
                resolved = true;
                return("one");
            });
            container.TryRegisterSingle(typeof(string), c => "two");

            resolved.Should().BeFalse();
        }
        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);
        }
Ejemplo n.º 6
0
        public void ConfigureApplication(IAppBuilder app, string nodeId, IObservable <IEnumerable <Peer> > peerObserver)
        {
            thisServersNode = new Node(new NodeSettings(nodeId,
                                                        TimeSpan.FromSeconds(5),
                                                        TimeSpan.FromSeconds(1),
                                                        peerObserver));

            var config = new HttpConfiguration();
            var pocket = new PocketContainer();

            config.ResolveDependenciesUsing(pocket);
            pocket.RegisterSingle(c => thisServersNode);
            config.MapHttpAttributeRoutes();
            JsonSerialization(config);
            app.UseWebApi(config);
            thisServersNode.Start();
        }
        public void A_strategy_can_be_used_to_register_a_singleton()
        {
            var container = new PocketContainer();

            container.AddStrategy(type =>
            {
                if (type == typeof(IList <string>))
                {
                    container.RegisterSingle <IList <string> >(c => new List <string>());

                    return(c => c.Resolve <IList <string> >());
                }

                return(null);
            });

            var list1 = container.Resolve <IList <string> >();
            var list2 = container.Resolve <IList <string> >();

            list1.Should().BeSameAs(list2);
        }
        public void Registering_is_invoked_when_registering_using_RegisterSingle()
        {
            var receivedDelegates = new List <Delegate>();

            var container = new PocketContainer();

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

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

            container.Resolve <string>();

            receivedDelegates
            .Should()
            .HaveCount(1);
            receivedDelegates
            .Single()
            .Should()
            .BeOfType <Func <PocketContainer, string> >();
        }
Ejemplo n.º 9
0
 internal static PocketContainer UseJsonSerializerSettingsForSerialization(this PocketContainer container)
 {
     container.RegisterSingle(c => (SerializeEvent)null);
     container.RegisterSingle(c => (DeserializeEvent)null);
     return(container);
 }
Ejemplo n.º 10
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())
                            .Register(c => new TestSession(c.Resolve <IHttpContextAccessor>()))
                            .Register(c => services.GetRequiredService <IHttpContextAccessor>());

            var testDependencyRegistry = new TestDependencyRegistry(container);

            container.RegisterSingle(c => new TestTarget(testDependencyRegistry)
            {
                Application = application,
                Environment = environment,
                BaseAddress = baseAddress
            });

            container.OnFailedResolve = (t, e) =>
                                        throw new InvalidOperationException($"TestTarget does not contain registration for '{t}'.");

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

                    if (services.GetService(type) != null)
                    {
                        return(c => services.GetService(type));
                    }

                    return(null);
                });
            }

            testDependencies?.Invoke(testDependencyRegistry);

            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);
        }