是一个具有共享字段和函数的基类,良好的设计
我有一个具有共享字段和函数的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种特殊情况下,我会说不。这不是一个好的设计。为了使其正常工作,每个类上的每个方法调用首先必须获取
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.