Exemplo n.º 1
0
 public void should_be_able_to_add_an_interface_as_a_singleton()
 {
     var objectBoss = new ObjectBoss();
     objectBoss.Configure(x => x.AddUsing<ISimpleInterface, SimpleObjectType>().Singleton());
     var simpleObject1 = objectBoss.GetInstance<ISimpleInterface>();
     var simpleObject2 = objectBoss.GetInstance<ISimpleInterface>();
     Assert.IsTrue(objectBoss.GetSingletonCount() == 2);
     Assert.IsNotNull(simpleObject1);
     Assert.IsNotNull(simpleObject2);
     Assert.IsTrue(simpleObject1.Id == simpleObject2.Id);
 }
Exemplo n.º 2
0
 public void should_add_object_as_singleton_and_get_the_same_instance_whenever_it_is_retrieved()
 {
     var objectBoss = new ObjectBoss();
     objectBoss.Configure(x => x.Add<SimpleObjectType>().Singleton());
     var simpleObject1 = objectBoss.GetInstance<SimpleObjectType>();
     var simpleObject2 = objectBoss.GetInstance<SimpleObjectType>();
     Assert.IsTrue(objectBoss.GetSingletonCount() == 2);
     Assert.IsNotNull(simpleObject1);
     Assert.IsNotNull(simpleObject2);
     Assert.IsTrue(simpleObject1.Id == simpleObject2.Id);
 }
Exemplo n.º 3
0
        public void should_instantiate_and_contain_instantiated_instance_for_objectbuilder()
        {
            var objectBoss = new ObjectBoss();
            objectBoss.Configure(x => x.Add<SimpleObjectType>());

            var ibo = objectBoss.GetInstance<IObjectBuilder>();
            Assert.AreEqual(objectBoss, ibo);
        }
Exemplo n.º 4
0
        public void should_send_and_receive_messages()
        {
            var objectBoss = new ObjectBoss();
            objectBoss.Configure(x => x.Add<ReceiveMessage>().ForMessagging());

            var recieveMessage = objectBoss.GetInstance<ReceiveMessage>();

            Assert.IsTrue(recieveMessage.Count == 1);
            objectBoss.SendMessage(new AddMessage() { HowMuchToAdd = 5 });
            Assert.IsTrue(recieveMessage.Count == 6);
        }
Exemplo n.º 5
0
        public void should_only_receive_messages_if_registered_to_receive_messages()
        {
            var objectBoss = new ObjectBoss();
            objectBoss.Configure(x =>
            {
                x.Add<ReceiveMessage>().ForMessagging();
                x.Add<DoesNotReceiveMessage>();
            });

            var recieveMessage = objectBoss.GetInstance<ReceiveMessage>();
            var doesNotReceive = objectBoss.GetInstance<DoesNotReceiveMessage>();

            Assert.IsTrue(recieveMessage.Count == 1);
            Assert.IsTrue(doesNotReceive.Count == 1);

            objectBoss.SendMessage(new AddMessage() { HowMuchToAdd = 5 });

            Assert.IsTrue(recieveMessage.Count == 6);
            Assert.IsTrue(doesNotReceive.Count == 1);
        }
Exemplo n.º 6
0
        public void should_be_able_to_bind_to_a_concrete_instance_and_get_that_instance()
        {
            var simpleObject = new SimpleObjectType();
            var objectBoss = new ObjectBoss();
            objectBoss.Configure(x =>
            {
                x.Add<SimpleObjectType>().BindTo(simpleObject);
            });

            var soc = objectBoss.GetInstance<SimpleObjectType>();
            Assert.AreEqual(simpleObject.Id, soc.Id);
        }
Exemplo n.º 7
0
        public void should_be_able_to_bind_to_an_instance_using_interface_and_get_that_instance()
        {
            var simpleObject = new SimpleObjectType();
            var objectBoss = new ObjectBoss();
            objectBoss.Configure(x =>
            {
                x.AddUsing<ISimpleInterface, SimpleObjectType>().BindTo(simpleObject);
            });

            var soi = objectBoss.GetInstance<ISimpleInterface>();
            Assert.AreEqual(simpleObject.Id, soi.Id);
        }
