AOP 如何帮助数据绑定?

发布于 2024-10-03 11:12:05 字数 595 浏览 2 评论 0原文

我最近读了一篇有趣的文章关于 Anders Hejlsberg 反对 AOP 的论点的博客文章

第一个反反论证提到了数据绑定:

神话 1。“面向方面的编程对于代码的调试和检测很有趣,但它并不是一个成熟的编程学科。”

真相 1。安德斯可能停在“Hello, world”的例子上。

尽管代码插装无疑是 AOP 的一个重要用例(并且您会在每个“入门”文档中看到该用例),但该技术在实现任何重要的现实生活中时显着简化了开发人员的工作应用。仅举几个 AOP 真正有帮助的现实场景:

* Data Binding (INotifyPropertyChanged) 

我正在尝试考虑如何在数据绑定场景中使用 AOP。我一直认为绑定依赖于反射来发挥它的“魔力”。我非常确定绑定场景中所需的一切都可以通过反射获得。 AOP 是否用于(轻微)性能提升?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黒涩兲箜 2024-10-10 11:12:05

他指的不是数据绑定,而是 AOP 真正大放异彩的 INotifyPropertyChanged (以及类似的)部分。

目前,当一个类实现 INotifyPropertyChanged 时,属性看起来像这样:

private bool _isSomeProperty;
public bool IsSomeProperty
{
  get{ return _isSomeProperty;}
  set
  { 
    if( value != _isSomeProperty)
    {
      _isSomeProperty = value;
      OnNotifyPropertyChanged( "IsSomeProperty");
    }
  }
}

在适当的 AOP 中,它们可能看起来像这样

[NotifyOnChange]    
public bool IsSomeProperty {get; set;}

造成很大的可读性差异,特别是当几个属性的设置器在其中有一些实际规则时它。

即使使用通用基类、表达式、反射和一些棘手的实现,您所希望的最好结果是:

private bool _isSomeProperty;
public bool IsSomeProperty
{
  get{ return _isSomeProperty;}
  set
  { 
    SetAndNotify( x=>x.IsSomeProperty)
  }
}

即使这样可读性也较差(并且性能也较差)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文