Linq to SQL

I’m not even sure what led me to start playing around with the new Linq to SQL (formerly called DLinq) DAL in VS2008B2, but I’m glad I did. I discovered some very fascinating functionality that I believe will really shake up the way most developers approach the DAL. I see some similarities  between Linq to SQL and other OR/M plugins (tools, methodologies) out there, but since this is Microsoft’s approach, I anticipate it will catch on en mass. I don’t believe Microsoft is trying to make the other methods obsolete, but that they are delivering a product demanded by their customers. One of the problems with the current OR/M scene, is that there are so many options to choose from, that you never know if you’ve made the right choice. While Linq to SQL, may have some deficiencies, it will give developers, and their managers a little piece of mind when breaking from the traditional DAL mold. I can only hope that Linq to SQL does not disappoint, even after taking into consideration it’s a Gen 1 product. Issues such as performance, extendibility, and plugability (into one’s code or way of doing things) are some things that I want to explore (as with any tool such as Linq to SQL).

I used this article to help jump start my foray into Linq to SQL (let’s just call it L2S from here on). Being away from WinForms for so long, I’ve missed out on some of the data binding features available, and was surprised to see how easy it was to get up and going with a grid and details form. I remember seeing the videos when VS 2005 was coming out, but never had a need to play around with it until now. I’ve also never had good luck with drag and drop data binding, as it assumes to much, I’m hoping that there are plenty of hooks available to help customize the data binding experience, but any fault with data binding should not be confused with L2S as they are 2 separate features (actually data binding is a feature, and L2S is a feature of VS 2008, and part of the .Net Framework 3.5).

In a couple of minutes, I was able to generate a data class using the new visual OR/M tool in VS2008, create a binding source, drag/drop elements from the binding source onto my form, and create a functional application. Functional should not be confused with enterprise level, nor was I expecting it to be. If I could create something like that in 2 minutes using drag and drop, my value ($$$$) as a developer would be on the decline. Allow me to recap some of my steps I took to create a functional application.

  1. Create a new Winforms 3.5
  2. Right click on the new project and select add new item, choose Linq To SQL Classses.
    1. This will create a new .dbml file. It will open a design surface where you can drag/drop tables, views and stored procedures. This creates a DataContext class.
  3. Create a new database connection, or open an existing connection on your Server Explorer Window
  4. Drag tables from the database from the Server Explorer to the design surface of your DataContext class.
  5. From the top menu bar, choose Data/Add new data source, or, show the data sources window, and choose the add new data source option
    1. Create a project data source
    2. Here is where it starts to get tricky. Select an object to make a data source for. It will depend on what you are trying to accomplish and your database schema. The schema I was using, relies heavily on extended tables, and it took me a couple of tries to pick the object that made the most sense. It’s possible that I will have to go back and create a different object down the road as I get more into this.
  6. If you haven’t already opened the Data Source window, do so now. Select some elements from your data source, and drag/drop them onto the default Form1. I chose a grid to list a collection of objects, and then their details below. A BindingNavigator was added for me automatically. If you didn’t get one, or need to add one later, just drag one onto the form from the tool box, and set it’s BindingSource property to the bind source you created in step 5.
  7. Open the code view of Form1
  8. Add a private member variable: DataClasses1DataContext test;
  9. Under the form’s constructor, do the following
  10.    1: public Form1()
       2: {
       3:  InitializeComponent();
       4:  test = new DataClasses1DataContext();
       5:  if(test.DatabaseExists() == false) //Not required, just testing feature found thru intellisense
       6:   test.CreateDatabase();
       7:  x_t_personBindingSource.DataSource = test.x_t_persons;
       8: }
  11. Jump back to the Form, and add a button to your binding navigator for saving. Double click on the new button to have the event handler code generated and add the following:
  12.    1: private void saveToolStripButton_Click(object sender, EventArgs e)
       2: {
       3:  test.SubmitChanges(); //Need to add exception handling and pre-save validation
       4: }
  13. Build and Debug.

A connection string is automatically added to your App.Config file for you. You can test out the CreateDatabase feature by changing the connection string to a non-existent database. You should be able to add/edit/delete records in your database, using this simple application we just created. The DataContext class, is the top level class in the hierarchy used to work with your database. It controls the connection to the database, and, as you’ve seen, has support for creating and deleting the database. The create database feature reminds me of a similar feature in the Castle’s Active Record project. Just take a look via object explorer or intelli-sense, all of the methods available to you. I’ve listed the ones that caught my attention below. 

  • Create/Delete Database
  • Deferred Loading Enabled: Implies that L2S uses deferred loading for many to one relationships
  • ExecuteCommand: Direct execution of a command.
  • ExecuteQuerey: Returns IEnumerable or <T> based on a provided query.
  • GetChangeSet: Returns changes tracked by the DataContext
  • Transaction: Set a transaction object to support transactions

Validation – Check out ScottGu’s part 4 on L2S

  • Schema Validation – Linq is supposed to check for obvious schema issues, such as null values. However, the exception I got when I tried to insert a null value, came from SQL, not Linq. Also I was adding items on a grid, and added 2 items in a row. Their Id fields were both set to 0, and when I went to save, I got a Linq exception saying I couldn’t have duplicate keys. Setting the ID values to something different, allowed me to get to the database at least, so I need to look into this more.
  • Property and Submit Validation – Create a partial class, then a partial method that implements validation when a property is changed. If you type partial in the code view window, you will get a list of partial methods you can implement. You have access to 2 methods for each property, OnChanging and OnChanged. You also get access to a OnValidate method. If there is an error, you throw an exception. This works OK, but doesn’t provide the nicest feedback available. However, for anything less then an enterprise application, this may be OK, especially in a proof of concept, or highly iterative application, where you can add better a better user experience later. 
  • Insert/Update/Delete Validation – Same scheme as property and submit validation, but you can have validation in response to an Insert, Update or Delete.
  • ChangeSet: Starting with Beta2, you can get access to all of the changes in the DataContext. One possible use, is to inherit the DataContextClass, and override the SubmitChanges method, checking the ChangeSet and performing validation.
  • My Take on validation:
    • There is some work that needs to be done with validation. I could see an edition to the Enterprise Library designed specifically for L2S.
    • My own idea would be to create a class to track validation errors.
      • New custom exception called validation error.
      • A collection of validation errors, including error text, and the property that is invalid
      • Starting to sound allot like CSLA

Other things I noticed

    • You can manually create classes in the Design Surface, as well as edit the auto generated ones
      • Control nullable, access modifier, and inheritance modifier.
    • There is an inheritance property you can set on the design surface, allowing you to indicate which objects inherit from another. This looks very promising for some of the schemas I work with.  

What’s Next?

I’m going to read over ScottGu’s 7 part series on L2S to get some good ideas on what I can do with L2S, and see how it can fit into future and existing projects. I am really excited to see how people start to integrate L2S with their existing projects and frameworks. Of particular interest would be using CSLA and L2S.

Additional Resources

Note: First post where I’ve had formatted HTML for source code. I’m using the Code Snippet Plugin for Windows Live Writer.

This entry was posted in Programming (Other), Tooling and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">