可重用类库 - INotifyPropertyChanged
我正在开发一个 WPF 应用程序,其中它将引用一个类库,该类库主要包含业务对象和一些操作这些对象的方法。由于该库与 WPF 结合使用,因此我需要使用 INotifyPropertyChanged 接口。
但是,我确实看到该库的未来用途可能不会专门依赖 WPF(或需要属性更改通知)作为表示层。根据您的专业经验,最好是通过寻找替代方法来省略接口以确保 DLL 中的不必要代码“干净”以供将来使用,还是使用接口并继续前进?
(显然) ,这可以应用于各种其他编程实践)。
谢谢。
I am developing a WPF application in which it will reference a class library that contains mostly business objects and some methods to manipulate those objects. Since this library is being used in conjunction with WPF, I have the need to use the INotifyPropertyChanged interface.
However, I do see future uses in the library which may not specifically rely on WPF (or posses the need for property change notifications) as the presentation layer. In your professional experience, would it be better to omit the interface to ensure the DLL is "clean" from unnecessary code for future uses by finding an alternative method, or use the interface and move on?
(obviously, this could be applied to a variety of other programming practices).
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不同意 INotifyPropertyChanged 行为仅驻留在 ViewModel 中。虽然 INotifyPropertyChanged 在表示层中大量使用,但它的价值超出了表示层。
我的每个模型通常都包含 INotifyPropertyChanged 行为,而不仅仅是 ViewModel。
归根结底,如果您能够说服自己 INotifyPropertyChanged 行为可以由消费者在表示层外部使用,请将其保留。
I don't agree that INotifyPropertyChanged behavior is only to reside within the ViewModel. While INotifyPropertyChanged is heavily used within the Presentation layer, it's value extends beyond the Presentation layer.
Every one of my Models generally contains INotifyPropertyChanged behavior, not solely the ViewModel.
What this boils down to is if you can convince yourself that the INotifyPropertyChanged behavior may be used external to the Presentation layer by a consumer, leave it in.
我会省略业务层中的 INotifyPropertyChanged。
如果需要,我可以派生子级并使它们成为 INotifyPropertyChanged,或者我可以创建一个“模型”类,它可以包含我的业务对象的实例。
这也有助于将我的业务层与其他层(表示、数据访问等)分开,并允许我在服务器代码中使用所述对象,而无需了解有关 INotifyPropertyChanged 的任何信息。
I would omit the INotifyPropertyChanged in a business layer.
If I need to, I can derive children and make them INotifyPropertyChanged, or I can make a "model" class and it can contain an instance to my business objects.
This also helps to separate my business layers from my other layers (presentation, data access, etc.) as well as allowing me to use said objects in my server code which does not need to know anything about INotifyPropertyChanged.
INotifyPropertyChanged 实现属于 ViewModel,它封装了 GUI/View 的状态和相关行为,以便易于测试。 ViewModel 属于表示层。所有与视图无关的可重用代码都需要下推到另一层。因此,如果您需要换出 WPF,请构建一个使用该层的不同表示层,然后就可以开始了。较低层不应对其上方层做出假设。因此,该层中没有 INotifyPropertyChanged。
INotifyPropertyChanged implementations belong with ViewModels, which encapsulate the state of the GUI/View and related behavior so that they are easily testable. ViewModels belong in the Presentation layer. All view-agnostic reusable code needs to be pushed down to another layer. So if you need to swap out WPF, build a different presentation layer, which uses this layer and you're good to go. A lower layer should not make assumptions of the layer above it.. so no INotifyPropertyChanged in this layer.