473,657 Members | 2,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static vs Non Static DAL Class(es)

I'm wondering what some of the important considerations are regarding
implementing the DAL as a static class vs creating instances of it as
needed.

I'm writing a .NET 2.0 Windows application (MDI) that will communicate with
a SQL Server; approximately 120 users.

On smaller apps I have tended to implement the DAL as a static class. But
this new app will likely spawn background threads periodically to update
business objects throughout the session. All such threads (likely not more
than one or two at a time) would make use of the DAL.

This is my first foray into multi-threaded apps - so I'm wanting to proceed
with caution and guidance. So any relevant would be appreciated!
Dec 26 '06 #1
10 9088
"Fred Mertz" <A@B.comwrote in message
news:u$******** ******@TK2MSFTN GP04.phx.gbl...
I'm wondering what some of the important considerations are regarding
implementing the DAL as a static class vs creating instances of it as
needed.
I'm really struggling to come up with *any* possible advantages of a
non-static DAL...

My DAL has six methods:

GetDataSet
GetDataReader
GetScalar
ExecuteSQL
ExecuteSQLTrans action
WriteImage

all of which are overridden to support the passing in of a connection string
or reading it from the config file, and either a SQL string or the name of a
stored procedure plus parameters collection.

What else do you need your DAL to do...?
Dec 26 '06 #2
Hi,

I don't know how u plan to implement your DAL but as long as they are
stateless (most probably they are already) you should have no problem with
concurrency.
--
Ignacio Machin
machin AT laceupsolutions com
"Fred Mertz" <A@B.comwrote in message
news:u$******** ******@TK2MSFTN GP04.phx.gbl...
I'm wondering what some of the important considerations are regarding
implementing the DAL as a static class vs creating instances of it as
needed.

I'm writing a .NET 2.0 Windows application (MDI) that will communicate
with a SQL Server; approximately 120 users.

On smaller apps I have tended to implement the DAL as a static class. But
this new app will likely spawn background threads periodically to update
business objects throughout the session. All such threads (likely not more
than one or two at a time) would make use of the DAL.

This is my first foray into multi-threaded apps - so I'm wanting to
proceed with caution and guidance. So any relevant would be appreciated!

Dec 26 '06 #3
Hi Fred,

When using static methods with asynchronous processes you'll have to be sure
that they are thread-safe. If the methods in your static classes don't rely
on any shared state then you won't have to worry about thread-safety in
managed code, at least.

Using instances of your DAL entities is more OO though, and therefore more
flexible. You'll probably be better off using instances instead regardless
of whether thread-safety is an issue. Objects will allow you to handle
entity relationships easily, and if you ever need to expand into a more
robust design pattern then you'll probably need instances of your entities
anyway (to facilitate the polymorphic techniques used by design patterns).
Static classes would just end up being a bottleneck in the development
process.

--
Dave Sexton
http://davesexton.com/blog

"Fred Mertz" <A@B.comwrote in message
news:u$******** ******@TK2MSFTN GP04.phx.gbl...
I'm wondering what some of the important considerations are regarding
implementing the DAL as a static class vs creating instances of it as
needed.

I'm writing a .NET 2.0 Windows application (MDI) that will communicate
with a SQL Server; approximately 120 users.

On smaller apps I have tended to implement the DAL as a static class. But
this new app will likely spawn background threads periodically to update
business objects throughout the session. All such threads (likely not more
than one or two at a time) would make use of the DAL.

This is my first foray into multi-threaded apps - so I'm wanting to
proceed with caution and guidance. So any relevant would be appreciated!

Dec 26 '06 #4


"Mark Rae" <ma**@markNOSPA Mrae.comwrote in message
news:Or******** ******@TK2MSFTN GP02.phx.gbl...
"Fred Mertz" <A@B.comwrote in message
news:u$******** ******@TK2MSFTN GP04.phx.gbl...
>I'm wondering what some of the important considerations are regarding
implementing the DAL as a static class vs creating instances of it as
needed.

I'm really struggling to come up with *any* possible advantages of a
non-static DAL...

My DAL has six methods:

GetDataSet
GetDataReader
GetScalar
ExecuteSQL
ExecuteSQLTrans action
WriteImage

all of which are overridden to support the passing in of a connection
string or reading it from the config file, and either a SQL string or the
name of a stored procedure plus parameters collection.

