Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 2010 > BOOK: Professional Visual Basic 2010 and .NET 4
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional Visual Basic 2010 and .NET 4
This is the forum to discuss the Wrox book Professional Visual Basic 2010 and .NET 4 by Bill Sheldon, Billy Hollis, Kent Sharkey, Gaston Hillar, Rob Windsor, Jonathan Marbutt; ISBN: 9780470502242
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Visual Basic 2010 and .NET 4 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #11 (permalink)  
Old April 5th, 2011, 10:53 AM
Registered User
 
Join Date: Mar 2011
Posts: 17
Thanks: 4
Thanked 1 Time in 1 Post
Smile Your suggestion works great!

THIS IS NOT WORKING AFTER ALL. SOMETHING ABOUT ACCESSING A DISPOSED OBJECT. I NEED TO LOOK AT IT MORE.

Hi Imar,

Here is what I did. I use a Try/Catch in all procedures. I put a dc.Dispose() in the Catch block and upon entering each procedure, I have:

Code:
If dc Is Nothing Then
    dc = New PubsDataContext
End If
This is because I would need to re-instantiate dc if I had to dispose of it because of a previous error.

This worked in all cases.

Is this what I should be doing?

Thanks,

mangel

PS: You are an inveterate teacher; I can see why you are a WROX author. I for one appreciate you beyond measure.

Last edited by mangel; April 5th, 2011 at 11:03 AM.
Reply With Quote
  #12 (permalink)  
Old April 5th, 2011, 11:17 AM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

Hi mangel,

It probably doesn't work because Dispose() does not set the object reference to Nothing.

In other words, dc is still a valid object (and thus not Nothing) but internally it's been disposed.

I see a few options:

1. In the Catch block, call Dispose and directly create a new instance.

2. Create a wrapper around your context that manages the life time. Here's a quick example:

Code:
 
Public Class ContextWrapper
  Private _dataContext As PlanetWroxDataContext
  Private _isReset As Boolean = False
  Public Sub New()
    _dataContext = New PlanetWroxDataContext()
  End Sub
  Public ReadOnly Property DataContext As PlanetWroxDataContext
    Get
      If _isReset Then
        _dataContext = New PlanetWroxDataContext()
        _isReset = False
      End If
      Return _dataContext
    End Get
  End Property
 
  Public Sub SaveChanges()
    _dataContext.SubmitChanges()
  End Sub
 
  Public Sub ResetChanges()
    _dataContext.Dispose()
    _isReset = True
  End Sub
End Class
You can now instantiate an instance of ContextWrapper and access its DataContext property. You can call ResetChanges which kills the underlying context, but a new one is created when you access the context again. Note: untested code, so make sure you test this before you start using this.

With this code, you don't need to check the object in each method call as now the wrapper will take care of this.

If you go down this route, implementing a Repository pattern may be a better idea: http://www.google.com/#sclient=psy&h...y+unit+of+work

Quote:
PS: You are an inveterate teacher; I can see why you are a WROX author. I for one appreciate you beyond measure.
Thank you!

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; April 5th, 2011 at 11:28 AM.
Reply With Quote
  #13 (permalink)  
Old April 5th, 2011, 12:37 PM
Registered User
 
Join Date: Mar 2011
Posts: 17
Thanks: 4
Thanked 1 Time in 1 Post
Default

Hi Imar,

Thanks again. Before I use your wrapper suggestion and because I tried something else before I received your message and because it appears to be working and because it is a simple change from what I did earlier, let me tell you what it is. I removed the if is nothing code and used the following in the catch block instead.

Code:
dc.Dispose()
dc = New PubsDataContext
I added code to deliberately create exceptions to see if things would recover using this approach. I tried to add a title that had already been added, the exception occurs and then the code above runs. I next update an existing title and it works; so it looks like it recovered from the previous exception. I then try to update a non-existing title and the exception occurs and so I run the code above again. I then delete a title without a problem; so it looks like it recovered from the second exception. The rest of the examples work properly. Is there a gotcha here I am not considering?

