如何避免在两个类似的WinForms复制逻辑是什么? [英] How to avoid duplicating logic on two similar WinForms?

查看:95
本文介绍了如何避免在两个类似的WinForms复制逻辑是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式,A型和B形这些表格必须在外观上有所区别,但他们分享了很多逻辑。问题是,这种逻辑是联系在一起的外观(验证上按一下按钮,事件被解雇等)。例如,我有一个名字字段,并按下保存按钮时,我需要火导致父窗体验证记录名称,以避免重复的事件。这两种形式都需要这样的逻辑,但他们的保存按钮都在不同的地方,并在发生错误时也需要出现在不同的地方所显示的提示。这只是一个例子,但没有人知道的一种方式,我可以在这里避免复制和粘贴代码?也许我缺少明显的东西...

I have two forms, form A and form B. These forms must differ in appearance, but they share a lot of logic. The problem is that this logic is tied to the appearance (validation on button click, events being fired, etc.). For example, I have a name field, and when the save button is pressed, I need to fire an event which causes the parent form to validate the record name to avoid duplicates. Both forms need this logic, but their save buttons are in different places, and the tooltip that is shown when an error occurs also needs to appear in a different place. This is just one example, but does anyone know of a way that I can avoid copying and pasting code here? Perhaps I am missing something obvious...

推荐答案

您可以创建与在两种形式表示的数据的对象,并把在该对象的验证逻辑。表示层应填充该对象与输入的数据,询问该验证对象本身,然后在一个特定形式的双向处理验证错误。

You could create an object with data that is represented in both forms, and put validation logic in that object. The presentation layer should populate that object with the entered data, ask the object to validate itself, and then handle validation errors in a form-specific way.

这篇关于如何避免在两个类似的WinForms复制逻辑是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