コード例 #1
0
        public void ShouldAutoInjectProperty()
        {
            var container = new ServiceContainer();
            container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll");

            var instance = new SampleClassWithInjectionProperties();

            // Initialize the container
            container.Inject<ISampleService>().Using<SampleClass>().OncePerRequest();
            container.Inject<ISampleService>("MyService").Using(c => instance).OncePerRequest();

            var result = container.GetService<ISampleService>("MyService");
            Assert.AreSame(result, instance);

            // On initialization, the instance.SomeProperty value
            // should be a SampleClass type
            Assert.IsNotNull(instance.SomeProperty);
            Assert.IsInstanceOfType(typeof(SampleClass), instance.SomeProperty);
        }
コード例 #2
0
        public void ShouldSetPropertyValue()
        {
            var targetType = typeof(SampleClassWithInjectionProperties);
            var targetProperty = targetType.GetProperty("SomeProperty");
            Assert.IsNotNull(targetProperty);

            // Configure the target
            var instance = new SampleClassWithInjectionProperties();

            // This is the service that should be assigned
            // to the SomeProperty property
            object service = new SampleClass();

            // Initialize the container
            var container = new ServiceContainer();
            container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll");

            IPropertySetter setter = container.GetService<IPropertySetter>();
            Assert.IsNotNull(setter);

            setter.Set(instance, targetProperty, service);

            Assert.IsNotNull(instance.SomeProperty);
            Assert.AreSame(service, instance.SomeProperty);
        }