473,473 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Best Practice for using LINQ object within class

All,

I have a class describing various actions to take against a LINQ to SQL
datasource. What are the pros/cons of instantiating the LINQ object either
in the root of the class (for lack of a better description) or within each
of the methods? I am naturally drawn to instantiating the LINQ object in the
root but some examples I've seen preffer to keep this local to the methods.
Is there a compelling reason for this? It seems that garbage collection
would work the same for either approach but with less overhead instantiating
in the 'root' (at least to my relatively novice eye).
//instantiate in 'root' of class - this approach feels less wasteful
public class myDB
{
private myLINQObject db = new myLINQObject();

public void action1(){for i in db.tbl1...}
public void action2(){for i in db.tbl2...}
}

//instantiate in methods - seems like a lot of repetition when I know the
object will be needed anyway
public class myDB
{
public void action1()
{myLINQObject db = new myLINQObject();
for i in db.tbl1...}

public void action2(){
myLINQObject db = new myLINQObject();
for i in db.tbl2...}
}
Many thanks :)
n

Sep 3 '08 #1
2 1721
if you keep the linq object as a private field, when is dispose called (you
forgot in your sample code)? did you implement IDispose in you db class and
call dispose on the linq object? you are moving the responsibility to the
caller to call dispose on the dbclass. are the undisposed objects (unmanaged
memory) being keep too long? better to release resources as soon as possible.

// dispose as soon as possible:

public void action1()
{
using(myLINQObject db = new myLINQObject())
{
for i in db.tbl1...
}
}
-- bruce (sqlwork.com)
"Neil Chambers" wrote:
All,

I have a class describing various actions to take against a LINQ to SQL
datasource. What are the pros/cons of instantiating the LINQ object either
in the root of the class (for lack of a better description) or within each
of the methods? I am naturally drawn to instantiating the LINQ object in the
root but some examples I've seen preffer to keep this local to the methods.
Is there a compelling reason for this? It seems that garbage collection
would work the same for either approach but with less overhead instantiating
in the 'root' (at least to my relatively novice eye).
//instantiate in 'root' of class - this approach feels less wasteful
public class myDB
{
private myLINQObject db = new myLINQObject();

public void action1(){for i in db.tbl1...}
public void action2(){for i in db.tbl2...}
}

//instantiate in methods - seems like a lot of repetition when I know the
object will be needed anyway
public class myDB
{
public void action1()
{myLINQObject db = new myLINQObject();
for i in db.tbl1...}

public void action2(){
myLINQObject db = new myLINQObject();
for i in db.tbl2...}
}
Many thanks :)
n

Sep 3 '08 #2
Bruce,

Thanks for the insight.

I went about making some changes to implement using(){} automatic disposal
and was surprised by some of the results. Specifically, returning a LINQ
DataContext result to a page control didn't work. I got errors about the
object being disposed before it was used. This is actually a feature of the
LINQ DataContext - the connection/query etc is only actioned when the
results are interrogated. A few searches later I landed on this post:

http://weblogs.asp.net/stephenwalthe...-or-don-t.aspx

It would appear that calling dispose on the DataContext itself to free up
the database connection is not actually required - it's handled
automatically. Neato!

All that's left is the memory reference to the DataContext which will get
collected at some point anyway - should I worry about this? I guess that is
dependent on the amount of resources I have available.

Thanks again!

n

"bruce barker" <br*********@discussions.microsoft.comwrote in message
news:50**********************************@microsof t.com...
if you keep the linq object as a private field, when is dispose called
(you
forgot in your sample code)? did you implement IDispose in you db class
and
call dispose on the linq object? you are moving the responsibility to the
caller to call dispose on the dbclass. are the undisposed objects
(unmanaged
memory) being keep too long? better to release resources as soon as
possible.

// dispose as soon as possible:

public void action1()
{
using(myLINQObject db = new myLINQObject())
{
for i in db.tbl1...
}
}
-- bruce (sqlwork.com)
"Neil Chambers" wrote:
>All,

I have a class describing various actions to take against a LINQ to SQL
datasource. What are the pros/cons of instantiating the LINQ object
either
in the root of the class (for lack of a better description) or within
each
of the methods? I am naturally drawn to instantiating the LINQ object in
the
root but some examples I've seen preffer to keep this local to the
methods.
Is there a compelling reason for this? It seems that garbage collection
would work the same for either approach but with less overhead
instantiating
in the 'root' (at least to my relatively novice eye).
//instantiate in 'root' of class - this approach feels less wasteful
public class myDB
{
private myLINQObject db = new myLINQObject();

public void action1(){for i in db.tbl1...}
public void action2(){for i in db.tbl2...}
}

//instantiate in methods - seems like a lot of repetition when I know the
object will be needed anyway
public class myDB
{
public void action1()
{myLINQObject db = new myLINQObject();
for i in db.tbl1...}

public void action2(){
myLINQObject db = new myLINQObject();
for i in db.tbl2...}
}
Many thanks :)
n

Sep 4 '08 #3

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

Similar topics

11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
14
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them......
4
by: Tarun Mistry | last post by:
Hi all, I have posted this in both the c# and asp.net groups as it applies to both (apologies if it breaks some group rules). I am making a web app in asp.net using c#. This is the first fully OO...
2
by: MikeG | last post by:
When creating a class library is it wrong or not a 'Best Practice' to reference a property of an object from within a constructor or method of that object? I recall being told not to do this but I...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
13
by: G | last post by:
Hello, Looking for opinions on a fairly simple task, new to ASP.net (C#) and want to make sure I do this as efficiently as possible. I have a web based form, and I need to run some SQL before...
52
by: burgermeister01 | last post by:
First, let me say that this question is a rather general programming question, but the context is PHP, so I figured this group would have the most relevant insight. Anyways, this is also more of...
2
by: Gabriel | last post by:
Hello, I'm looking for documentation with "Best Practice" for ASP.NET application In which case use Connected or Disconnected mode Typed dataset or not ? I didn'd find anything pertinent...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
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...
1
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...
1
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...
0
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...
0
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...
0
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...

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.