backgroundTop
backgroundTop
Front Page
GreenBackgroundTop

Whirlwind Around .NET 4

Whirlwind Tour Around .NET 4 and VS 2010

*Video Quality Caution - there are some audio/visual problems with this video. Especially near the beginning*. Scott Hanselman kindly allowed us to video his Whirlwind Tour around .NET 4 talk at the Portland CodeCamp and repost his code samples. This talk is all about the new stuff that's coming up in .NET 4 and Visual Studio 2010. Thanks also to Dennis Parks who went out of his way to video the presentation.

erikmork

356 Votes

GreenBackgroundBottom
 

Important Code

Mark as Inappropriate Content

This video is licensed under a Creative Commons License.

Creative Commons License Download the Video

Information From the Video

GreenBackgroundTop

.NET 4 is Side-by-Side

.NET 4 is the first new .NET runtime in a while. Unlike .NET 2, 3, 3.5 and 3.5SP1, it doesn't rely on the .NET 2.0 runtime. Instead, it has a brand new runtime that will be side-by-side deployed with the existing versions.
GreenBackgroundBottom
 
GreenBackgroundTop

Multi-Targetting

VS2010 can target different frameworks. It used to be that picking a Visual Studio version tied you to a framework version. No longer, but now, for instance, VS2010 can build Framework 2.0 applications.
GreenBackgroundBottom
 
GreenBackgroundTop

ClientIDs in WebForms

Client IDs used to be a problem because their names were difficult to predict. This lead to issues on the client when we were attempting to use those IDs. In .NET 4, the "ClientIDRowSuffix" and "ClientIDMode" attributes allow fine grained control into how the client IDs are generated.
GreenBackgroundBottom
<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Television Series Picker</title>
        <link href="Default.css" rel="stylesheet" type="text/css" />
        <script src="jquery-1.3.2.min.js" type="text/javascript"></script>
        <script src="ui.core.js" type="text/javascript"></script>
        <script src="ui.sortable.js" type="text/javascript"></script>
        <script type="text/javascript">

            $(document).ready(function() {
                $("#televisionList li").append("<div><span>Click</span></div>");
                $("#televisionList").sortable({ handle: $("#televisionList li div") });
            });            
        </script>
    </head>
    <body>
        <form runat="server">
            <h1>Television Series Picker</h1>
            <p>Order the following television series' based on which you think is most awesome:</p>
            <asp:ListView
                DataSourceID="televisionDataSource"
                ClientIDRowSuffix="ID"
                ClientIDMode="Predictable"
                runat="server">
                <LayoutTemplate>
                    <ol id="televisionList">
                        <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
                    </ol>
                </LayoutTemplate>
                <ItemTemplate>
                    <li id="televisionItem" runat="server">
                        <%# Eval("Name") %>
                    </li>
                </ItemTemplate>
            </asp:ListView>
            <asp:ObjectDataSource
                ID="televisionDataSource"
                TypeName="_01_ClientId.TelevisionDataProvider"
                SelectMethod="GetTelevisionSeries"
                runat="server" />
        </form>
    </body>
</html>
 
GreenBackgroundTop

Routing

Route allows us to specify how the URLs should look in an application (think ASP.NET MVC). In this example code, the URL is based on a category and is handled by the Products.aspx page. Notice that this is a two way process. The URLs trigger dispatching to a particular .aspx page, but the routes are also used to generate URLs for linking purposes.
GreenBackgroundBottom
public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.Add("Product",
            new Route("Products/{category}",
            new PageRouteHandler("~/Products.aspx")));
    }
}
 
GreenBackgroundTop

Using the Route

The Route can be used in code, of course, but it can even be used in other places as well. Here's an example of using the "Category" portion of the route in a Data Source.
GreenBackgroundBottom
<asp:EntityDataSource
    ID="productsDataSource"
    ContextTypeName="AdventureWorksLTEntities"
    EntitySetName="Products"
    OrderBy="it.Name"
    Where="it.Category.Name = @Category"
    runat="server">
    <WhereParameters>
        <asp:RouteParameter
            Name="Category"
            RouteKey="Category"
            Type="String" />
    </WhereParameters>
</asp:EntityDataSource>
 
GreenBackgroundTop

ASP.NET AJAX 4

