是一个具有共享字段和函数的基类,良好的设计

发布于 2024-08-29 22:00:44 字数 782 浏览 4 评论 0原文

我有一个具有共享字段和函数的 BaseDataClass

    Protected Shared dbase as SqlDatabase
    Protected Shared dbCommand as DBCommand
    ...
    //also have a sync object used by the derived classes for Synclock'ing
    Protected Shared ReadOnly syncObj As Object = New Object()

    Protected Shared Sub Init() //initializes fields, sets connections 
    Protected Shared Sub CleanAll() //closes connections, disposes, etc.

我有几个派生自该基类的类。派生类具有所有可以直接从 BLL 调用的共享函数,无需实例化。
这些派生类中的函数调用基类 Init(),调用其特定的存储过程,调用基类 CleanAll(),然后返回结果。

因此,如果我有 5 个派生类,每个派生类有 10 个函数,总共有 50 个可能的函数调用,因为它们都是共享的,所以 CLR 一次只调用一个函数,对吗?所有调用都会排队等待,直到每个 Shared 函数完成。

是否有更好的设计,在 DAL 中拥有 Shared 函数并且仍然具有基类函数?或者既然我有一个基类,那么转向 DAL 中的实例方法是否更好?

I've got a BaseDataClass with shared fields and functions

    Protected Shared dbase as SqlDatabase
    Protected Shared dbCommand as DBCommand
    ...
    //also have a sync object used by the derived classes for Synclock'ing
    Protected Shared ReadOnly syncObj As Object = New Object()

    Protected Shared Sub Init() //initializes fields, sets connections 
    Protected Shared Sub CleanAll() //closes connections, disposes, etc.

I have several classes that derive from this base class. The derived classes have all Shared functions that can be called directly from the BLL with no instantiation.
The functions in these derived classes call the base Init(), call their specific stored procs, call the base CleanAll() and then return the results.

So if I have 5 derived classes with 10 functions each, totaling 50 possible function calls, since they are all Shared, the CLR only calls one at a time, right? All calls are queued to wait until each Shared function completes.

Is there a better design with having Shared functions in your DAL and still have base class functions? Or since I have a base class, is it better to move towards instance methods within the DAL?

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

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

发布评论

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

评论(1

沫离伤花 2024-09-05 22:00:44

在这种特殊情况下,我会说不。这不是一个好的设计。为了使其正常工作,每个类上的每个方法调用首先必须获取 syncObj 上的锁,以免由于多个线程竞争同一资源而破坏状态。忽略由此可能产生的潜在性能影响,您将无法让派生类独立工作,因为它们都使用相同的“东西”。

在这种情况下,我绝对建议采用实例方法。如果您仍然希望单个类使用相同的 SqlConnection 或其他内容,那么可以使用依赖注入将其传递到该类中。

In this particular case I would say no. This is not a good design. To even get this to work correctly every method call on every class would first have to acquire a lock on syncObj so as not to corrupt state due multiple threads competing for the same resource. Ignoring the potential performance impacts that may arise because of that, you would not be able to get your derived classes to work independently because they are all using the same "stuff".

I definitely recommend pushing towards instance methods in this case. If you still want your individual class using the same SqlConnection or whatever then pass it into the class using dependency injection.

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