Wednesday, August 15, 2007

First Experiences With VS2K8 Part 2

This is the second in a series of posts I'm going to be making about my experiences with Visual Studio 2008 and .NET3.5. The first post can be found here. In this post I will discuss how I set up my LINQ to SQL data model and how I started using it to build my order taking application. This is not intended to be a tutorial on the specifics of LINQ to SQL. For that I recommend ScottGu's excellent series of posts on the subject which can be found here. For now I've got two projects making up this solution. One is a Web Project (called OrderEntryApp) and is the site with that users will be accessing. The other is a class libarary and is called OrderDBLINQ. It contains the LINQ to SQL mapping and generated classes. For the OrderDBLINQ project I simply chose to add a new item and selected the Linq To Sql classes option. This created a designer view for me. I then opened the Server Explorer and dragged the tables I was interested in accessing from there onto the new designer view. This is a legacy database that was created by one of our vendors and for various reasons it would be quite difficult to change it. This is kind of lame since the db in question is not very well designed. Naming conventions are nonexistent and there isn't a single foreign key defined anywhere. But I figured this would make it a good test for LINQ to SQL. Since there aren't any foreign keys in my crappy db schema the LINQ to SQL designer was unable to infer the relationships of my new objects. This is only a minor stumbling block however since the designer offers tools for defining relationships between objects. The interface is quite similar to the one used by SqlServer to create foreign keys. Each order in my application is placed by a single customer so I created a relationship between the OrderHeaders table and the Customers table. An order header will have multiple detail items associated with it so I created a relationship between OrderHeaders and OrderDetails. Each OrderDetails entry references a single Product so I created a relationship between OrderDetails and Products. Finally there is a many-to-many relationship between Products and Categories. This many-to-many is defined in the ProductCategories table so I created relationships between Products and ProductCategories and also between Categories and ProductCategories. This all took me about 20 minutes and a good portion of that time was spent just figuring things out for the first time. All in all it was quite easy to do. But would the code generated from this diagram actually be able to service all of my applications needs? Well first I figured I'd start with a simple test page. In my OrderEntryApp web project I added a new AJAX Web Form item called Test.aspx. My current goal here was simply to bind a read-only GridView to my Customers table. The first step was to add a LinqDataSource object to my page and configure it. I was able to choose the DataContext object that was created by the code generator in my OrderDBLINQ project. Next I selected the Customers table from the drop down provided and left everything else at the defaults. Next I added a GridView to my page. The only property other than 'ID' and 'runat' that I had to set was DataSourceID. I set this to the ID of the LinqDataSource I'd just created and ran the page. Sure enough there on the page was a simple GridView with all of my customers' information listed. Pretty neat! Now this was a fairly simple test I suppose but it was also quite simple to implement. Certainly easier than doing this the old way. Of course now I had a gigantic web page listing every single customer in my database. My next step would be to add some tools to refine the list and add support for paging and sorting. And that will be the subject of my next post.

No comments: