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 14 6286
On Sat, 4 Oct 2003 01:57:50 -0400, "Joe Fallon"
<jf******@nospamtwcny.rr.com> wrote: 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!
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,
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> wrote in message news:<Oz**************@TK2MSFTNGP10.phx.gbl>... 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???
They won't. The best way would be if your data access layer (which is
responsible for the data in your application) would be the only layer
to know how to connect to the DB. That way you keep the
responsibilities clear, which is one of the objectives of multitier
development.
If you want to stick with passing it from your business/presentation
classes, you will have to pass it with every Shared call.
Good luck
Freek Versteijn
PS: a nice thing to look at, if you are a bit more experienced with
the multitier design, would be the DAC2 component at www.codeproject.com/useritems/dac2.asp
Hi Freek,
|| > 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.
Lol, There's also a logic argument for the opposite case. ;-)
Shared Classes and Modules can both have their own constructors. See my
other post in this thread.
Regards,
Fergus
Hi Freek,
|| > If so, does that mean the constructor does not run?
||
|| Yes ...
Shared Classes and Modules can both have their own constructors. See my
other post in this thread.
Regards,
Fergus
On Sat, 4 Oct 2003 12:56:20 +0100, "Fergus Cooney"
<fi******@tesco.net> wrote: Hi Freek,
|| > If so, does that mean the constructor does not run? || || Yes ...
Shared Classes and Modules can both have their own constructors. See my other post in this thread.
What's a "Shared Class"?
On Sat, 04 Oct 2003 17:07:50 +0100, _Andy_ <wi******@nospamthanks.gov>
wrote: On Sat, 4 Oct 2003 12:56:20 +0100, "Fergus Cooney" <fi******@tesco.net> wrote:
Hi Freek,
|| > If so, does that mean the constructor does not run? || || Yes ...
Shared Classes and Modules can both have their own constructors. See my other post in this thread.
What's a "Shared Class"?
By that, I mean...
Public Class God
Shared Sub New()
MsgBox("Not yet")
End Sub
Public Shared Sub DoYouExist()
End Sub
End Class
What the hell does "Shared Sub New" actually mean? Constructing an
object that doesn't exist? Yet? Jeez... it's such a horrible
construct... ;)
Rgds,
Hi Andy (sorry about your name in the last reply)
'Shared' - I prefer the C# keyword which is 'static' - meaning that it's
there once and for all. Also in C# the name of the class is used rather than
New. Lots of things seem to make better sense from the C# perspective. I
recommend learning it. ;-)
Shared New means 'get your act together class, I need you'.
Consider the following class:
Public Class Heaven
Private Shared ohGod As New God
Private Shared Jeez As New SonOf (God)
Private Shared alAngels As New ArrayList
Private Shared alSouls As New ArrayList
Private Shared GatesOfHeaven = New Gates (Not Bill)
Shared Sub New
alAngels.Add (New Angel (Michael, Angels.Arch))
alAngels.Add (New Angel (Gabriel, Angels.Normal))
GatesOfHeaven.Open
End Sub
Private Sub New 'There can be only one.
End Sub
Public Shared Function GoToHeaven (oPerson)
If oPerson.GoodDeeds < Enough Then
Throw New MiserableSinnerException
End If
alSouls.Add (oPerson.Soul)
oPerson.Dispose
End Sub
End Class
God and the Angels have to exist somewhere, for although Heaven cannot be
instantiated, it has members - it occupies memory. Thus the class Heaven is
actually an object in that sense.
Heaven exists as a potential at the start of the application and will
remain thus until the very end unless someone dies and tries to GoToHeaven.
Then the class will be loaded, the member fields will be created and
initialised and the construction of Heaven will take place. Not until the
GatesOfHeaven are finally opened will the bounty of Heaven be available to the
Mortal Masses and the DearDeparted may be finally Judged.
Regards,
Fergus
On Sat, 4 Oct 2003 18:48:08 +0100, "Fergus Cooney"
<fi******@tesco.net> wrote: God and the Angels have to exist somewhere, for although Heaven cannot be instantiated, it has members - it occupies memory. Thus the class Heaven is actually an object in that sense.
Heaven exists as a potential at the start of the application and will remain thus until the very end unless someone dies and tries to GoToHeaven. Then the class will be loaded, the member fields will be created and initialised and the construction of Heaven will take place. Not until the GatesOfHeaven are finally opened will the bounty of Heaven be available to the Mortal Masses and the DearDeparted may be finally Judged.
LOL!
So just to get my facts right... a "shared class" in VB terminology is
the same as a basic singleton, but without the need to
"GetInstance()"? - I come from a SmallTalk/C++/Java OO backgound, and
it just looks funny.
Hi Andy,
Ahah! - you've already been to the C-side.
|| a "shared class" in VB terminology is the same as a basic singleton,
I don't know if it's official VB terminology - I just used it. But, yes,
it's VB's version of the singleton (a word I only learnt recently and thought
what's all the fuss over these - it's a static class!!)
I've no idea what GetInstance() is. I gave C++ a miss (I didn't like it
compared to C. >> and ::, yuk, put me right off) and I've skipped Java in
favour of C#.
Regards,
Fergus
Thank you all for the enlightenment.
I especially liked the Heavenly class example! <g>
--
Joe Fallon
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:uW**************@tk2msftngp13.phx.gbl... 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
> Lol, There's also a logic argument for the opposite case. ;-)
Might be, depending on your defenition of constructor. Mine would be a
'method that is called directly after an object of the class has been
constructed'. In that case your scenario does not fit.
Shared Classes and Modules can both have their own constructors. See my other post in this thread.
Could be..!
Freek Versteijn
Hi Freek,
|| .. depending on your defenition of constructor. Mine would
|| be a 'method that is called directly after an object of the class
|| has been constructed'.
Yes, I can go with that. It applies to both objects of a class (ie.
instances) and the class object (ie. the class <itself>). As soon as the class
object has been loaded and its fields initialised (ie. created), its
contructor is called.
|| .. depending on your defenition of constructor. Mine would
|| be a 'method that is called directly after an object of the class
|| has been constructed'.
I know what you mean, of course, but I would be more likely to say:
A constructor is a method that is called directly after an object of
the class
has been <created>.
because, to some people, it would sound strange that a constructor can be
called after the object is <constructed>.
|| In that case your scenario does not fit.
Somehow I can't see where we disagree. ;-)
Regards,
Fergus
First there are static/shared constructors in .NET. They execute the first
time the class is accessed; however, you cannot pass parameters in since it
is called automatically.
What you could try to do is to create a factory class that has a shared
method that returns your DAL object (something like CreateDAL()). This
support class would be able to hold an instance of your DAL class. The
first time it is called, the instance would be created. Subsequent calls
just returns the object itself. The only overhead is the call to the
method. Where the support class gets the connectionstring is up to you.
You would hardcode it in the class or get it from a config file, or
whatever.
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:Oz**************@TK2MSFTNGP10.phx.gbl... 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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John Brock |
last post by:
I have a base class with several derived classes (I'm writing in
VB.NET). I want each derived class to have a unique class ID (a
String), and I want the derived classes to inherit from the base...
|
by: Mark Kamoski |
last post by:
Hi Everyone--
Please help.
What are the implications, (in terms of memory, application footprint,
resource use, threading, and so forth), of using Shared methods?
These Shared classes raise...
|
by: Joe Fallon |
last post by:
1. I have a Base class that has a certain amount of functionality.
2. Then I have a CodeSmith generated class that inherits from the Base class
and adds functionality.
3. Since I want to be able...
|
by: Erik Cruz |
last post by:
Hello!
I have read some threads discussing the fact that a module is in reality a
shared class. If I try to create a Public Shared Class in vb.net I receive a
compile error. Why? If I can't...
|
by: gemel |
last post by:
I have been reading sime material in .NET that throws some doubt on my
understanding of shared procedures.
With regard to object programming I assumed that variables declared
within a class were...
|
by: Elephant |
last post by:
Hello, question,
I want to make a COM-compatible .NET DLL (Visual Basic). For this I need an
interface. The DLL has a class that must contain methods that can be used
without making an instance...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |