Entries Tagged 'LINQ' ↓

Jan 24th Links: ASP.NET, ASP.NET AJAX, Visual Studio, .NET, IIS

I just arrived back from my trip from Asia, and decided to celebrate (since I'm jet-lagged and can't sleep) with a new post in my link-listing series.  You can check out my ASP.NET Tips, Tricks and Tutorials page for links to popular articles I've done myself in the past.

ASP.NET

ASP.NET AJAX

Visual Studio

  • Using Ctrl-Break to Stop VS Building: Steven Harman points out a cool tip/trick, which is that you can use the Ctrl-Break key within Visual Studio to kill the current compilation build.  A useful tip if you've accidentally kicked off a long build or get tired waiting for it to finish.

.NET

  • Marshaling between Managed and Unmanaged Code: Yi Zhang and Xiaoying Guo from my team in Shanghai have written a great MSDN article that describes how to use the marshaling interop features of the CLR to call native code.  One of the tools they highlight is an awesome P/Invoke Interop Assistant application they built that makes it much, much easier to generate p/invoke interop signatures when calling native methods.  A must-have tool for anyone doing native/managed interop!

  • .NET Framework 3.5 Poster: Brad Abrams posts about the cool new .NET Framework 3.5 posters now available for download (now in multiple file formats).

IIS

  • Microsoft Web Deployment Tool Technical Preview 1: Yesterday the IIS team posted the first preview of a new Microsoft Web Deployment tool.  This tool works with both IIS6 and IIS7 and enables automated deployment, synchronization, and migrating of applications on web servers.  If you are looking for a great way to automate the deployment of your ASP.NET applications then this tool is definitely one to check out.  To learn more, read the walkthroughs at the bottom of this page (in particular the "Introduction to MS Deploy" one).  This tool is awesome and should make automated deployment much easier.

Hope this helps,

Scott

Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library)

LINQ (language integrated query) is one of the new features provided with VS 2008 and .NET 3.5.  LINQ makes the concept of querying data a first class programming concept in .NET, and enables you to efficiently express queries in your programming language of choice.

One of the benefits of LINQ is that it enables you to write type-safe queries in VB and C#.  This means you get compile-time checking of your LINQ queries, and full intellisense and refactoring support over your code:

While writing type-safe queries is great for most scenarios, there are cases where you want the flexibility to dynamically construct queries on the fly.  For example: you might want to provide business intelligence UI within your application that allows an end-user business analyst to use drop-downs to build and express their own custom queries/views on top of data. 

Traditionally these types of dynamic query scenarios are often handled by concatenating strings together to construct dynamic SQL queries.  Recently a few people have sent me mail asking how to handle these types of scenarios using LINQ.  The below post describes how you can use a Dynamic Query Library provided by the LINQ team to dynamically construct LINQ queries.

Downloading the LINQ Dynamic Query Library

Included on the VS 2008 Samples download page are pointers to VB and C# sample packages that include a cool dynamic query LINQ helper library.  Direct pointers to the dynamic query library (and documentation about it) can be found below:

Both the VB and C# DynamicQuery samples include a source implementation of a helper library that allows you to express LINQ queries using extension methods that take string arguments instead of type-safe language operators.  You can copy/paste either the C# or VB implementations of the DynamicQuery library into your own projects and then use it where appropriate to more dynamically construct LINQ queries based on end-user input.

Simple Dynamic Query Library Example

You can use the DynamicQuery library against any LINQ data provider (including LINQ to SQL, LINQ to Objects, LINQ to XML, LINQ to Entities, LINQ to SharePoint, LINQ to TerraServer, etc).  Instead of using language operators or type-safe lambda extension methods to construct your LINQ queries, the dynamic query library provides you with string based extension methods that you can pass any string expression into.

For example, below is a standard type-safe LINQ to SQL VB query that retrieves data from a Northwind database and displays it in a ASP.NET GridView control:

Using the LINQ DynamicQuery library I could re-write the above query expression instead like so:

 

Notice how the conditional-where clause and sort-orderby clause now take string expressions instead of code expressions.  Because they are late-bound strings I can dynamically construct them.  For example: I could provide UI to an end-user business analyst using my application that enables them to construct queries on their own (including arbitrary conditional clauses).

Dynamic Query Library Documentation

Included with the above VB and C# Dynamic Query samples is some HTML documentation that describes how to use the Dynamic Query Library extension methods in more detail.  It is definitely worth looking at if you want to use the helper library in more depth:

 

Download and Run a Dynamic Query Library Sample

You can download and run basic VB and C# samples I've put together that demonstrate using the Dynamic LINQ library in an ASP.NET web-site that queries the Northwind sample database using LINQ to SQL:

You can use either Visual Web Developer 2008 Express (which is free) or VS 2008 to open and run them.

Other Approaches to Constructing Dynamic LINQ Queries

Using the dynamic query library is pretty simple and easy to use, and is particularly useful in scenarios where queries are completely dynamic and you want to provide end user UI to help build them.

In a future blog post I'll delve further into building dynamic LINQ queries, and discuss other approaches you can use to structure your code using type-safe predicate methods (Joseph and Ben Albahari, authors of the excellent C# 3.0 In a Nutshell book, have a good post on this already here). 

Hope this helps,

Scott

New ASP.NET Dynamic Data Support

The ASP.NET 3.5 Extensions CTP we shipped this past weekend contains a bunch of great new features.

