Trying to follow your own advice is something that only sounds easy. The case in point: ubiquitous use of auto-properties. The idea sounds nice, but it doesn't quite work.
public class C
{
public IList MySomethings { get; set; }
}
Having lots of lists in a class is tough: each IList deserves its own List initialization. In an ideal world, you could do the following:
public class C
{
[Dependency]
public IList MySomethings { get; set; }
}
// and then
var uc = new UnityContainer();
uc.RegisterType, List>();
But we cannot do it. So, aside from the option of creating everything manually (ugh) and using Nemerle (I'm almost there), the only way to init those stupid lists manually (I have many) is to write the following:
public class C
{
[LazyLoad(typeof(List))]
public IList MySomethings; // it's a field!
}
Using a PostSharp aspect certainly might do the trick, but it's only simple if it's a field. Nonetheless, since fields can be rewritten by PostSharp into properties, we might get the semantics similar to the following:
public class C
{
private IList mySomethings;
public IList MySomethings
{
get { return mySomethings ?? (mySomethings = new List(); }
// where is the set? :)
}
}
This bit of metaprogramming is naughty: we have a set-less property in place of a field. It's lazy-loaded so none of that null nonsense.
This solution can even be done without AOP. It works, and is moderately readable (I hope everyone recognizes the ?? operator). It's not very elegant in the sense where I might want to init this stuff from a container but then again, in my case all I need are empty lists. As a future task, I'll investigate Nemerle for a simpler, DSL=like way of defining these structures.
On a happy note: I found some code that used INotifyPropertyChanged and refactored it, removing any mention of that interface completely. AOP rules!