Thanks,

mangel
Reply With Quote
  #14 (permalink)  
Old April 5th, 2011, 12:48 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

Quote:
The rest of the examples work properly. Is there a gotcha here I am not considering?
Nope, this is exactly what I meant by:

Quote:
In the Catch block, call Dispose and directly create a new instance.
The only "problem" with this code is that it might break the DRY principle. You now have to remember to call Dispose and reinstantiate dc in every Catch block.

By creating a wrapper or a repository, you can centralize this behavior and make things as easy as a single method call.

So, it's mostly a design and maintainability decision, not necessarily a technical decision.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
mangel (April 5th, 2011)
  #15 (permalink)  
Old April 5th, 2011, 04:23 PM
Registered User
 
Join Date: Mar 2011
Posts: 17
Thanks: 4
Thanked 1 Time in 1 Post
Default DRY

Hi Imar,

I've not heard of DRY before but I generally try not to repeat myself in code. My intention was to find out if my solution was technically correct and if so I would go on to the next step. I tend to use functions and subs even if only to act as self-commenting code; so I'm a DRY kind of guy. Here is what I did:

Code:
Private Function RefreshPubsDataContext(ByVal dc As DataContext) As PubsDataContext
    dc.Dispose()
    Return New PubsDataContext
End Function
Code:
Private Function RefreshPubsDataContext(ByRef dc As PubsDataContext) As PubsDataContext
    dc.Dispose()
    Return New PubsDataContext
End Function
I can use either of the functions above in the catch block as:

Code:
dc = RefreshPubsDataContext(dc)
But I think that the ByRef version is preferable.

So all I have to remember is to refresh the data context when there are exceptions. This works quite well.

I am now trying implement the wrapper and/or the repository.

Thanks,

mangel

Last edited by mangel; April 5th, 2011 at 04:34 PM.
Reply With Quote
  #16 (permalink)  
Old April 5th, 2011, 07:19 PM
Wrox Author
 
Join Date: Aug 2003
Location: Oceanside, CA, USA.
Posts: 12
Thanks: 0
Thanked 2 Times in 2 Posts
Default

A few things to keep in mind:
1. You don't always need to destroy and recreate your DataContext - you can refresh it: http://msdn.microsoft.com/en-us/libr...t.refresh.aspx just pay attention to which flag you use, typically after an error you'll want to force an update from teh DB.
2. Keeping your data context constantly open isn't always best practice, especially in a multi-user environment. The data context for LINQ-to-SQL uses optomistic locking. This means if another user makes changes in the same table behind you your update may fail.
3. Most importantly consider LINQ-to-SQL to be 'depricated'. It isn't officially, however it's in a maintenance only mode. Note LINQ to datasets, EF, XML etc. are all alive and well, I have an article on SQL Mag discussing the current state of LINQ and in particular LINQ to SQL. The good news is the transition from LINQ to SQL to LINQ to EF is rather painless.
__________________
Bill Sheldon
Professional Visual Basic 2010 and .NET 4
(also all prior editions of Professional VB.NET)
Professional Office Business Application Development
Reply With Quote
The Following User Says Thank You to BillS For This Useful Post:
Imar (April 6th, 2011)
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Linq to SQL and the Entity Framework marko_ln BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 4 February 25th, 2010 10:20 AM
LINQ to SQL or not? gvdamn BOOK: Professional DotNetNuke Module Programming ISBN: 978-0-470-17116-5 2 August 20th, 2009 05:08 AM
LINQ to SQL bakm04 BOOK: Professional C# 2008 ISBN: 978-0-470-19137-8 0 August 17th, 2009 04:07 PM
Linq: 'Specified cast is not valid' error exception. mksingh Visual Studio 2008 2 February 4th, 2009 10:06 PM
LINQ to SQL in TheBeerHouse zhoux BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 October 1st, 2008 07:37 AM



All times are GMT -4. The time now is 08:36 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.