Proper use of unity with abstract classes

Mar 5, 2010 at 1:26 PM

Could someone point me in the right direction for the use of a base class and dependency injection on properties?  

Lets say you have something like

public class MyViewModel : ViewModelBase<IMyView>

{

 

}

 

And

public abstract class ViewModelBase<TView>

{

[Dependency]

public IEventAggregator EventAggregator { get; set; }

}

 

If you call resolve on the container for the MyViewModel class the abstract class default constructor is called.  The properties on the abstract class cannot be used from the derived class during construction, but the properties are resolved when a method is called on the derived class.

 

 

Mar 6, 2010 at 3:40 AM

Properties aren't set until after the constructor has completed, just like when you call new directly. If you need the object in the constructor, use a constructor parameter instead.

 

May 11, 2010 at 6:10 AM

I don't know why, but in Unity 2.0 this didn't work - unity ignores attributes in abstract classes... :-( (In Unity 1.2 that worked fine) Breaking change :-(