Exemplo n.º 8
0
        public void when_handler_weak_reference_is_gone_should_no_longer_receive_messages()
        {
            var objectBoss = new ObjectBoss();
            objectBoss.Configure(x => x.Add<ReceiveMessage>().ForMessagging());

            var recieveMessage = objectBoss.GetInstance<ReceiveMessage>();

            Assert.IsTrue(recieveMessage.Count == 1);
            objectBoss.SendMessage(new AddMessage() { HowMuchToAdd = 5 });
            Assert.IsTrue(recieveMessage.Count == 6);
            objectBoss.UnregisterTypeForMessaging(recieveMessage.GetType());

            Assert.IsNotNull(recieveMessage);
            objectBoss.SendMessage(new AddMessage() { HowMuchToAdd = 5 });
            Assert.IsTrue(recieveMessage.Count == 6);
        }
Exemplo n.º 9
0
        public void should_retrieve_concrete_classes_of_interfaces_by_key()
        {
            var objectBoss = new ObjectBoss();
            objectBoss.Configure(x =>
            {
                x.AddUsing<ISimpleInterface, SimpleObjectType>("object1");
                x.AddUsing<ISimpleInterface, AnotherSimpleObject>("object2");
            });

            var object1 = objectBoss.GetInstance<ISimpleInterface>("object1");
            var object2 = objectBoss.GetInstance<ISimpleInterface>("object2");
            Assert.IsNotNull(object1);
            Assert.IsTrue(object1.Name == "SimpleObject1");
            Assert.IsNotNull(object2);
            Assert.IsTrue(object2.Name == "SimpleObject2");
        }
Exemplo n.º 10
0
        public void should_be_able_to_configure_a_custom_constructor_with_primitives()
        {
            var objectBoss = new ObjectBoss();
            objectBoss.Configure(x => x.Add<ObjectWithPrimitives>()
                .WithCustomConstructor(new Dictionary<string, object>()
                                       {
                                           {"isCool", true},
                                           {"myAge", 31}
                                       }));

            var primitiveObject = objectBoss.GetInstance<ObjectWithPrimitives>();

            Assert.AreEqual(primitiveObject.IsCool, true);
            Assert.AreEqual(primitiveObject.MyAge, 31);
        }
Exemplo n.º 11
0
        public void should_be_able_to_configure_a_constructor_with_primitives_and_inject_complex_type()
        {
            var objectBoss = new ObjectBoss();
            objectBoss.Configure(x =>
            {
                x.Add<ObjectWithPrimitives>()
                    .WithCustomConstructor(new Dictionary<string, object>()
                                                                {
                                                                    {"isCool", true},
                                                                    {"myAge", 31}
                                                                });
                x.Add<ComplexObjectWithTwoDependencies>();

            });

            var primitiveObject = objectBoss.GetInstance<ObjectWithPrimitives>();

            Assert.AreEqual(primitiveObject.IsCool, true);
            Assert.AreEqual(primitiveObject.MyAge, 31);
            Assert.IsNotNull(primitiveObject.ComplexObject);
            Assert.IsNotNull(primitiveObject.ComplexObject.OneDependencyObject);
            Assert.IsNotNull(primitiveObject.ComplexObject.SimpleObjectType);
        }
Exemplo n.º 12
0
 public void should_wire_up_objects_for_complex_object_with_two_dependencies()
 {
     var objectBoss = new ObjectBoss();
     objectBoss.Configure(x =>
     {
         x.Add<SimpleObjectType>();
         x.Add<ObjectWithOneDependency>();
         x.Add<ComplexObjectWithTwoDependencies>();
     });
     var complexObject = objectBoss.GetInstance<ComplexObjectWithTwoDependencies>();
     Assert.IsInstanceOfType(complexObject, typeof(ComplexObjectWithTwoDependencies));
     Assert.IsNotNull(complexObject.SimpleObjectType);
     Assert.IsInstanceOfType(complexObject.SimpleObjectType, typeof(SimpleObjectType));
     Assert.IsNotNull(complexObject.OneDependencyObject);
     Assert.IsInstanceOfType(complexObject.OneDependencyObject, typeof(ObjectWithOneDependency));
     Assert.IsNotNull(complexObject.OneDependencyObject.SimpleObjectType);
     Assert.IsInstanceOfType(complexObject.OneDependencyObject.SimpleObjectType, typeof(SimpleObjectType));
 }
