开发者

Is it okay to break LSP for the sake of binding?

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 prope开发者_运维百科rties 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