473,383 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Static v Instance in DAL

I am slightly confused regarding when to use an instance method and when to
use a static method, particularly in the context of a DAL.

I have basically got a class which has methods for CRUD, an object passed
into the method an I return an object representing the outcome of the
operation or in the casse of a search a Data Transfer Object. Each of my
methods are static within an abstract Data Access Class, but I could of
course instantiate an object and use it's methods. In a couple of teching
examples I have seen instantiation of the Data Access Object.

Am I using static methods appropriately? Where is the line drawn between
instantiating an object an using it's methods and using an abstract object
to perform static operations?

Many thanks,
Richard
Oct 25 '07 #1
6 2102
Generally speaking, utilty methods are candidates for being made static. For
example, if you look at the DAAB v2 "SqlHelper" class, everything is static.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"RichB" wrote:
I am slightly confused regarding when to use an instance method and when to
use a static method, particularly in the context of a DAL.

I have basically got a class which has methods for CRUD, an object passed
into the method an I return an object representing the outcome of the
operation or in the casse of a search a Data Transfer Object. Each of my
methods are static within an abstract Data Access Class, but I could of
course instantiate an object and use it's methods. In a couple of teching
examples I have seen instantiation of the Data Access Object.

Am I using static methods appropriately? Where is the line drawn between
instantiating an object an using it's methods and using an abstract object
to perform static operations?

Many thanks,
Richard
Oct 25 '07 #2
OP seems to think he can only have static methods in an abstract class. Of
course, that is not the case and I am thinking he probably doesn't need nor
want his class with static methods to be abstract! Not sure what to point him
to for reference material.
--
Thom
"Peter Bromberg [C# MVP]" wrote:
Generally speaking, utilty methods are candidates for being made static. For
example, if you look at the DAAB v2 "SqlHelper" class, everything is static.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"RichB" wrote:
I am slightly confused regarding when to use an instance method and when to
use a static method, particularly in the context of a DAL.

I have basically got a class which has methods for CRUD, an object passed
into the method an I return an object representing the outcome of the
operation or in the casse of a search a Data Transfer Object. Each of my
methods are static within an abstract Data Access Class, but I could of
course instantiate an object and use it's methods. In a couple of teching
examples I have seen instantiation of the Data Access Object.

Am I using static methods appropriately? Where is the line drawn between
instantiating an object an using it's methods and using an abstract object
to perform static operations?

Many thanks,
Richard

Oct 25 '07 #3
RichB,

You do not need to instantiate a class nor use an abstract class for static
methods. No matter what class a static method is in you can reference/call it
via <classname>.<methodname>(<any paramaters>). Hope this helps.
--
Thom
"RichB" wrote:
I am slightly confused regarding when to use an instance method and when to
use a static method, particularly in the context of a DAL.

I have basically got a class which has methods for CRUD, an object passed
into the method an I return an object representing the outcome of the
operation or in the casse of a search a Data Transfer Object. Each of my
methods are static within an abstract Data Access Class, but I could of
course instantiate an object and use it's methods. In a couple of teching
examples I have seen instantiation of the Data Access Object.

Am I using static methods appropriately? Where is the line drawn between
instantiating an object an using it's methods and using an abstract object
to perform static operations?

Many thanks,
Richard
Oct 25 '07 #4

One reason I got the non-static route is because I have 2 or 3 ways I might
use an alternate connection string.
public abstract class DataLayerBase
{

private string _instanceName = string.Empty;

public DataLayerBase()
{ }

public DataLayerBase(string instanceName)
{
this._instanceName = instanceName;
}

protected Database GetDatabase()
{
Database returnDb = null;
if (this._instanceName.Length 0)
{
returnDb =
DatabaseFactory.CreateDatabase(this._instanceName) ;
}
else
{
returnDb = DatabaseFactory.CreateDatabase();
}
return returnDb;
}
}

public class ZebraData : DataLayerBase // all DAL objects inherit from
DataLayerBase
{
//Procedures
private readonly string PROC_ZEBRA_GET_ALL =
"[dbo].[uspZebraGetAll]";

public ZebraData() : base()
{
}

public ZebraData(string instanceName) : base(instanceName)
{
//use a named connection string
}

public IDataReader ZebraGetAll()
{
IDataReader idr = null;
try
{
Database db = base.GetDatabase();
DbCommand dbc =
db.GetStoredProcCommand(this.PROC_ZEBRA_GET_ALL);
idr = db.ExecuteReader (dbc);
return idr;
}
finally
{
}
}
}
So I can use the "defaultDatabase", when I call the empty constructor, or
use one of the named connection strings when calling the constuctor with one
string argument.

Obviously, you can do this via overloaded static methods as well. I just
chose the instantiated route.
I actually have a third overload, (not shown), because I created this small
"DatabaseCreationArgs", which allow you to set up parameters like
"ExcelFileName", and give you a well formed Excel connection string.

Aka, I have a third constructor...(not seen).
Obviously Im using the EnterpriseLibrary.Data to do my dataaccess as well.

Anyway, just giving you a food for thought about this.

I often debate which to do, but I went with this one. Aka, I think about
just using static methods, and the overloads, but I also want clear intent
for maintenance reasons

"RichB" <ri***@community.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>I am slightly confused regarding when to use an instance method and when to
use a static method, particularly in the context of a DAL.

I have basically got a class which has methods for CRUD, an object passed
into the method an I return an object representing the outcome of the
operation or in the casse of a search a Data Transfer Object. Each of my
methods are static within an abstract Data Access Class, but I could of
course instantiate an object and use it's methods. In a couple of teching
examples I have seen instantiation of the Data Access Object.

Am I using static methods appropriately? Where is the line drawn between
instantiating an object an using it's methods and using an abstract object
to perform static operations?

Many thanks,
Richard

Oct 25 '07 #5
One major advantage of not using static methods is that it allows you to
replace/stub out the functionality for unit testing without a database.
Or you can use different DAL implementations for Sql Server, Oracle, XML etc.
Oct 29 '07 #6

Another good point.

In fact, take a look here:
http://groups.google.com/group/micro...c9b0924188effa

"Niels Ull" <ni******@hotmail.comwrote in message
news:97**************************@msnews.microsoft .com...
One major advantage of not using static methods is that it allows you to
replace/stub out the functionality for unit testing without a database.
Or you can use different DAL implementations for Sql Server, Oracle, XML
etc.


Oct 31 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
2
by: superseed | last post by:
Hi, I'm pretty new to C#, and I'm quite stuck on the following problem. I would like to add to my application a Windows.Form (singleton) on which I could display a message of one of the...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: nytimescnn | last post by:
I've read some discuession about lock() for thread-safe. I am wondering what will be the differce between below two code segment? Code 1: class A { private static Object padlock = new...
12
by: chandu | last post by:
hello, i want to know usage of static methods in a class. is it advantageous or disadvantage to use more static methods in a class. thank u
14
by: Jordan Marr | last post by:
I have the following class: class ProvisionCollection { ... private int m_VarianceCount; public int VarianceCount { get { return m_VarianceCount; }
13
by: learning | last post by:
Hi I have a static class written by other team which encapsulates a database instance. but I need to extend it to incldue other things. I know that C# static class is sealed and can;t be inherited...
4
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
5
by: Andy B | last post by:
I have a class that I want to make static but it uses some objects that are instance objects. I keep getting a compiler error saying something about using instance objects in a static class or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.