Exemplo n.º 13
0
 public void should_return_parameterless_object_instance()
 {
     var objectBoss = new ObjectBoss();
     objectBoss.Configure(x => x.Add<SimpleObjectType>());
     var simpleObject = objectBoss.GetInstance<SimpleObjectType>();
     Assert.IsNotNull(simpleObject);
     Assert.IsInstanceOfType(simpleObject, typeof(SimpleObjectType));
 }
Exemplo n.º 14
0
        public void should_get_crushed_by_threads_and_work()
        {
            var simpleObjects = new List<SimpleObjectType>();
            var complexDependentObjects = new List<ComplexObjectWithInterfaceDependencies>();

            var objectBoss = new ObjectBoss();
            objectBoss.Configure(x =>
                {
                    x.AddUsingDefaultType<ISimpleInterface, SimpleObjectType>();
                    x.AddUsingDefaultType<IComplexInterface, ComplexObjectWithTwoDependencies>().Singleton();
                    x.Add<ComplexObjectWithInterfaceDependencies>();
                    x.Add<SimpleObjectType>();
                });

            Parallel.For(0, 10000, i =>
                {
                    simpleObjects.Add(objectBoss.GetInstance<SimpleObjectType>());
                    complexDependentObjects.Add(objectBoss.GetInstance<ComplexObjectWithInterfaceDependencies>());
                    Thread.Sleep(2);
                });
            Parallel.For(0, 10000, i =>
            {
                simpleObjects.Add(objectBoss.GetInstance<SimpleObjectType>());
                complexDependentObjects.Add(objectBoss.GetInstance<ComplexObjectWithInterfaceDependencies>());
                Thread.Sleep(4);
            });
            Parallel.For(0, 10000, i =>
            {
                simpleObjects.Add(objectBoss.GetInstance<SimpleObjectType>());
                complexDependentObjects.Add(objectBoss.GetInstance<ComplexObjectWithInterfaceDependencies>());
                Thread.Sleep(6);
            });

            Assert.IsTrue(simpleObjects.Count == 30000);
            Assert.IsTrue(complexDependentObjects.Count == 30000);
        }
Exemplo n.º 15
0
        public void should_retrieve_a_default_type()
        {
            var objectBoss = new ObjectBoss();
            objectBoss.Configure(x => x.AddUsingDefaultType<ISimpleInterface, SimpleObjectType>());

            var simpleType = objectBoss.GetInstance<ISimpleInterface>();
            Assert.IsNotNull(simpleType);
            Assert.IsTrue(simpleType.Name == "SimpleObject1");
        }
Exemplo n.º 16
0
        public void should_retreive_an_object_based_purely_on_key()
        {
            var objectBoss = new ObjectBoss();
            objectBoss.Configure(x =>
            {
                x.AddUsing<ISimpleInterface, SimpleObjectType>("object1");
            });

            var object1 = objectBoss.GetInstance("object1") as ISimpleInterface;
            Assert.IsNotNull(object1);
            Assert.IsTrue(object1.Name == "SimpleObject1");
        }
Exemplo n.º 17
0
 public void should_get_multiple_instances_of_objects_that_are_not_defined_as_singleton()
 {
     var objectBoss = new ObjectBoss();
     objectBoss.Configure(x => x.Add<SimpleObjectType>());
     var simpleObject1 = objectBoss.GetInstance<SimpleObjectType>();
     var simpleObject2 = objectBoss.GetInstance<SimpleObjectType>();
     Assert.IsTrue(objectBoss.GetRegisteredClassCount() == 2);
     Assert.IsNotNull(simpleObject1);
     Assert.IsNotNull(simpleObject2);
     Assert.IsTrue(simpleObject1.Id != simpleObject2.Id);
 }
Exemplo n.º 18
0
        public void should_inject_singleton_interface_and_get_instance_singleton_interface_and_they_should_be_the_same()
        {
            var objectBoss = new ObjectBoss();
            objectBoss.Configure(x =>
            {
                x.AddUsingDefaultType<ISimpleInterface, SimpleObjectType>();
                x.AddUsingDefaultType<IComplexInterface, ComplexObjectWithTwoDependencies>().Singleton();
                x.Add<ComplexObjectWithInterfaceDependencies>();
            });

            var complexObjectWithInterfaceDependencies = objectBoss.GetInstance<ComplexObjectWithInterfaceDependencies>();
            var complexInterface = objectBoss.GetInstance<IComplexInterface>();

            Assert.IsNotNull(complexObjectWithInterfaceDependencies);
            Assert.IsNotNull(complexInterface);
            Assert.IsTrue(complexInterface.Id == complexObjectWithInterfaceDependencies.ComplexInterface.Id);
        }
