public void ShouldSetPropertyValue() { var targetType = typeof(SampleClassWithInjectionProperties); var targetProperty = targetType.GetProperty("SomeProperty"); Assert.NotNull(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"); var setter = container.GetService <IPropertySetter>(); Assert.NotNull(setter); setter.Set(instance, targetProperty, service); Assert.NotNull(instance.SomeProperty); Assert.Same(service, instance.SomeProperty); }
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.Same(result, instance); // On initialization, the instance.SomeProperty value // should be a SampleClass type Assert.NotNull(instance.SomeProperty); Assert.Equal(typeof(SampleClass), instance.SomeProperty?.GetType()); }