类中的共享方法 [英] Shared Methods in a Class

查看:78
本文介绍了类中的共享方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为SQL Server后端构建数据访问层。

我的类中有许多方法。

构造函数接受连接字符串。


我目前必须实例化一个对象才能使用我的一种方法。

所以我最终在我的应用程序中做了很多。


如果我将所有方法更改为共享,这意味着我可以使用

而不实例化我的DAL类的实例吗? />

如果是这样,是否意味着构造函数不运行?

如果是这样,我的类方法将如何知道ConnectionString值???


有人可以概述解决这个问题的最佳方法吗?

谢谢!


-

Joe Fallon

I am trying to build a Data Access Layer for a SQL Server back end.
My class has a number of methods in it.
The constructor takes a connection string.

I currently have to instantiate an object in order to use one of my methods.
So I end up doing this quite a lot in my app.

If I change all of the methods to be Shared does that mean I can just use
them without instantiaing an instance of my DAL class?

If so, does that mean the constructor does not run?
If so, how will my class methods know the ConnectionString value???

Can someone please outline the best way to resolve this?
Thanks!

--
Joe Fallon

推荐答案

2003年10月4日星期六01:57:50 -0400,Joe Fallon

< jf ****** @ nospamtwcny.rr.com>写道:
On Sat, 4 Oct 2003 01:57:50 -0400, "Joe Fallon"
<jf******@nospamtwcny.rr.com> wrote:
我正在尝试为SQL Server后端构建数据访问层。
我的类中有许多方法。
构造函数取一个连接字符串。

我目前必须实例化一个对象才能使用我的一种方法。
所以我最终在我的应用程序中做了很多这样的事情。

如果我将所有方法改为共享,那意味着我可以在没有实例化我的DAL类实例的情况下使用它们吗?


是的。

如果是这样,这是否意味着构造函数不运行?


是的 - 但是你可以强制代码在调用任何方法之前运行(如果

那就是你所追求的行为)。

如果是这样,我的类方法将如何知道ConnectionString值???


是否有默认的连接字符串?如果是这样,请将其放在您班级的共享

属性中,并在其他地方使用该属性。如果

没有默认连接字符串,你可以使用另一个模型:


1)强制开发人员实例化一个单例(接受

连接字符串作为参数)。

2)单例将其实例(Me)存储在共享属性中。

3)DAL使用共享属性。





公共类MyDatabase

私有共享mInstance为MyDatabase = Nothing

私有mConnectionString为字符串


私有子新(oConnectionString为字符串)

mConnectionString = oConnectionString

End Sub


''这个方法由BLL使用

公共共享函数GetInstance(oConnectionString As String)_

作为MyDatabase

如果mInstance什么都没有那么

mInstance =新的MyDatabase(oConnectionString)

结束功能

返回mInstance

结束功能


''这个方法由DAL使用

朋友共享函数GetCurrentInstance()As MyDatabase

返回mInstance

结束功能

结束类

BLL开发人员将拥有一个全局,例如:


公共gDatabase作为MyDatabase = MyDatabase.GetInstance(

Config.ConnectionString)

有人可以概述解决此问题的最佳方法吗?
谢谢!
I am trying to build a Data Access Layer for a SQL Server back end.
My class has a number of methods in it.
The constructor takes a connection string.

I currently have to instantiate an object in order to use one of my methods.
So I end up doing this quite a lot in my app.

If I change all of the methods to be Shared does that mean I can just use
them without instantiaing an instance of my DAL class?
Yes.

If so, does that mean the constructor does not run?
Yes - but you can force code to run before any method is called (if
that''s the behaviour you''re after).
If so, how will my class methods know the ConnectionString value???
Is there a default connection string? If so, put it in a shared
property of your class and use the property everywhere else. If there
is no default connection string, you can work along another model:

1) Force the developer to instantiate a singleton (which accepts the
connection string as a parameter).
2) The singleton stores its instance (Me) in a shared property.
3) DAL makes use of the shared property.

i.e.

Public Class MyDatabase
Private Shared mInstance As MyDatabase = Nothing
Private mConnectionString As String

Private Sub New(oConnectionString As String)
mConnectionString = oConnectionString
End Sub

'' This method is used by the BLL

Public Shared Function GetInstance(oConnectionString As String) _
As MyDatabase
If mInstance Is Nothing Then
mInstance = New MyDatabase( oConnectionString )
End Function
Return mInstance
End Function

'' This method is used by the DAL

Friend Shared Function GetCurrentInstance() As MyDatabase
Return mInstance
End Function
End Class
The BLL developer would then have a single global, something like:

Public gDatabase As MyDatabase = MyDatabase.GetInstance(
Config.ConnectionString )

Can someone please outline the best way to resolve this?
Thanks!




实现DAL的方法有很多种。我想你正试图做正确的事情,因为你试图让界面变得简单

,对吧?


我更喜欢的方法是让一个对象代表一个Schema。

Schema对象知道如何与Factory对象通信。工厂

对象知道如何创建(执行SQL),更新,删除&找到

DatabaseObject对象。并且DatabaseObject有一个私有成员

表示它与之关联的模式(因此它可以提供默认的

Save()和Delete()方法,并与其他工厂通信)。

从这个模型中,你可以看出Factory对象(SQL Server,Oracle,Access ...)中唯一依赖于驱动程序的部分是

。实现

IDatabaseObject,IFactory和ISchema接口允许您交换

数据库类型。你可能还想花五分钟时间专门化一下
a DataView(实现相关的IObjectCollection)来包装你的数据库对象。


