473,659 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Business/Data design, IDisposable, and using statement?

I trying to determine the best pattern for designing my business and data
layers...

Can the instance of the business object eventually cause memory leaks in
Example 1? If your business class doesn't implement IDisposable and falls
out scope, does it eventually get cleaned up by the GC or should it be set to
NULL?

If it can cause leaks, should the IDisposable always be implemented at a
base class level as a best practice?

If it does implement IDisposable, is wrapping the using statement (Example
2) in a try block to catch any exceptions the proper technique? I see
samples of "using" but not many of how to handle an exception if it occurs in
the using block.

EXAMPLE 1
-------------
MyBizClass myBizClass = null;
try
{
myBizClass = new MyBizClass();
return myBizClass.GetS omeDataFromData Layer();
}
catch
{
throw;
}
finally
{
//Set myBizClass = null???
}

EXAMPLE 2
--------------

try
{
using (MyBizClass myBizClass = new MyBizClass())
{
return myBizClass.GetS omeDataFromData Layer();
}
}
catch
{
throw;
}


Feb 1 '06 #1
3 3489
Dave,

You should use IDisposable only when you have the need for controlling
the lifetime of an object, most likely as the result of using a resource
which can not just be left in an undisposed state (such as a file handle,
unmanaged memory, a database handle, etc, etc).

I also use IDisposable on structures when I want to have deterministic
finalization semantics in a method.

Your class doesn't seem to be doing either, so I would not implement
IDisposable. Also, setting a variable to null (not a field, a variable)
will actually prolong the lifetime of the object in code compiled for
release mode.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Dave" <Da**@discussio ns.microsoft.co m> wrote in message
news:E6******** *************** ***********@mic rosoft.com...
I trying to determine the best pattern for designing my business and data
layers...

Can the instance of the business object eventually cause memory leaks in
Example 1? If your business class doesn't implement IDisposable and falls
out scope, does it eventually get cleaned up by the GC or should it be set
to
NULL?

If it can cause leaks, should the IDisposable always be implemented at a
base class level as a best practice?

If it does implement IDisposable, is wrapping the using statement (Example
2) in a try block to catch any exceptions the proper technique? I see
samples of "using" but not many of how to handle an exception if it occurs
in
the using block.

EXAMPLE 1
-------------
MyBizClass myBizClass = null;
try
{
myBizClass = new MyBizClass();
return myBizClass.GetS omeDataFromData Layer();
}
catch
{
throw;
}
finally
{
//Set myBizClass = null???
}

EXAMPLE 2
--------------

try
{
using (MyBizClass myBizClass = new MyBizClass())
{
return myBizClass.GetS omeDataFromData Layer();
}
}
catch
{
throw;
}

Feb 1 '06 #2
"Dave" <Da**@discussio ns.microsoft.co m> wrote in message news:E6******** *************** ***********@mic rosoft.com...
I trying to determine the best pattern for designing my business and data
layers...
Oki. And it seems like you think you must try... and catch..

Can the instance of the business object eventually cause memory leaks in
Example 1? If your business class doesn't implement IDisposable and falls
out scope, does it eventually get cleaned up by the GC or should it be set to
NULL?
It will be cleaned up by the GC. At some time... In seconds, hour or days...
Why IDisposable is sometimes important. As for sql connections or filehandles.

If you open a file for writing, you can't wait for the GC to (maybe) kick in and remove the filehandle in the destructor (finalizer) of the object. The file will be locked until the stream is collected.
If it can cause leaks, should the IDisposable always be implemented at a
base class level as a best practice?
Your objects typically won't leak. Only if you start allocating resources that must (should) be disposed of after its direct use you should implement IDsposable.

Hence, If you have a class that sports the metod save, and that opens a stream, writes and call .Dispose() you should not implement IDisposable yourself.

But, if you create a class with the methods Open(string filename), Write(string stuff) and Close(), you should implement IDisposable.
EXAMPLE 1
-------------
This does nothing. catching and throwing does what?
And as your ? says in your finally comment. What should it do?

You're doing nothing with your try, catch, finally..
Just rewrite it into:

public SomeData DoStuff()
{
MyBizClass myBizClass = new MyBizClass();
return myBizClass.GetS omeDataFromData Layer();
}
EXAMPLE 2
--------------


You think you have to implement IDisposable. Well you don't.
So don't focus on the caller, let's have a look att the method being called...

it should look something like this:

public SomeData GetSomeDataFrom DataLayer();
{
using (SqlConnection conn = SomeConnectionF actory.Create() )
{
//etc etc
}
}

SqlConnection implements IDisposable, and by using the using syntactic suggar, you have actually done something like this:

