为了绑定而破坏LSP可以吗?
有件事告诉我,我可能会因为问这个问题而被处以私刑。对于提前的冗长描述表示歉意。
我正在研究 Silverlight 4 项目中的一些特殊情况。本质上我正在构建的是一个自定义表单生成器。
一个表单可能有多个不同类型的字段(文本、整数、电子邮件等)。现在,所有这些类型都扩展了 Field
类,但可能具有其他属性,例如文本字段的长度。
我能够在 FormFieldsManagementViewModel
中的 FieldViewModels
集合中添加和删除字段。所以到目前为止,这都是非常标准的东西。
现在,...为了让用户针对 Field 对象设置属性,我有一个 UserControl
,它具有 DataTemplate
类型的依赖属性,并且代表我想要的 UI当选择特定类型的字段时显示。因此,澄清一下,UserControl
有一个 SingleLineTextTemplate
属性,当选择 SingleLineTextFieldViewModel
但选择 EmailFieldViewModel
时,该属性将显示。选择 code> 后,将显示 EmailFieldTemplate
。 SingleLineTextFieldViewModel
和 EmailFieldViewModel
均继承自 FieldViewModel
。
当我在每个模板中声明绑定时,我的问题就出现了。设置基类 FieldViewModel
的属性(如 IsRequired
和 Position
)是非常有效的(恕我直言),但我也有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对此不会有任何问题 - 我过去也做过相同或类似的事情。
假设您仅在
DataContext
是SingleLineTextFieldViewModel
的实例时使用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 theDataContext
is an instance ofSingleLineTextFieldViewModel
(presumably through mapping theDataTemplate
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.