OOP模式设计(数据访问)的常用方式是什么

发布于 2024-07-07 03:59:22 字数 976 浏览 7 评论 0原文

最初有一个 DAL 对象,我的 BO 调用它来获取信息,然后传递给 UI。 然后我开始注意到 UI 中的代码减少了,并且出现了控制器类。 有什么好的推荐。

我目前构建了我的

Public Class OrderDAL

    Private _id Integer
    Private _order as Order

    Public Function GetOrder(id as Integer) as Order

        ...return Order

    End Function

End Class

控制器类(最近实现了这种风格)

Public Class OrderController

    Private Shared _orderDAL as new OrderDAL

    Public Shared Function GetOrder(id) As Order

        Return _orderDAL.GetOrder(id)

    End Function

End Class

然后在我的应用程序中

My app Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        msgbox(OrderController.GetOrder(12345).Customer.Name)

    End Sub


End app

我最初发现使用共享类我不必在需要获取数据时不断创建 DAL 的新实例

Dim _orderDAL as New OrderDal

_orderDAL.GetOrder(1234)

.....

您的看法是什么?

谢谢

Originally there was the DAL object which my BO's called for info and then passed to UI. Then I started noticing reduced code in UI and there were Controller classes. What's the decent recomendation.

I currently structure mine

Public Class OrderDAL

    Private _id Integer
    Private _order as Order

    Public Function GetOrder(id as Integer) as Order

        ...return Order

    End Function

End Class

then I have controller classes (recently implemented this style)

Public Class OrderController

    Private Shared _orderDAL as new OrderDAL

    Public Shared Function GetOrder(id) As Order

        Return _orderDAL.GetOrder(id)

    End Function

End Class

Then in my application

My app Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        msgbox(OrderController.GetOrder(12345).Customer.Name)

    End Sub


End app

I originally found that with the Shared Class I didn't have to keep creating a new instance of the DAL whenever I need to fetch data

Dim _orderDAL as New OrderDal

_orderDAL.GetOrder(1234)

.....

What's your take?

Thanks

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

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

发布评论

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

评论(5

橪书 2024-07-14 03:59:22

我认为这本优秀的书中列出了几种替代方案:企业应用程序架构模式。 您可能感兴趣的一些模式:

I think there are several alternatives listed in this excellent book: Patterns of Enterprise Application Architecture. Some patterns that may be of interest to you:

初雪 2024-07-14 03:59:22

我过去使用过你的解决方案,我面临的唯一问题是“共享”或“静态”方法不支持继承。 当您的应用程序增长时,您可能非常需要支持不同类型的“OrderController”。

理论上,支持不同 OrderController 的既定方法是创建一个工厂:

OrderControllerFactory.ConfiguredOrderController().GetOrder(42);

这里的问题是:“ConfiguredOrderController()”返回什么类型? 因为它必须具有静态“GetOrder(int id)”方法——而继承或接口不支持静态方法。 解决这个问题的方法是不要在 OrderController 类中使用静态方法。

public interface IOrderController
{
    Order GetOrder(int Id)
}

public class OrderController: IOrderController
{
    public Order GetOrder(int Id)
    {}
}

因此

public class OrderControllerFactory()
{
    public IOrderController ConfiguredOrderController()
    {}
}

,对控制器使用非静态方法可能会更好。

I've used your solution in the past, and the only problem I faced is that "Shared" or "static" methods don't support inheritance. When your application grows, you might very well need to support different types of "OrderControllers".

The estabilished way of supporting different OrderControllers would be, in theory, to create a factory:

OrderControllerFactory.ConfiguredOrderController().GetOrder(42);

The problem here is: what type is returned by "ConfiguredOrderController()"? Because it must have the static "GetOrder(int id)" method -- and static methods are not supported by inheritance or interfaces. The way around this is not to use static methods in the OrderController class.

public interface IOrderController
{
    Order GetOrder(int Id)
}

public class OrderController: IOrderController
{
    public Order GetOrder(int Id)
    {}
}

and

public class OrderControllerFactory()
{
    public IOrderController ConfiguredOrderController()
    {}
}

Hence, you will probably be better off by using non-static methods for the controller.

九局 2024-07-14 03:59:22

那么您的应用程序不应该实例化数据访问层的单独版本,因此您可以控制它。 不过,您发布的伪代码确实很难阅读。

问题是,你的数据访问层是什么,有多少? 这将决定你所做的大部分事情。 如果您坚持一个文件,那么我认为您编写的内容很好,如果您需要与系统中的其他控制器共享它,则可以将该项目包装成一个单例(颤抖)。

如果您确实正在执行订单处理并将其持久化回数据库,我个人认为是时候考虑一​​下 ORM 了。 这些包将为您处理 CRUM 方面的问题并减少您必须维护的项目数量。

我的 $.02,一旦我看到更好的例子,我保留修改我的答案的权利。

Well your application shouldn't be instantiating seperate versions of the data acces layer, so you have that under control. The Psuedo code you posted is really hard to read though.

The question is, what is your data access layer, and how much is there? That's going to dictate a good bit of what you do. If your persiting to a file then I think what you have written is fine, and if you need to share it with other controllers in your system, it's possible to wrap the item into a singelton(shudder).

If you really are doing order processing and persisting back to a database, I personaly think it's time to look at an ORM. These packages will handle the CRUM aspects for you and reduce the number of items that you have to maintain.

My $.02, and I reserve the right to revise my answer once I see a better example.

我的鱼塘能养鲲 2024-07-14 03:59:22

我不能谈论 VB 细节,因为我不是 VB 开发人员,但是:

您正在做的是一个行之有效的良好实践,将 GUI/表示层与数据层分离。 在 GUI 事件方法中包含真实的应用程序代码是一种(遗憾的是也是公认的)不好的做法。

您的控制器类类似于桥接模式,如果两个层都能够在对方不知情的情况下改变自己的形态。

前进!

I can't speak to the VB details because I'm not a VB developer, but:

What you are doing is a well-established good practice, separating the GUI/presentation layer from the data layer. Including real application code in GUI event methods is a (sadly also well-established) bad practice.

Your controller class resembles the bridge pattern which is also a good idea if both layers shall be able to change their form without the other knowing about it.

Go ahead!

§对你不离不弃 2024-07-14 03:59:22

这是一个很好的实践——尤其是当您到达控制器需要做的不仅仅是简单委托给底层 DAL 的时候。

Its a good practice -- especially when you get to a point where the Controller will need to do more than simple delegation to the underlying DAL.

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