AJAX 4 allows client side databinding. This means that instead of using Javascript to parse XML/JSON and put that information into the DOM, we can use binding statements instead. These statements allow us to keep the HTML and data flowing naturally. AJAX 4 can retrieve that data from a variety of sources: ADO.NET Data Services, ASMX/JSON, and MVC JSON ActionResults. It has client side change tracking, and the ability to push changes back to the server.
GreenBackgroundBottom
    <body
        xmlns:sys="javascript:Sys"
        xmlns:class="http://schemas.microsoft.com/aspnet/class">
        <h1>Customer Directory</h1>
        <table cellspacing="0">
            <thead>
                <tr>
                    <th>Title</th>
                    <th>First Name</th>
                    <th>Middle Name</th>
                    <th>Last Name</th>
                    <th>Suffix</th>
                </tr>
            </thead>
            <tbody id="customers-template" class="sys-template">
                <tr sys:command="select" class:odd="{{ $index % 2 != 0 }}">
                    <td>{binding Title, defaultValue=}</td>
                    <td>{binding FirstName}</td>
                    <td>{binding MiddleName, defaultValue=}</td>
                    <td>{binding LastName}</td>
                    <td>{binding Suffix, defaultValue=}</td>
                </tr>
            </tbody>
        </table>
        <fieldset id="customer-template" class="sys-template">
            <legend>{{ FirstName + " " + LastName }}</legend>
            <label for="first-name">
                <span>First Name:</span>
                <input type="text" id="first-name" value="{binding FirstName}" />
            </label>
            <label for="middle-name">
                <span>Middle Name:</span>
                <input type="text" id="middle-name" value="{binding MiddleName, defaultValue=}" />
            </label>
            <label for="last-name">
                <span>Last Name:</span>
                <input type="text" id="last-name" value="{binding LastName}" />
            </label>
            <input id="update-button" type="button" value="Update" />
        </fieldset>
    </body>
 
GreenBackgroundTop

MEF

The Managed Extensibility Framework is a matchmaking service. It brings together plugins with applications. Essentially, it allows for easy extension of managed applications. In fact, Visual Studio 2010 uses MEF for its extensibility.
GreenBackgroundBottom
 
GreenBackgroundTop

Parallel LINQ

PLINQ gets at the idea of using more declaritive statements to specify what and not how an application should execute. This allows the framework to chop up tasks and distribute them among many processors/cpus. In the most straight-forward case, a LINQ query can be parallelized simply by adding ".AsParallel" to the end.
GreenBackgroundBottom
 
GreenBackgroundTop

DLR

The DLR doesn't just improve Ruby and Python. It also allows for cool scenarios in more statically typed languages (C#, for instance). The new "dynamic" keyword is a good example of this. It allows more natural interactions with scripting languages (IronPython, COM automation objects, etc.).
GreenBackgroundBottom
 
GreenBackgroundTop

Primay Interop Assemblies

These assemblies provide wrappers around COM servers. Unfortunately, they're big; They include all the managed types that duplicate the public bits (interfaces, structs, etc.) in the unmanaged code. Visual Studio 2010 allows embedding of these interop types, and it only brings in the types that your application is using.
GreenBackgroundBottom
 
GreenBackgroundTop

Dynamically Typed Objects

The dynamic keyword allows late-bound invocation of objects. The upside is that it makes scriptable objects (COM or Python or whatever) much easier to deal with. The downside is that the invocations can't be checked until runtime, and it won't be a tool for every situation. For interop situations, however, it will be a real time saver.
GreenBackgroundBottom
//This
Calculator calc = GetCalculator();
int sum = calc.Add(10, 20);

//Is the same as this
var calc = GetCalculator();
int sum = calc.Add(10, 20);

//But what if you're interacting with a dynamic calculator (maybe it's COM or another scripting language).  You've got to use reflection:
object calc = GetCalculator();
Type calcType = calc.GetType();
object res = calcType.InvokeMember("Add",
    BindingFlags.InvokeMethod, null,
    new object[] { 10, 20 });
int sum = Convert.ToInt32(res);

//With the dynamic keyword, this scenario becomes a lot easier
dynamic calc = GetCalculator();
int sum = calc.Add(10, 20);
 
GreenBackgroundTop

.NET Framework Compatibility

.NET 4 lives side-by-side with the other versions of .NET. While previous .NET versions will mostly work fine if built against .NET 4 (think about the transition between 1.0 and 2.0), this is a side-by-side release. Applications will specifically need to target .NET 4 to run in that environment.
GreenBackgroundBottom
 
GreenBackgroundTop

Process Separation

.NET 4 provides the ability for different framework versions to be loaded inside of the same process space. This allows for scenarios where plugins might be expecting different versions of the runtime (explorer, etc.).
GreenBackgroundBottom
 
 
GreenBackgroundTop

References

Scott Hanselman's Blog

Scott's blog is pretty much a must-read for .NET developers.

Routing/WebForms and AJAX 4

Scott's post/sample code

PIAs and Office Automation

Code for Primary Interop Assemblies and Office in .NET 4.

Dynamic Keywork

Scott's post and code on the dynamic keyword.

MEF Post

Scott's sample MEF Code.

MEF Home

Codeplex site with all of the MEF code, docs and samples.

PLINQ Article

MSDN article about PLINQ

GreenBackgroundBottom
backgroundBottom
backgroundBottom