2

The various posts here do not show why every time I attempt to use the InjectionProperty() construct in Unity 2.0 (April) it never populates the properties in my resolved instance. They are always null. I can see in the debugger that the object is created, but the first reference to the property that is supposed to be injected is always a null reference exception. There must be something very fundamentally wrong in what I am doing.

Any help is greatly appreciated.

I have traversed many paths on the Internet to determine how to use Unity to do Property Injection, and I still end up with an instantiated object with a null property that is supposed to be injected. So it dies.

There are several issues:

1) Where are these PropertyInjector objects visible in the debugger? No amount of mining has revealed these, so I cannot determine if they are 'ready to inject'.

2) The object with the property does indeed instantiate via Resolve but it never gets a property value (the property is an ILog object).

I am at my wit's end, which admittedly might be a short piece of rope, but what the heck is going on with this? Any insights.

Here's the code:

// (Unity 2.1, May something or other drop, so I think this is the latest )  

public class RuntimeFilesRepository:IFilesRepository
{
...
...
...

// A customized version of the standard Log4Net ILog

public ILog Logger {get;set;} 
...
...

 public RuntimeFilesRepository()
{
...
... 

// INJECTION NEEDS TO HAPPEN BEFOE THIS, BUT NEVER DOES, SO THIS IS A NULL OBJECT 

Logger.Debug("I do like my CaesarSalad with the extra chicken"); 

}


// and the registration looks like this:**

public void WireUp()
{

...
...

// a container

        ParentContainer = new UnityContainer();

// this thing is really helpful!!! 

//  [https://github.com/dbuksbaum/unity.extensions][1] 

        ParentContainer.AddNewExtension<**TypeTrackingExtension**>();

// and the Logger type

        ParentContainer.RegisterType<ILog, Log4NetLog>("Logger",
                                                 new InjectionFactory(
                                                     factory => LogManager.GetLogger("Visual Element Migrator")));

// and an instance of an ILog.  It can be referred to as 'LoggingService'
// to use as a resolved parameter to inject   


 ILog Logger = ParentContainer.Resolve<ILog>("Logger");
        ParentContainer.RegisterInstance("LoggingService", Logger, new LifeTimeManager()); 


...  
...  


//various Logger log statements from the resolved ILog object work here as we plod along, by the way  

...  
...  

// then the next statement works, type is registered, shows up in the debugger,    
// but where the heck are the injection properties???     

    DataServicesContainer.RegisterType<IFilesRepository,RuntimeFilesRepository>(new InjectionProperty("Logger", Logger));  

// I have tried 3 different variants of the above InjectionProperty() to no avail.  

// runtime files repo, want a singleton
// allow Unity to resolve the  RUN TIME files repositoryand hold onto reference

// DOES NOT WORK.  Apparently instantiates RuntimeFilesRepository,  but does not inject the ILog to the Logger property


            var filesRepo = DataServicesContainer.Resolve<RuntimeFilesRepository>();


// *never gets here where I try to register the object so it can be REUSED in other contexts....*

            DataServicesContainer.RegisterInstance<IFilesRepository>("FilesRepositoryDataService", filesRepo,  new LifeTimeManager()); // to inject, singleton

...  
...  
// lots more of the same sort of class register and instantiate stuff
...  
...  

}

I do see in several locations that marking a property with [Dependency]
seems to be required, and other places where it says those markings will be overridden by the use of the InjectionProperty object in code. Discussion is ambiguous.

I am greatly afeared that Unity = DisUnity and I may have screwed myself even trying to use it.

AllenM
  • 201
  • 2
  • 6
  • When you put 4 spaces befor your code, it will become a code block. If you like to get some help, please reformat your code, because it is very hard to figure out what's going on right now. – Steven Jun 09 '11 at 17:53

0 Answers0