重构为 n 层
我是一名自学的 vb6 程序员,使用 DAO。 下面是我可以编写的一段典型代码的示例:
Sub cmdMultiplier_Click() 'Button on form, user interface '
dim Rec1 as recordset
dim strSQL as string
strSQL = "select * from tblCustomers where ID = " & CurrentCustomerID 'inline SQL '
set rec1 = GlobalDataBase.openrecordset(strSQL) ' Data access '
if rec1.bof <> true or rec1.eof <> true then
if rec1.fields("Category").value = 1 then
PriceMultiplier = 0.9 ' Business Logic '
else
priceMultiplier = 1
end if
end if
End Sub
请假装上面是 CRUD 应用程序的完整源代码。 我知道这个设计很糟糕,所有东西都混在一起了。 理想情况下,它应该具有三个不同的层:用户界面、业务逻辑 和数据访问。 我有点明白为什么这是可取的,但我不知道它是如何完成的,我怀疑 这就是为什么我不完全明白为什么这样的分离是好的。 我认为如果有人可以荒谬地重构上述内容,我会走得更远 将一个简单的例子分为 3 层。
I am a self taught vb6 programmer who uses DAO. Below is an example of a typical piece of code that I could churn out:
Sub cmdMultiplier_Click() 'Button on form, user interface '
dim Rec1 as recordset
dim strSQL as string
strSQL = "select * from tblCustomers where ID = " & CurrentCustomerID 'inline SQL '
set rec1 = GlobalDataBase.openrecordset(strSQL) ' Data access '
if rec1.bof <> true or rec1.eof <> true then
if rec1.fields("Category").value = 1 then
PriceMultiplier = 0.9 ' Business Logic '
else
priceMultiplier = 1
end if
end if
End Sub
Please pretend that the above is the entire source code of a CRUD application.
I know this design is bad, everything is mixed up together. Ideally it should have three distinct layers, user interface, business logic
and data access. I sort-of get why this is desirable but I don't know how it's done and I suspect
that's why I don't fully get why such a separation is good.
I think I'd be a lot further down the road if someone could refactor the above ridiculously
trivial example into 3 tiers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该按钮的用途是什么?
我的第一步是:
What is the purpose of the button?
My first steps would be:
是的,这是一个简单的例子,但具有所有基本元素 - 它们只是属于 3 个不同的类别(见下文)。 主要原因是“关注点分离”原则,即GUI只关心GUI的东西,Biz逻辑层只关心业务规则,而数据访问层只关心数据表示。 这允许每个层独立维护并跨应用程序重用:
[“真实”应用程序将缓存当前客户,具有用于客户查询的常量或存储过程等; 为简洁起见,忽略]
将此与您原来的一切按钮处理程序示例进行对比(这在 VB 代码中非常常见,因为这样做很容易) - 如果您需要在另一个应用程序中使用价格乘数规则,您必须将代码复制、粘贴并编辑到该应用程序的按钮处理程序中。 现在将有两个地方维护相同的业务规则,并且有两个地方执行相同的客户查询。
a trivial example, yes, but with all the basic elements - they just belong in 3 different classes (see below). The main reason for this is the "separation of concerns" principle, i.e. the GUI is only concerned with GUI things, the Biz Logic layer is only concerned with the business rules, and the data-access layer is only concerned with data representations. This allows each layer to be maintained independently and reused across applications:
[a 'real' application would cache the current customer, have constants or stored procedures for the customer query, etc.; ignored for brevity]
Contrast this with your original everything-in-the-button-handler example (which is appallingly common in VB code because it is so easy to do it that way) - if you needed the price-multiplier rule in another application, you'd have to copy, paste, and edit the code into that application's button-handler. Now there would be two places to maintain the same business rule, and two places where the same customer query was executed.
通常,您的 UI 代码会响应用户引发的事件,在本例中为“按钮单击”。
之后,这实际上取决于您的程序的设计方式,最基本的设计是引用 Customer 实例,并且它将包含一个 multiplier 属性。
您的客户对象是根据 DAL 中的数据填充的。
UI 的验证将进入 UI 层,业务验证规则可以进入您的业务对象,然后您的 DAL 就是您的持久层。
这是一个非常基本的伪代码示例:
Typically you will have your UI code responding to the events raised by the user, in this case the Button Click.
After that it really depends on how your program is designed, the most basic design would be to reference a Customer instance and it would contain a multiplier property.
Your customer object is populated from data in your DAL.
Validation for UI would go in UI layer, business validation rules could go into your business object, and then your DAL is your persistence layer.
Here is a very basic pseudo-code example:
知道如何重构是一件好事。 从现在起您将知道如何分离图层。
不过,我认为您最好花时间来升级您同时使用的工具。 你有考虑过用VB.Net来做吗?
保留现有代码库的一种方法是在 VB.Net 中对数据层和 BR 进行编码。 然后通过COM接口公开BR(这是项目中的复选框选项)。 然后,您可以从当前界面使用新的 BR。
完成所有 BR 和 DAL 后,您将迈向全新平台。
Knowing how to refactor is a good thing. From now you will know how to separate layers.
However, I think your time will be better spend to upgrade the tools you are using at the same time. Do you have consider to do it with VB.Net ?
A way to do it will preserving your existing code base is to code the Data layer and BR in VB.Net. Then to expose the BR through COM Interface (this is a check box option in the project). You can then use the new BR from your current interface.
Once all BR and DAL done, you will be a step away to a complete new platform.