ARMPLINKER (1) [Avatar] Offline
#1
(Unity 2.0, April drop, so latest I could find)

I have read your chapter on Unity, and traversed many paths on the Internet to determine how to use it to do Property Injection, and I still end up with an instantiated object with a null ILog property that is supposed to be injected magically and reuse the instance from the container. So the class throws an exception and the viewmodel locator construction process dies.

There are several issues/ concerns:

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<T> but it never gets a property value (the property is an ILog object).
3) For tidiness reasons, which I admit are somewhat OCD, I have separated different classes of objects into different child containers. The ParentContainer holds a few objects such as ILog while the class I am trying to resolve is in the child DataServicesContainer. Perhaps this may be part of the issue?
4) It also seems like some of this injection stuff may have been overhauled for 2.0 and perhaps it is just broken?


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 are welcome

The code for the object that needs injection:

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

// A customized version of the standard Log4Net ILog
public ILog Logger {get;set;}
...
...
public RuntimeFilesRepository()
{

...
...
// INJECTION NEEDS TO HAPPEN, BUT NEVER DOES, SO THIS IS A NULL OBJECT
Logger.Debug("I do like my CaesarSalad with the extra chicken");

}


and the registration (which is a method inside the ViewModelLocator class in my case) looks like this:

public void WireUp()
{

...
...

// a container

ParentContainer = new UnityContainer();

// this thing is really helpful!!! https://github.com/dbuksbaum/unity.extensions
ParentContainer.AddNewExtension<TypeTrackingExtension>();

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

// and an instance of an ILog. It can be referred to as 'LoggingService'

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

...
//various Logger log statements work here as we plod along, by the way
...
...

// then

// works, type is registered, shows up in the debugger,
// but where the heck are the injection properties within the registered item???
// Gotta be somewhere in the hive of bees

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

// runtime files repo, singleton
// allow Unity to resolve the RUN TIME files repositoryand hold onto reference
// DOES NOT WORK. Instantiates RuntimeFilesRepository but does not inject

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

// never get here....

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

...
// lots more of the same stuff with different salad dressings and sometimes even croutons.
...

}

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. Ambiguous at best.

I think your book has been very helpful, and the recipes were tasty,
but I am greatly afeared that Unity = DisUnityand I may have screwed myself even trying to use it.

Thank you in advance for any guidance, or referrals, or whatever.

Allen

Message was edited by:
ARMPLINKER

Message was edited by:
ARMPLINKER
mark.seemann (383) [Avatar] Offline
#2
Re: Unity Property Injection problematic - properties are ALWAYS null
Sorry about the late reply, but perhaps you'll get a better response on Stack Overflow. I'm pretty busy with the book's proof reading, as well as my day job, so I haven't been able to set aside the time to answer this question. Sorry.