What else do you need your DAL to do...?
Wow, those methods almost look familiar. You may want to look into the
Microsoft Patterns and Practices Enterprise Library - Data Access
Application Block <pants, tries to breath>.

http://msdn.microsoft.com/practices/.../html/daab.asp

The P&P EL application blocks come with full source code, so you can
modify/add methods to better suit your needs...

HTH,
Mythran
Dec 26 '06 #5
Hi Mark,
I'm really struggling to come up with *any* possible advantages of a
non-static DAL...
I created a DAL for my applications that isn't static. It consists of a
generic class named, "DataAccessCont ext" (with some supporting classes). I
can derive from that class something like SqlDataAccessCo ntext (which I
already have in my library). The DAL uses a custom .NET 2.0 Provider (for
WinForms or ASP.NET) to allow the future addition of other context providers
such as Oracle, which I haven't needed myself yet. In an application I can
then derive a class from SqlDataAccessCo ntext to provide a typed
implementation of my DAL and with the generic arguments specify the typed
DataSet being used for my application so that all of the DAL members are
strong-typed. It supports transactions using those provided by the data
provider or even 2.0 System.Transact ions.

I use it to perform operations against the database as such:

// "Data" is a common namespace that I use
// "data" is an instance of the application's strong-typed DataSet
using (Data.Context dataContext = new Data.Transacted Context(data))
{
dataContext.Add TableMapping("U sers");
dataContext.Fil l("SelectUsers" );
}

--
Dave Sexton
http://davesexton.com/blog
Dec 26 '06 #6


One reason I use non static is

A: Constructor
B. I inherit from an abstract class.
class DataLayerAbstra ct
{
public DataLayerAbstra ct()

public DataLayerAbstra ct(string instanceName)

}
class EmployeeData : DataLayerAbstra ct
{
public EmployeeData : base ()

public EmployeeData (string instanceName) : base (instanceName)

}
class DepartmentData : DataLayerAbstra ct
{
public DepartmentData : base ()

public DepartmentData (string instanceName) : base (instanceName)

}
Something like that. I have a few different ways I could instantiate the
object (mainly, from where do I get the connection string info).

Then I only write that code once, in the abstract class.
............

If I ever have a need, where I might use Access or SqlServer, then I can do
something like this:

class DataLayerAbstra ct
{
public DataLayerAbstra ct()

public DataLayerAbstra ct(string instanceName)

public override DataSet GetAllEmployees ();

}
class EmployeeDataWit hAccess : DataLayerAbstra ct
{
public EmployeeData : base ()

public EmployeeData (string instanceName) : base (instanceName)

public override DataSet GetAllEmployees ()
{
//return a DataSet of employees via Access
}

}

class EmployeeDataWit hSqlServer : DataLayerAbstra ct
{
public EmployeeData : base ()

public EmployeeData (string instanceName) : base (instanceName)

public override DataSet GetAllEmployees ()
{
//return a DataSet of employees via Sql Server
}

}

Then I can write my code to work with the abstract class, and then create a
factory class to return either the sql server or access version.

pubic class MyDatabaseFacto ry
{
private MyDatabaseFacto ry ()
{}//hide the contructor

public static DataLayerAbstra ct GetADataLayerOb ject()
{
bool isAccess = true;// config file? etc?

if (isAccess)
{
return new EmployeeDataWit hAccess ();
}
else
{
return new EmployeeDataWit hSqlServer ();
}
}
}

Forgive my syntax errors, but hopefully you get the idea.
I would use the "data access application block" 2.0 if you're using sql
server ... to ~~~help with and aid your DataLayer object(s).
If you have needs beyond Sql Server, then check the EnterpriseLibra ry.Data
library.
This will do housekeeping for you on almost everything, except properly
closing an IDataReader. You ~~always need to close the IDataReader
yourself, as soon as possible.

I'm not sure if you're creating a helper DAL object OR DAL objects per
entity (as in, EmployeeData, DeptData, stuff like that)

Those are some reasons, each situation is different.
"Fred Mertz" <A@B.comwrote in message
news:u$******** ******@TK2MSFTN GP04.phx.gbl...
I'm wondering what some of the important considerations are regarding
implementing the DAL as a static class vs creating instances of it as
needed.

I'm writing a .NET 2.0 Windows application (MDI) that will communicate
with
a SQL Server; approximately 120 users.

On smaller apps I have tended to implement the DAL as a static class. But
this new app will likely spawn background threads periodically to update
business objects throughout the session. All such threads (likely not more
than one or two at a time) would make use of the DAL.