Exemplo n.º 19
0
        public void should_inject_simple_object_type_into_object_with_one_dependency()
        {
            var objectBoss = new ObjectBoss();
            objectBoss.Configure(x =>
                {
                    x.Add<SimpleObjectType>();
                    x.Add<ObjectWithOneDependency>();
                });

            var objectWithOneDependecy = objectBoss.GetInstance<ObjectWithOneDependency>();
            Assert.IsInstanceOfType(objectWithOneDependecy, typeof(ObjectWithOneDependency));
            Assert.IsNotNull(objectWithOneDependecy.SimpleObjectType);
            Assert.IsInstanceOfType(objectWithOneDependecy.SimpleObjectType, typeof(SimpleObjectType));
        }
Exemplo n.º 20
0
        public void should_inject_interfaces_as_dependencies()
        {
            var objectBoss = new ObjectBoss();
            objectBoss.Configure(x =>
            {
                x.AddUsingDefaultType<ISimpleInterface, SimpleObjectType>();
                x.AddUsingDefaultType<IComplexInterface, ComplexObjectWithTwoDependencies>();
                x.Add<ComplexObjectWithInterfaceDependencies>();
            });

            var complexObjectWithInterfaceDependencies = objectBoss.GetInstance<ComplexObjectWithInterfaceDependencies>();
            Assert.IsNotNull(complexObjectWithInterfaceDependencies.ComplexInterface);
            Assert.IsNotNull(complexObjectWithInterfaceDependencies.SimpleInterface);
            Assert.IsInstanceOfType(complexObjectWithInterfaceDependencies.ComplexInterface, typeof(IComplexInterface));
            Assert.IsInstanceOfType(complexObjectWithInterfaceDependencies.SimpleInterface, typeof(ISimpleInterface));
        }
Exemplo n.º 21
0
        public void should_retrieve_default_type_when_multiple_types_are_defined()
        {
            var objectBoss = new ObjectBoss();
            objectBoss.Configure(x =>
            {
                x.AddUsing<ISimpleInterface, SimpleObjectType>();
                x.AddUsingDefaultType<ISimpleInterface, AnotherSimpleObject>();
            });

            var anotherSimpleObject = objectBoss.GetInstance<ISimpleInterface>();
            Assert.IsNotNull(anotherSimpleObject);
            Assert.IsTrue(anotherSimpleObject.Name == "SimpleObject2");
        }
Exemplo n.º 22
0
        public void should_be_able_to__do_custom_wiring()
        {
            var objectBoss = new ObjectBoss();

            var complexObject = new ComplexObjectWithTwoDependencies(new SimpleObjectType(),
                new ObjectWithOneDependency(new SimpleObjectType()))
            {
                Id = Guid.NewGuid(),
                OneDependencyObject = { Id = Guid.NewGuid() },
                SimpleObjectType = { Id = Guid.NewGuid() }
            };

            objectBoss.Configure(x =>
            {
                x.Add<ObjectWithPrimitives>()
                    .WithCustomConstructor(new Dictionary<string, object>()
                                                                {
                                                                    {"isCool", true},
                                                                    {"myAge", 31},
                                                                    {"complexObject",complexObject}
                                                                });
                x.Add<ComplexObjectWithTwoDependencies>();

            });

            var primitiveObject = objectBoss.GetInstance<ObjectWithPrimitives>();

            Assert.AreEqual(primitiveObject.IsCool, true);
            Assert.AreEqual(primitiveObject.MyAge, 31);

            Assert.AreEqual(primitiveObject.MyAge, 31);
            Assert.IsNotNull(primitiveObject.ComplexObject);
            Assert.IsNotNull(primitiveObject.ComplexObject.OneDependencyObject);
            Assert.IsNotNull(primitiveObject.ComplexObject.SimpleObjectType);

            Assert.AreEqual(complexObject.Id, primitiveObject.ComplexObject.Id);
            Assert.AreEqual(complexObject.OneDependencyObject.Id, primitiveObject.ComplexObject.OneDependencyObject.Id);
            Assert.AreEqual(complexObject.SimpleObjectType.Id, primitiveObject.ComplexObject.SimpleObjectType.Id);
        }