.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

It’s time to MVCify my website.

OK it’s time. I have been spending quite some time learning ASP.NET MVC. I’ve read the Professional ASP.NET MVC 1.0 book and watched numerous videos on the topic but until you sit down and create a real website with the technology, it just won’t click. To this end, I have decided to redo my website using MVC. Frankly it needed an overhaul anyway. I used a template to construct my home and resume pages and they used tables for layout. They work perfectly fine but are outdated and need to be replaced with the preferred CSS DIV tag layout.

So here we go my first crack at creating an MVC application. I’ll be refactoring the home page first and will follow with the resume, agenda, toolbox and jQuery sites. I’m not to sure about my blog as it uses BlogEngine.net. My silverlight gallery can certainly be ported to MVC but that will take a bit longer.

So far I have almost completed MVCifying my home page. Check it out here. Hover over the menu items, my photo and my name, all done with jQuery and CSS.

Once completed I will make available the source code for download.

Currently rated 5.0 by 1 people

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

Posted by CynotWhyNot on Thursday, July 23, 2009 3:56 PM
Permalink | Comments (17) | Post RSSRSS comment feed

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 (4) | Post RSSRSS comment feed