How does AOP help in data binding?
I recently read an interesting blog post on Anders Hejlsberg's arguments against AOP.
The first anti-anti argument mentions data binding:
Myth 1. “Aspect-oriented programming is interesting for debugging and instrumentation of code and is not a full-fledged programming discipline.”
Truth 1. Anders probably stopped at the “Hello, world” example.
Although code instrumentation is certainly an important use case of AOP – and the one you would see in every “getting started” documentation – the technology significantly simplifies the work of developers when it comes to implement any non-trivial, real-life application. Just to cite a few real-life scenarios where AOP really helps:
* Data Binding (INotifyPropertyChanged)
I'm trying to开发者_开发百科 think of how AOP is used in a data binding scenario. I always assumed that binding relied on reflection to do it's "magic." I'm pretty sure everything you need in a binding scenario is available via reflection. Is AOP being used for a (slight) performance boost?
It's not the databinding he's referring to, but the INotifyPropertyChanged
(and similar) part that AOP would really shine.
Currently, when a class implements INotifyPropertyChanged
, properties look like this:
private bool _isSomeProperty;
public bool IsSomeProperty
{
get{ return _isSomeProperty;}
set
{
if( value != _isSomeProperty)
{
_isSomeProperty = value;
OnNotifyPropertyChanged( "IsSomeProperty");
}
}
}
Where with decent AOP, they could look like this
[NotifyOnChange]
public bool IsSomeProperty {get; set;}
Makes a big readability difference, especially when the setter for a couple properties has a few actual rules in it.
Even with generic base classes, Expressions, Reflection, and some tricky implementations, the best you hope for is:
private bool _isSomeProperty;
public bool IsSomeProperty
{
get{ return _isSomeProperty;}
set
{
SetAndNotify( x=>x.IsSomeProperty)
}
}
And even that's not as readable (and is less performant as well)
精彩评论