Rgds,



There are many ways to implement a DAL. I guess you''re trying to do
the right thing in that you are trying to make the interface as simple
as possible, right?

The way I prefer is to have an object to represent a Schema. The
Schema object knows how to communicate with Factory objects. Factory
objects know how to create (execute SQL), update, delete & find
DatabaseObject objects. And a DatabaseObject has a private member
indicating the schema it is associated with (so it can provide default
Save() and Delete() methods, and communicate with other factories).
From this model, you can tell that the only driver-dependent parts are
in the Factory objects (SQL Server, Oracle, Access...). Implementing
IDatabaseObject, IFactory and ISchema interfaces allows you to swap
database type. You might also want to spend five minutes specialising
a DataView (which implements the associated IObjectCollection) to wrap
your DatabaseObject.

Rgds,


你好Joe,


共享成员属于Class以及实例

类,所以无需创建实例即可访问它们。


您仍然需要使用类名来引用这些方法。类

可以拥有所谓的类构造函数。这与

实例相同,不同之处在于,对于类本身,它具有Shared关键字。

此共享New()将在等级是第一个

访问。


公共类SharedClass

子共享新

MsgBox(& ;嘿 - 优雅或什么!!")

结束子

公共共享子DoSomething

结束子

结束课


使用中:

''如果这是第一次使用SharedClass,将调用New

SharedClass.DoSomething


在VB.NET中,您甚至可以通过

将方法放入模块中来消除共享类的需要。一个模块是语法糖 - 在

场景后面它是一个共享类。因此,您不需要共享

关键字。您也不需要通过给出模块名称来使用这些方法(除非

与其他东西发生冲突,例如另一个模块,也定义了

该名称)。


公共模块东西

Sub New

MsgBox(嘿 - 新东西!!)

End Sub


Public Sub DoSomething

End Sub

结束模块


使用中:

DoSomething

无论您使用共享类还是模块都取决于您。您可以将它作为最佳实践问题构成

并获得半打意见。 ;-)


问候,

Fergus
Hi Joe,

Shared members belong to the Class as well as to the instances of the
Class, so they are accessible without needing to create an instance.

You will still need to use the class name to refer to the methods. Classes
can have what is called a class constructor. This is the same as for an
instance except that, being for the class itself, it has the Shared keyword.
This Shared New() will be automatically called when the Class is first
accessed.

Public Class SharedClass
Sub Shared New
MsgBox ("Hey - Classy or what!!")
End Sub
Public Shared Sub DoSomething
End Sub
End Class

In use:
''Will call New if this is the first use of SharedClass
SharedClass.DoSomething

In VB.NET you can eliminate even the need to have a Shared Class by
putting the methods into a Module. A Module is syntactic sugar - behind the
scenes it is a Shared Class. Because of this you don''t need the Shared
keyword. Nor do you need to use the methods by giving the Module name (unless
there is a clash with something else, eg another Module, that also defines
that name).

Public Module Stuff
Sub New
MsgBox ("Hey - New Stuff!!")
End Sub

Public Sub DoSomething
End Sub
End Module

In use:
DoSomething
Whether you use a Shared Class or a Module is up to you. You could pose it
as a Best Practice question and get half-a-dozen opinions. ;-)

Regards,
Fergus


" Joe Fallon" < JF ****** @ nospamtwcny.rr.com>在消息新闻中写道:< Oz ************** @ TK2MSFTNGP10.phx.gbl> ...
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message news:<Oz**************@TK2MSFTNGP10.phx.gbl>...
我正在尝试构建一个数据访问层一个SQL Server后端。
我的类中有许多方法。
构造函数接受一个连接字符串。


这样你的业务流程就不是真正的数据独立。

因为你需要在调用
$ b时指定连接字符串$ b DAL。

我目前必须实例化一个对象才能使用我的一种方法。
所以我最终在我的应用程序中做了很多这样的事情。


你可能想要使用你的业务类的全局成员,你可以在他们的构造函数中实例化。

如果我改变所有要共享的方法是否意味着我可以在没有实例化我的DAL类的实例的情况下使用它们?


正确。

如果是这样,这是否意味着构造函数不运行?


我想是的,从未测试过。但如果这确实是正确的,那将是合乎逻辑的。

如果是这样,我的类方法将如何知道ConnectionString值???
I am trying to build a Data Access Layer for a SQL Server back end.
My class has a number of methods in it.
The constructor takes a connection string.
That way your business processes are not really data independend.
Since you need to specify the connection string in the calls to your
DAL.

I currently have to instantiate an object in order to use one of my methods.
So I end up doing this quite a lot in my app.
You might want to use global members of your business classes that you
instantiate in their constructors.

If I change all of the methods to be Shared does that mean I can just use
them without instantiaing an instance of my DAL class?
Correct.

If so, does that mean the constructor does not run?
I guess so, never tested it. But it would be logical if this is
correct indeed.
If so, how will my class methods know the ConnectionString value???




他们不会。最好的方法是,如果您的数据访问层(对应用程序中的数据负责的是
)将是唯一一个知道如何连接到数据库的
层。这样你就可以明确

职责,这是多层开发的目标之一。


如果你想要坚持从你的业务/演示文稿传递

课程,你必须通过每次共享电话传递它。

祝你好运


Freek Versteijn


PS:一个好看的东西,如果你对多层设计更有经验,那将是DAC2组件在
www.codeproject.com/useritems/dac2.asp


这篇关于类中的共享方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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