public SomeData GetSomeDataFrom DataLayer();
{
Try
{
SqlConnection conn = SomeConnectionF actory.Create() )
//etc etc
}
finally
{
conn.Dispose(); // no matter what, the sql connection will be returned to the pool.
}
}

Hope this makes it clearer for you.

Happy Coding
- Michael S
Feb 1 '06 #3
A simple set of rules for when you need to implement IDisposable is:

1-the common one) If your class owns any object that implements IDisposable
then it must implement IDisposable and Dispose must call Dispose on those
objects.

2-the less common one) If you your class holds any "handle" to a resource
obtained by some interop call then you must implement IDisposable to release
that resource. [You should write a class whose sole purpose it to own that
handle and to wrap the interop calls associated with it.]

3) If some object gives you a handle then see 2 - but this should never
happen because the design sucks.

"Dave" <Da**@discussio ns.microsoft.co m> wrote in message
news:E6******** *************** ***********@mic rosoft.com...
I trying to determine the best pattern for designing my business and data
layers...

Can the instance of the business object eventually cause memory leaks in
Example 1? If your business class doesn't implement IDisposable and falls
out scope, does it eventually get cleaned up by the GC or should it be set
to
NULL?

If it can cause leaks, should the IDisposable always be implemented at a
base class level as a best practice?

If it does implement IDisposable, is wrapping the using statement (Example
2) in a try block to catch any exceptions the proper technique? I see
samples of "using" but not many of how to handle an exception if it occurs
in
the using block.

EXAMPLE 1
-------------
MyBizClass myBizClass = null;
try
{
myBizClass = new MyBizClass();
return myBizClass.GetS omeDataFromData Layer();
}
catch
{
throw;
}
finally
{
//Set myBizClass = null???
}

EXAMPLE 2
--------------

try
{
using (MyBizClass myBizClass = new MyBizClass())
{
return myBizClass.GetS omeDataFromData Layer();
}
}
catch
{
throw;
}

Feb 2 '06 #4

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

Similar topics

5
3133
by: Shibu | last post by:
Hi, I have a situation where I need to convert business objects to a flat table. The reverse is also required. I am using c# and Oracle ODP. I am looking for an easier method to do the below steps. Steps I follows for populating Business Objects is as follows (1) Get a list of records containing various tables joined together from DB using a single PL/SQL (This is a specific requirement, So cannot change the design)
9
6255
by: Hasan O. Zavalsiz | last post by:
Hi , i am trying to figure out which approach is better to use . let me explain the scenario. i am using the "Nortwind" database . in this database i have "Customers " table .The following is the two different ways to handle this table. CASE 1 : create a struct that encaplusates table "Customers" columns public struct structCustomers { public string CustomerID;
5
3182
by: Robert Heuvel | last post by:
Hi, this is what I did: public struct SWaitCursor:IDisposable { public SWaitCursor (int i) { Cursor.Current = Cursors.WaitCursor; } void System.IDisposable.Dispose() { Cursor.Current = Cursors.Default;
18
2850
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For example dsParts.xsd and including that in the data tier. I then will create a class that looks like this Public Class CPart Inherits dsParts
6
1630
by: Nate | last post by:
I am in a slight predicament trying to determine the most efficient and effective way to connect/disconnect from a database within a business object (c# dll). I'm also keeping in mind the concept of connecting late and disconnecting early. Background: - multi-tier application (code-behind uses properties and methods of the business object, the business object handles the data layer) For instance (in an ASPX code-behind file):
4
2028
by: Helge Jensen | last post by:
In C# 2.0 System.IO.Stream is declared as: public class Stream: ..., IDisposable { ... public void Dispose(); public void Dispose(bool); IDisposable.Dispose(); } Which must be a design-blunder, if not a 100-year sleep. It prevents
12
2441
by: Cordell Lawrence \(News Group\) | last post by:
There an ongoing discussion between a colleague and myself about the usefulness of the IDisposable pattern beyond the reclamation of unmanaged resources. The discussion is somewhat lengthy so I will distill it here. The core of his argument started with his statement: "There is no gain in performance, maintainability or otherwise by implementing the Dispose method if unmanaged resources are not involved." I focused particularly on...
25
2765
by: Penelope Dramas | last post by:
Hello, I'm in a front of very serious .net redesign/rewrite of an old VB6 application. I had been asked to make it .NET 2.0 and would like to ask couple of questions regarding data access as this application is heavily data-centric around MSDE database. Would it be better to use custom business objects or extend
0
8428
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
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
8535
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
7360
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
6181
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
4176
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
4338
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
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.