We are using Spring.Net for dependency injection at my company, DigitalFocus. It has worked out very well for us and aided us greatly in unit testing. There has only been one major snag, and it involves trying to use Spring.Net to inject internal classes. Internal classes are a good place to put plumbing that needs to be used inside an assembly that shouldn't be accessed directly from outside the assembly (such as low level database access code).
In order to inject an internal class into a public class, the property used for injection must also be internal (or else the compiler complains - inconsistent accessibility). This also is true for constructor or factory method injection. The problem is that Spring.Net does not use the NonPublic binding flag when looking for the property, constructor, or factory method to use for injection. I have been able to modify the Spring.Net code to make this work, but would like to know the intent in not allowing non-public members to participate in injection.
For reference, here are the 3 changes I made:
Spring.Objects.ObjectWrapper, changed a constant on line 110 (added BindingFlags.NonPublic):
private const BindingFlags PropertyResolutionFlags =
BindingFlags.Public
| BindingFlags.NonPublic
| BindingFlags.SetProperty
| BindingFlags.Static
| BindingFlags.Instance
| BindingFlags.IgnoreCase;
Spring.Objects.Factory.Support.AbstractAutowireCap ableObjectFactory, changed line 971 in method: protected IObjectWrapper AutowireConstructor (string objectName, RootObjectDefinition definition)
ConstructorInfo[] constructors = definition.ObjectClass.GetConstructors(BindingFlag s.Public | BindingFlags.NonPublic | BindingFlags.Instance);
Spring.Objects.Factory.Support.AbstractAutowireCap ableObjectFactory, changed line 781 in method: protected virtual IObjectWrapper InstantiateUsingFactoryMethod (string objectName, RootObjectDefinition definition, object [] arguments)
BindingFlags methodFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase;


Reply With Quote