.NET Blog

Tony Cavaliere

 
My Favourite Albums
  And the Grappa wins.
E-mail me Send mail
Add to Technorati Favorites AddThis Feed Button

Subscribe to Cynot Why Not


Recent posts

Disclaimer

Hey unlike other bloggers I stand by what I say but just in case. The opinions expressed herein are my own except on Tuesday when the second card is not turned up otherwise it ain't worth squat.

© Copyright 2010

Linq to SQL: A small CRUD application

For the last month or so I have been helping out with a medium .NET windows forms application that uses Linq to SQL for it’s ORM. It had been a while since I last used Linq to SQL, so I thought it would be a good time to refresh my skills and write a proof of concept application, one that used Linq to SQL to create, read, update and delete records.

The model consists of four classes and is shown in the following figure.

BookModel

Linq to SQL Model Diagram

The Book object contains references to each of the Genre, Publisher and Author objects. In this contrived example, a Book must have one and only one Author, Genre and Publisher.

Early one in this prototype, it became apparent that it was important to manage the lifetime on the Linq to SQL DataContext. Rich Strahl has an excellent post on managing the lifetime of DataContext. In this application, the Linq to SQL DataContext is abstracted within a BookRepository class and a singleton is used to serve up a reference to the repository. The following listing shows how this singleton is implemented.

    8   public sealed class Repository

    9   {

   10 

   11     static BooksRepository _bookRepository = null;

   12     static string connectionString;

   13 

   14     private Repository() { }

   15 

   16     public static void ConnectionString(string cs) { connectionString = cs; }

   17 

   18     public static BooksRepository BookRepository(Boolean create)

   19     {

   20       if (connectionString == null)

   21         throw new ApplicationException("Need to set connection string for Repository");

   22       if (!create && _bookRepository != null) return _bookRepository;

   23       else

   24       {

   25         _bookRepository = new BooksRepository(connectionString);

   26         return _bookRepository;

   27       }

   28     }

   29 

   30   }

The BookRepository method has a single parameter and decides whether to return a new BooksRepository or to return the existing BooksRepository, remember the BooksRepository abstracts the DataContext. Each call to  Repository.BookRepository(false) returns the same BooksRepository and therefore uses same DataContext.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: LINQ
Posted by CynotWhyNot on Wednesday, July 08, 2009 9:15 AM
Permalink | Comments (11) | Post RSSRSS comment feed

Related posts

Comments

maui lodging us

Monday, November 16, 2009 6:16 AM

maui lodging

This is really nice post.


Regards
Hickman



ukhostinghostingdirectory.com

Thursday, December 17, 2009 2:31 AM

pingback

Pingback from ukhostinghostingdirectory.com

Linq to SQL: A small CRUD application | UK Hosting Directory

James us

Thursday, January 21, 2010 7:09 AM

James

Very good and detailed post there OP. Apprecaiate the time you put into this.

Regards
www.prlog.org/...ancy-treating-pregnancy-acne.html
www.prlog.org/...roducts-devices-and-remedies.html

Oil Change us

Tuesday, January 26, 2010 2:32 PM

Oil Change

Thanks for posting this. Would be intrested to read more or possibly please contact me by email thank you!

272.computeronlinebingo.com

Thursday, May 20, 2010 2:25 PM

pingback

Pingback from 272.computeronlinebingo.com

D450 Repair Great Outdoors, D450 Bulb Halogen Acura Integra

air force 1s US

Friday, May 28, 2010 10:37 PM

air force 1s

I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!

adidas shoes US

Wednesday, July 28, 2010 1:58 PM

adidas shoes

I have been looking for content like this for a research project I am working. Thanks very much. These types of cheap electronic gadgets used to be readily available down at the mall when stores like Brookstone and Sharper Image were around, but when those places closed down I obviously had to come up with a different way to feed my habit.

nike air ltd US

Tuesday, August 24, 2010 6:19 PM

nike air ltd

I just wanna thank you for sharing your information and your site or blog this is simple but nice article I have ever seen i like it i learn something today... Having these terrifying attacks are not uncommon.

air max 90 shoes US

Tuesday, August 24, 2010 8:12 PM

air max 90 shoes

Your blog is perfect, and I like this article. I find the information I need. I think I can find more useful information here, thanks.

2010 slippers US

Wednesday, August 25, 2010 12:14 PM

2010 slippers

Hey, guy, your blog is nice. It can bring me many useful information.

gucci US

Friday, September 03, 2010 12:10 AM

gucci

You got fantastic nice ideas there. I made a research on the topic and got most peoples will agree with your blog. All you have to do is spend some time on the Internet to find stores that offer the items you are most interested in, so start browsing today!

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Saturday, September 04, 2010 2:03 AM