One of the cool new features is something we call "ASP.NET Dynamic Data Support".  In a nutshell this enables you to really quickly build data driven web-sites that work against a LINQ to SQL (and in the future LINQ to Entities) object model - and optionally allows you to-do this without having to build any pages manually.

The best way to see this in action is to watch David Ebbo's awesome 17 minute screen-cast:

You can also follow the steps below to easily get started and use the dynamic data support:

Step 1: Create a ASP.NET Dynamic Data Site:

Once you have the ASP.NET 3.5 Extensions CTP release installed, you can use VS 2008 or the free Visual Web Developer 2008 Express to create a new Web Site or Web Application Project that has the ASP.NET dynamic data support enabled:

This will create a new web-site you can use that has some default template files in them, along with CSS style-sheets you can use to customize the look and feel of them:

Step 2: Add a LINQ to SQL Data Model to Your Site

LINQ to SQL is an O/RM (object relational mapper) that ships in .NET 3.5, and which allows you to model a relational database using .NET classes.  You can then query the database using LINQ, as well as update/insert/delete data from it.  LINQ to SQL fully supports transactions, views, and stored procedures.  It also provides an easy way to integrate data validation and business logic rules into your data model.

The easiest way to get started with an ASP.NET Dynamic Data site is to add a new LINQ to SQL data model to your project (right-click->add new item->LINQ to SQL Classes item).  This will bring up the LINQ to SQL class designer.  Drag database tables from your server explorer to define a set of classes (with relationships) for your database schema:

Step 3: Enable Dynamic Templates in Your Project:

By default ASP.NET Dynamic Data projects provide built-in support for creating automatic "scaffolding" views of your LINQ to SQL and LINQ to Entities data models.  Dynamic Data projects include template pages and user controls that can automatically create built-in data browsing/editing/selection/deleting UI that is dynamically constructed at runtime based on the LINQ to SQL or LINQ to Entities data model you've added to your project.

To enable this auto-scaffolding support, open the web.config file of your project, search for the "dynamicData" section within it, and set the "enableTemplates" attribute to "true":

Note: this auto-scaffolding feature is currently turned off by default for security reasons (we want you to be explicit about turning it on). 

Note: as you can see above, the ASP.NET dynamic data section provides optional URL mapping support that allows you to customize the URLs for your dynamic scaffold pages to be in whatever URL format you want.

Step 4: Run Your Site

Hit F5 to run your project.  You'll by default be taken to a default.aspx page that lists all of the table objects in your data model:

Note: the default.aspx home-page is in the project - if you don't like how it looks just open it up and edit it however you want.

Clicking on any of the table names in the list above will take you to a dynamic data page that lists the data within it:

The default data view pages automatically create functional data navigation UI that includes built-in data paging and editing support (and are ASP.NET AJAX enabled):

The data view pages automatically infer relationships between your related objects and provide automatic filtering support:

The data view pages also automatically show relationships between your objects and allow users to easily navigate amongst them.  For example, notice how the Category column for products allows me to navigate to the category details for a specific product:

The data view pages also enable automatic selection, editing and delete support.  Notice below how when in inline edit mode they automatically infer friendly foreign key names (allowing you to edit the "Category" and pick a name from a dropdownlist instead of having to specify a "CategoryID" integer):

All of this works without having to create a single custom .aspx page or write any custom code.  All of the web UI is created dynamically off of your LINQ to SQL or LINQ to Entities data model.

Step 5: Customizing the Site Further

Having all of the above functionality work out of the box against your ORM data model is great for quickly getting your site up and running.  Once you get the basic site experience working, you can optionally go in and fully customize it - both at the data validation/business logic level as well as at the UI layer.

You can add standard LINQ to SQL data model and business validation rules to your data model in order to enforce constraints.  The dynamic data pages will automatically work off of this logic, and provide appropriate error messages and validation UI based on them. 

You can also optionally go in and customize the UI of any of the data pages you want.  Dynamic data projects allow you to customize the default look and feel of all data pages on the site, as well as customize the default look and feel of all column types used on the site.  You can customize both of these via the default template files added under the "/App_Shared" folder when you create a new dynamic data project:

Simply edit any of the files above to customize the default data page views and/or default data column appearances. 

You can also then optionally override the UI of specific data view pages by explicitly adding a .aspx page that maps to it in your project.  For example, I could override the default ListDetails behavior for Products by adding a \Products\ListDetails.aspx page to my project:

You can then add any standard ASP.NET data control to the page.  You can also use the new dynamic data aware ASP.NET data controls that ship in the ASP.NET 3.5 Extensions CTP to the page.  These allow you to completely customize the look and feel of the UI - while still using the new dynamic meta-data provider to infer relationships and meta-data about your data model classes for default behavior.

Learning More

The above walk-through only touches on a few of the scenarios and capabilities you can enable with ASP.NET Dynamic Data Projects.  To learn more:

  1. Watch David's webcast
  2. Check out the ASP.NET Dynamic Data QuickStarts
  3. Subscribe to David Ebbo's, Scott Hunter's, and Marcin Dobosz's blogs
  4. Visit the ASP.NET Dynamic Data Forum to ask questions

To learn more about LINQ to SQL and how you can use it to create data model classes, also check out my previous blog posts below:

You can also watch the great LINQ and LINQ to SQL "How do I?" videos on the www.asp.net site here.

Hope this helps,

Scott