为了绑定而破坏LSP可以吗?

发布于 2024-12-09 04:20:38 字数 1431 浏览 0 评论 0原文

有件事告诉我,我可能会因为问这个问题而被处以私刑。对于提前的冗长描述表示歉意。

我正在研究 Silverlight 4 项目中的一些特殊情况。本质上我正在构建的是一个自定义表单生成器。

一个表单可能有多个不同类型的字段(文本、整数、电子邮件等)。现在,所有这些类型都扩展了 Field 类,但可能具有其他属性,例如文本字段的长度。

我能够在 FormFieldsManagementViewModel 中的 FieldViewModels 集合中添加和删除字段。所以到目前为止,这都是非常标准的东西。

现在,...为了让用户针对 Field 对象设置属性,我有一个 UserControl ,它具有 DataTemplate 类型的依赖属性,并且代表我想要的 UI当选择特定类型的字段时显示。因此,澄清一下,UserControl 有一个 SingleLineTextTemplate 属性,当选择 SingleLineTextFieldViewModel 但选择 EmailFieldViewModel 时,该属性将显示。选择 code> 后,将显示 EmailFieldTemplateSingleLineTextFieldViewModelEmailFieldViewModel 均继承自 FieldViewModel

当我在每个模板中声明绑定时,我的问题就出现了。设置基类 FieldViewModel 的属性(如 IsRequiredPosition)是非常有效的(恕我直言),但我也有 Length< 的绑定SingleLineTextTemplate 中的 /code> 与 SingleLineTextFieldViewModel 的 Length 属性相匹配。因此,我依赖于具体类的扩展接口,而不仅仅是 FieldViewModel 类。

在我继续之前,我应该指出这有效。我不确定它会,现在确实如此,我不确定它应该......或者我什至应该这样做。

我这样做只是为了绑定,并且在没有绑定到动态对象和/或 Silverlight 中缺少 DataTemplateSelector 的情况下。

显然,在绑定场景中,SingleLineTextFieldViewModel 无法替代它的基类。我的问题是,因为我并不是在代码中真正使用这些对象,而是在标记中使用这些对象,..这仍然被认为是不好的做法吗?

预先非常感谢。

Something tells me I might get lynched for asking this. And sorry for the long winded description in advance.

I'm working on something of a corner case in a Silverlight 4 project. Essentially what I'm building is a custom form builder.

A form may have several fields which may be of different types (text, integer, email etc...). Now, all of these types extend the Field class but might have additional properties like length in the case of the text field.

I have the ability to add and remove fields from a collection of FieldViewModels in my FormFieldsManagementViewModel. So it's all pretty standard stuff so far.

Now,... In order for the user to set properties against the Field objects I have a UserControl which has Dependency Properties of type DataTemplate and which represent the UI I want to dipsplay when a particular type of field is selected. So , to clarify, the UserControl has a SingleLineTextTemplate property which will be shown when a SingleLineTextFieldViewModel is selected but when an EmailFieldViewModel is selected, the EmailFieldTemplate is shown. Both the SingleLineTextFieldViewModel and EmailFieldViewModel inherit from FieldViewModel.

My issue arises when I declare the bindings in each of these templates. It's quite valid (IMHO) to set properties of the base class FieldViewModel like IsRequired and Position but I also have bindings for Length in the SingleLineTextTemplate that match the Length property of the SingleLineTextFieldViewModel. So I'm relying on the extended interface of the concrete class rather than just the FieldViewModel class.

I should point out, before I carry on that this works. I wasn't sure it would and now that does, I'm not sure it should,..or that I should even be doing this.

I'm only doing this for the sake of binding and in the absence of binding to dynamic objects and/or the lack of DataTemplateSelector in Silverlight.

Clearly, SingleLineTextFieldViewModel is not substitutable for it's base class in a binding scenario. My question is, since I'm not really consuming these objects in code, but in markup,..Is this still considered bad practice?

Lots of thanks in advance.

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

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

发布评论

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

评论(1

待天淡蓝洁白时 2024-12-16 04:20:38

我对此不会有任何问题 - 我过去也做过相同或类似的事情。

假设您仅在 DataContextSingleLineTextFieldViewModel 的实例时使用 SingleLineTextTemplate(大概通过将 DataTemplate 映射到类型),那么就永远不会有任何问题,并且由于绑定框架的弹性性质,即使您确实尝试将模板用于不适当的类型,也不会产生任何显着的负面影响。

I wouldn't have any problem with this - I have done the same or similar in the past.

Assuming that you only use your SingleLineTextTemplate when the DataContext is an instance of SingleLineTextFieldViewModel (presumably through mapping the DataTemplate to the type) then there is never any problem, and because of the resilient nature of the binding framework you are not going to have any significant negative effects even if you do try to use the template for an inappropriate type.

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