This is my first foray into multi-threaded apps - so I'm wanting to
proceed
with caution and guidance. So any relevant would be appreciated!


Dec 26 '06 #7
"Mythran" <ki********@hot mail.comwrote in message
news:uq******** ******@TK2MSFTN GP04.phx.gbl...
The P&P EL application blocks come with full source code, so you can
modify/add methods to better suit your needs...
My DAL is based on the Microsoft one...
Dec 26 '06 #8
Instead of re-inventing the wheel....Have you thought about using a
time-tested DAL... I would HIGHLY recommend LLBLGenPro
(http://llblgen.com) as the main developer is a MASTER when it comes to
DAL, and the support along with the number of people actually using it
in real world application makes it a very stable product. I was using
DeKlarit, but found it difficult to use once a project got soo big. I
since found LLBLGenPro and can't say enough about it.
Fred Mertz wrote:
I'm wondering what some of the important considerations are regarding
implementing the DAL as a static class vs creating instances of it as
needed.

I'm writing a .NET 2.0 Windows application (MDI) that will communicate with
a SQL Server; approximately 120 users.

On smaller apps I have tended to implement the DAL as a static class. But
this new app will likely spawn background threads periodically to update
business objects throughout the session. All such threads (likely not more
than one or two at a time) would make use of the DAL.

This is my first foray into multi-threaded apps - so I'm wanting to proceed
with caution and guidance. So any relevant would be appreciated!
Dec 27 '06 #9
Mark Rae <ma**@markNOSPA Mrae.comwrote:
I'm wondering what some of the important considerations are regarding
implementing the DAL as a static class vs creating instances of it as
needed.

I'm really struggling to come up with *any* possible advantages of a
non-static DAL...
By making the DAL non-static, it can implement an interface which the
BOL then talks to after being configured with an implementation. For
production code this would be the normal DAL, but for unit test
purposes you can use mocks etc.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 27 '06 #10

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

Similar topics

3
1326
by: alexhong2001 | last post by:
When design a class, should always make it "derivable" as a base class? Is there really a situation that the designed class not "derivable"? When should make a member "protected"? Only when allowing the derived class(es) directly access it? Should destructor always be virtual? Thanks for your comments!
3
2340
by: Teis Draiby | last post by:
I want to write a base class that includes a member function that creates an instance of a derrived class and returns a pointer to it. Problem: The derived class definition has to follow the base class definition. Therefore I can't specify the return type of the function, that returns a pointer to the derived class, because the derived class is not yet known at that point. Hov can I solve this problem? In a nice manner :)
4
1522
by: Tomas Ukkonen | last post by:
Hi I'm currently trying to make my old code to compile with new gcc 3.4.0. It compiles correctly in 3.3.3 so I'm not sure if this is compiler bug or not. Code very close to a example below fails to compile because gcc 3.4.0 complains: "error: expected a type got 'Y<T>::particle'"
12
1885
by: James Brown | last post by:
Hi all, Having problems designing a template-class. I'll describe my scenario first then show what I've come up with so far: Need a class to provide pointer/array-like access to an area of physical memory located on a piece of custom hardware - this memory is only accessible using machine specific i/o so I want to hide all this in a class. I'm imagining
6
5872
by: eselk | last post by:
If I have: class A { public: class some_base_class **Obj; }; And I would like to redefine "Obj" in a class derived from class A, something like this maybe:
8
4671
by: Stanislav Simicek | last post by:
Hello, I've several C++ classes that must be ported to C#. Is there any construction in C# similar to C++: class A : protected B { ... }
11
3041
by: Aflj | last post by:
This code won't compile (two compilers tried, gcc and VC++, both of recent versions, but I don't remember them exactly): class C1 { public: void M1(int i) {} }; class C2: public C1
5
2352
by: Jack | last post by:
How to quickly build an HTML parser with C#? Does anything like "HTMLParser" exist? Thanks in advance Jack
3
2660
by: ramsisiv | last post by:
I know that it is possible but, if the abstract class has a virtual function print, that prints info about the objekt and the derived classes each have theire own version of print, will a print funktion in the ADT that prints the info in the current objekt by using its print function use the print function from the superclass or from the derived class(es)? I'm not shore if virtual helps me here sense the nodes in the ADT take a pointer to an...
0
8829
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8508
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7341
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6172
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5633
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4164
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2733
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1627
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.