473,324 Members | 1,856 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,324 software developers and data experts.

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.GetSomeDataFromDataLayer();
}
catch
{
throw;
}
finally
{
//Set myBizClass = null???
}

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

try
{
using (MyBizClass myBizClass = new MyBizClass())
{
return myBizClass.GetSomeDataFromDataLayer();
}
}
catch
{
throw;
}


Feb 1 '06 #1
3 3470
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.com

"Dave" <Da**@discussions.microsoft.com> wrote in message
news:E6**********************************@microsof t.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.GetSomeDataFromDataLayer();
}
catch
{
throw;
}
finally
{
//Set myBizClass = null???
}

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

try
{
using (MyBizClass myBizClass = new MyBizClass())
{
return myBizClass.GetSomeDataFromDataLayer();
}
}
catch
{
throw;
}

Feb 1 '06 #2
"Dave" <Da**@discussions.microsoft.com> wrote in message news:E6**********************************@microsof t.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.GetSomeDataFromDataLayer();
}
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 GetSomeDataFromDataLayer();
{
using (SqlConnection conn = SomeConnectionFactory.Create())
{
//etc etc
}
}

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

public SomeData GetSomeDataFromDataLayer();
{
Try
{
SqlConnection conn = SomeConnectionFactory.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**@discussions.microsoft.com> wrote in message
news:E6**********************************@microsof t.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.GetSomeDataFromDataLayer();
}
catch
{
throw;
}
finally
{
//Set myBizClass = null???
}

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

try
{
using (MyBizClass myBizClass = new MyBizClass())
{
return myBizClass.GetSomeDataFromDataLayer();
}
}
catch
{
throw;
}

Feb 2 '06 #4

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

Similar topics

5
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...
9
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...
5
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 =...
18
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...
6
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...
4
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...
12
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...
25
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.