Posts Tagged ‘Visual Studio 2008’

Linq or NetTiers?

December 26, 2007

With the core work of our first foray into collaborative web development out of the way, Jason and I have begun to discuss our next project. With that, I’m taking the opportunity to take a step back and think about what things we might be able to do differently next time around (me contributing more is but one).

In our first project, BuddyBetter.com, Jason and I leaned heavily on NetTiers to do most of the heavy lifting for us. It didn’t take me long to become an instant convert to how NetTiers could totally change my life as a coder. It’s fast, simple to use, and generates your entire data access layer for you. So why would I consider changing?

Enter Linq. With the release of .NET 3.5 and Visual Studio 2008, Linq finally becomes an ‘official’ part of .NET. Standing for “Language interpreted Query”, Linq is a radical new approach to treating a database in an object oriented fashion. Instead of the old paradigm:

// create dataset from a DB table    DataSet oDataSet = ExecuteDataset(ConnectionString, CommandType.StoredProcedure, "StoredProc",sqlParams);
 // Code here to load this dataset into a custom collection (or handle natively)

Of course the beauty of .NetTiers is that it handles the above code for you. However, under the hood it’s still treating the database like what it is — a collection of rows and columns that represent some object in the real world.

Where Linq differs is that it allows you to skip this conversion step and jump immediately to handling your database like a collection of objects. Now we can do something like :

using System.Data.Linq; // be sure to add this. the System.Linq namespace is not sufficient for SQL to Linq  // Create a class for each table we want to access (or use a tool like SQL metal to automatically create the classes for us)  [Table (Name="Groups")]
    public class Groups
    {
        [Column(IsPrimaryKey = true)]
        public Guid ID;
        [Column]
        public string GroupName;
        [Column]
        public string GroupSiteUrl;
    }     // now we can access our table thusly:  // actually handles the main connection to the DB
DataContext oContext = new DataContext(sConnectionString);            

// getting our table "Groups" --> see Groups.cs in this project
Table<Groups> Groups = oContext.GetTable<Groups>();
// query-like language. you can do wheres, group bys, orders, etc
var q =
 	from c in Groups
	select c;

 // all of the rows
foreach (var group in q)
	{
         	string s = group.GroupName;
                string r = group.GroupSiteUrl;
                Guid g = group.ID;
        }

And that’s it. You are now accessing table data using Linq.

So with all of this being said, what do we do for our next project? Stick with .NetTiers? Or move on to Linq. My gut tells me to sit tight and use NetTiers for the next project or two while we hone our Linq skills. I still have a lot to learn. Your thoughts Jason?