예제 #1
0
        public void ConfigurationIsFluent()
        {
            var container = new MicroIocContainer();

            var config1 = container.GetConfiguration();
            var config2 = config1.Configure <Bar>(new InjectedProperty <Foo>("Foo", new Foo()));

            Assert.AreSame(config1, config2);
        }
예제 #2
0
        public void ConfigurationHoldsReferenceToContainer()
        {
            var container = new MicroIocContainer();

            var configuration = container.GetConfiguration();

            var configType = configuration.GetType();
            var fieldInfo  = configType.GetField("_container", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.AreSame(container, fieldInfo.GetValue(configuration));
        }
예제 #3
0
        public void BuildUpAppliesDependencyProperty()
        {
            var container = new MicroIocContainer();

            container.GetConfiguration()
                .Property<TestClassWithProperty, string>(x => x.CustomerName, "TestCustomerName");

            var obj = new TestClassWithProperty();

            container.BuildUp(obj);

            Assert.AreEqual("TestCustomerName", obj.CustomerName);
        }
예제 #4
0
        public void BuildUpAppliesDependencyProperty()
        {
            var container = new MicroIocContainer();

            container.GetConfiguration()
            .Property <TestClassWithProperty, string>(x => x.CustomerName, "TestCustomerName");

            var obj = new TestClassWithProperty();

            container.BuildUp(obj);

            Assert.AreEqual("TestCustomerName", obj.CustomerName);
        }
예제 #5
0
        public void BuildUpDoesNotApplyStandardProperty()
        {
            var container = new MicroIocContainer();

            container.GetConfiguration()
                .Property<TestClassWithProperty, string>(x => x.CustomerName, "TestCustomerName")
                .Property<TestClassWithProperty, string>(x => x.NotInjected, "NotInjected");

            var obj = new TestClassWithProperty();

            container.BuildUp(obj);

            Assert.IsNull(obj.NotInjected, string.Format("Should have been null, but was {0}", obj.NotInjected));
        }
예제 #6
0
        public void ConfiguredConstructorParameterInjectionGetsResolved()
        {
            const string connectionString =
                "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;";

            var container = new MicroIocContainer();

            container.GetConfiguration()
            .ConstructorParam <SampleRepository, string>("connectionString", connectionString);

            var instance = container.Resolve <SampleRepository>();

            Assert.AreEqual(connectionString, instance.ConnectionString);
        }
예제 #7
0
        public void ConfiguredPropertyInjectionGetsResolved()
        {
            const string customerName = "Bloggs & Co";
            var          container    = new MicroIocContainer()
                                        .Register <IFirst, First>()
                                        .Register <ISecond, Second>();

            container.GetConfiguration()
            .Property <TestClassWithProperties, string>(c => c.CustomerName, customerName);

            var instance = container.Resolve <TestClassWithProperties>();

            Assert.AreEqual(customerName, instance.CustomerName);
        }
예제 #8
0
        public void BuildUpDoesNotApplyStandardProperty()
        {
            var container = new MicroIocContainer();

            container.GetConfiguration()
            .Property <TestClassWithProperty, string>(x => x.CustomerName, "TestCustomerName")
            .Property <TestClassWithProperty, string>(x => x.NotInjected, "NotInjected");

            var obj = new TestClassWithProperty();

            container.BuildUp(obj);

            Assert.IsNull(obj.NotInjected, string.Format("Should have been null, but was {0}", obj.NotInjected));
        }
예제 #9
0
        public void ContainerResolvesConfiguredPropertyIfSpecified()
        {
            var first = new First();

            var container = new MicroIocContainer()
                            .Register <IFirst, First>()
                            .Register <ISecond, Second>();

            container.GetConfiguration()
            .Configure <TestClassWithProperties>(new InjectedProperty <string>("CustomerName", "Anything"))
            .Configure <TestClassWithProperties>(new InjectedProperty <First>("FirstProperty", first));

            container.GetConfiguration()
            .Configure <TestClassWithPropertiesAndConstructor>(new InjectedProperty <string>("CustomerName",
                                                                                             "Whatever"));

            var instance = container.Resolve <TestClassWithProperties>();

            Assert.AreSame(first, instance.FirstProperty);

            var otherInstance = container.Resolve <TestClassWithPropertiesAndConstructor>();

            Assert.AreNotSame(first, otherInstance.FirstProperty);
        }
예제 #10
0
        public void BuildUpResolvesAllInjectedProperties()
        {
            var container = new MicroIocContainer()
                .Register<IFirst, First>()
                .Register<ISecond, Second>();

            container.GetConfiguration()
                .Property<TestClassWithProperties, string>(x => x.CustomerName, "TestCustomerName");

            var obj = new TestClassWithProperties();

            container.BuildUp(obj);

            Assert.AreEqual("TestCustomerName", obj.CustomerName);

            Assert.IsNotNull(obj.FirstProperty, "FirstProperty shouldn't be null");
            Assert.IsInstanceOfType(obj.FirstProperty, typeof(First), "FirstProperty should a 'First' object");

            Assert.IsNotNull(obj.SecondProperty, "SecondProperty shouldn't be null");
            Assert.IsInstanceOfType(obj.SecondProperty, typeof(Second), "SecondProperty should a 'Second' object");
        }
예제 #11
0
        public void BuildUpResolvesAllInjectedProperties()
        {
            var container = new MicroIocContainer()
                            .Register <IFirst, First>()
                            .Register <ISecond, Second>();

            container.GetConfiguration()
            .Property <TestClassWithProperties, string>(x => x.CustomerName, "TestCustomerName");

            var obj = new TestClassWithProperties();

            container.BuildUp(obj);

            Assert.AreEqual("TestCustomerName", obj.CustomerName);

            Assert.IsNotNull(obj.FirstProperty, "FirstProperty shouldn't be null");
            Assert.IsInstanceOfType(obj.FirstProperty, typeof(First), "FirstProperty should a 'First' object");

            Assert.IsNotNull(obj.SecondProperty, "SecondProperty shouldn't be null");
            Assert.IsInstanceOfType(obj.SecondProperty, typeof(Second), "SecondProperty should a 'Second' object");
        }