MVP 2010 Summit is over. Next!

Add Comment | Feb 19, 2010

microsoft_mvp_logo-191x300[1] And that’s a rap. The 2010’s Most valuable Professional Summit is now over. It took place in Bellevue & Redmond, Washington, and for those who couldn’t be there for all the good stuff in the agenda, well, eyes wide open for blogs, tweets and streams.

 

In the meantime, get yourself on the run for that distinguished MVP title and get your ticket to next year’s summit as well as your trophy-kit. The girls will love it… I guess :o)

 

Next, on April 12th, Microsoft will be pairing with DevConnections for the Visual Studio Launch at the Bellagio Hotel and Casino in Las Vegas, NV. Microsoft will launch Visual Studio 2010, and by attending the Microsoft Visual Studio Conference & Expo, you will not only experience the launch alongside the Visual Studio team and industry legends - you'll get three days of sessions by the best speakers in the industry on the latest and greatest technologies, such as WPF, Silverlight and .NET 4.0.  Also, If you attend Microsoft Visual Studio Conference & Expo, you'll automatically get access to both the ASP.NET and Silverlight and the SQL Conference and Expo.

Object, var, dynamic and the DLR Showdown

Add Comment | Feb 16, 2010

There’s a time when new feature or frameworks arise and you get to know them and immediately grasp that new and useful way of doing things, but there are also times when clarity only comes after a bit of blurriness. From what I’ve read, the new Dynamic type in c# 4.0 is currently one of such cases, when compared to var. I read many developers say “What’s the point?” or “Why do I need this?”. Well, I believe it’s a matter of architectural needs, mixed with coding style. To break this discussion into pieces, and because I feel the force is strong with this one ;-) I found myself with the task of enlightening things up a bit.

 

- Help keep this blog alive -

 

 

In fact, this article hopes to settle the dust on this. I’ve seen a number of articles and posts on the web comparing and debating advantages and disadvantages, but I didn't find them clear or eligible for training purposes. And even after knowing what each one really represent and allow, their usage and necessity is somewhat cloudy. Specially when it comes to dynamic.

 

So, to get the discussion started, I believe that we first must make sure we understand the basics of each one of these types, and build our structured thinking from there. You all know the System.Object type. Or its alias, object. Everything in the .Net Framework is an object, right? You can even refer to any type as object, by boxing it like this:

//Boxing
object var1 = 23;

// This will produce a compiler error
object sum = 3 + var1;

Since we specified 23 as var1’s value, it will be inferred and internally assumed as System.Int32. However, adding 3 var1 produces an “Operator '+' cannot be applied to operands of type 'int' and 'object'” compiler error.

 

This is where var comes in handy. If, instead of object we use the var type, the compiler will infer the underlying type and take it into consideration along the method scope. I say method scope because the var type can only be declared inside a method. So, the following would work:

var var2 = 23;

object sum2 = 3 + var2;

You can see the differences when boxing an Int32 value, but the same kind of difference arises when unboxing. Assigning var1 to an int variable produces a compiler error “Cannot implicitly convert type ‘object’ to ‘int’”, suggesting a cast, while assigning var2 compiles successfully:

// Produces compiler error
int var3 = var1;

// Compiles successfully
int var4 = var2;

var is still statically typed though. The only difference is, for instance, that you don’t need to repeat the type upon initialization:

// Type is specified only once
var x = new Collection<int>();

It’s a nice bonus, but not it’s main purpose. The true purpose behind var is so that we can declare a strongly typed variable without needing to know the name of the variable's type. This is crucial in order to enable C# 3.0’s anonymous types. You wouldn't be able to declare a variable of an anonymous type if you always had to include the type name as part of the variable declaration. That's the main reason var has been added to the language.

 

In some cases, when software architecture enables you to do so, you also have the advantage of changing return types without producing compilation errors.

 

Consider the following two classes. Event though they are similar in functionality, they are, nevertheless, different types and so, bound to the usual rules:

public class Human
{
    public int Age { get; set; }

    public string Name { get; set; }
}

public class Dog
{
    public int Age { get; set; }

    public string Name { get; set; }

    public string Breed { get; set; }
}

 

Now, imagine the following method from any given business layer, that returns a new instance of type Human:

public Human GetInstance()
{
     return new Human();
}

 

And a consumption example:

var o = GetInstance();

o.Name = "Alex";
o.Age = 23;

 

If, in the future, someone changes GetIntance()’s return type to Dog, you won’t need to change the previous code, because the same rules apply to the new type, since var’s type is implicit. Weird architectural approach, but an advantage to consider nevertheless.

 

And then there’s the dynamic type. dynamic is a static type, but an object of type dynamic bypasses static type checking. It’s like a type closing the door to the compiler when he knocks asking who’s in. And, as such, you don’t get the usual intellisence information. You’ll simply get the following:

 

dynamic 

At compile time, an element that is typed as dynamic is assumed to support any operation. He simply doesn’t know what it is, nor where it comes from. You are in control :)

dynamic var1 = 23;

// Sum operation resolves successfully.
object sum1 = 3 + var1;

// This also works
int var2 = var1;

 

As you see in the previous snippet, it behaves just like var, but without intellisence. “No intellisence? Why bother then??” - you ask. We’ll get to that shortly. For now, consider the following extended Human class:

public class Human
{
    public int Age { get; set; }

    public string Name { get; set; }

    public void Punch(int intensity)
    {
        Console.WriteLine(string.Format("Punched with {0} strength.", intensity));
    }
}

 

A typical consumption example would be:

Human h = new Human();
h.Punch(100);

 

But if we used dynamic, we could do this:

dynamic h = new Human();
h.Punch(100, "Right in the face!");

 

Even though there’s no corresponding method signature in the Human class, the dynamic type made the compiler became passive, and he didn’t analyze the underlying/resolving type. So the snipped compiles just fine, but resulting in a runtime error later on.

 

The dynamic type has a broader usage than var. It can be used as a property, field, indexer, parameter, return value, local variable, or type constraint; it can be the target of an explicit conversion and it can be the right side assignment of both is or as operators or typeof argument. The following example demonstrates some of these arguments:

public MainWindow()
{
    InitializeComponent();

    // Prints ".net Brainwork rocks!" to the output window
    Console.WriteLine(ExampleMethod(23));
}

public dynamic ExampleMethod(dynamic number)
{
    dynamic dyn;

    int num = number;
    dyn = (dynamic)num;

    string str = ".net Brainwork rocks!";
    dyn = (dynamic)str;

    return dyn;
}

 

So what the dynamic type really does is telling the compiler to skip operation analysis and validation at design time, and instead, it resolves the types and operations at runtime. It accomplishes this through a new engine called Dynamic Language Runtime. The dynamic language runtime (DLR) is a new .Net Framework 4.0 runtime environment that adds a set of services for dynamic languages to the common language runtime (CLR). The DLR makes it easier to develop dynamic languages to run on the .NET Framework and to add dynamic features to statically typed languages. IronRuby and IronPython are two examples of languages developed by using the DLR.

 

Dynamic languages can identify the type of an object at run time, whereas in statically typed languages such as C# you must specify object types at design time.

 

The purpose of the DLR is to enable a system of dynamic languages to run on the .NET Framework and give them .NET interoperability. The DLR introduces dynamic objects to C# and Visual Basic in Visual Studio 2010 to support dynamic behavior in these languages and enable their interoperation with dynamic languages.

 

So, all in all, dynamic has an entirely new operation going on. It’s not just a matter of making it easier for developers to produce code or improve its quality, but it’s a new system for dynamic type handling, that improves how the language interacts with external data objects or services, whose types are only known at runtime like COM.

 

References:

Dynamic Language Runtime Overview - MSDN

 

Technorati Tags: ,

ASP.NET MVC 2 Release Candidate 2 Released

Add Comment | Feb 05, 2010

The ASP.NET team shipped ASP.NET MVC 2 Release Candidate 2 for VS 2008/.NET 3.5, and it’s the sequel to the RC version made available in December. It features bug fixes, performance optimizations, and API and behavior additions/changes.  Below are a few of the changes between the RC1 and RC2 release (read the release notes for even more details):

  • The new ASP.NET MVC 2 validation feature now performs model-validation instead of input-validation (this means that when you use model binding all model properties are validated instead of just validations on changed values of a model).  This behavior change was based on extensive feedback from the community.
  • The new strongly-typed HTML input helpers now support lambda expressions which reference array or collection indexes.  This means you can now write code like Html.EditorFor(m=>m.Orders[i]) and have it correctly output an HTML <input> element whose “name” attribute contains the index (e.g. Orders[0] for the first element), and whose “value” contains the appropriate value.
  • The new templated Html.EditorFor() and Html.DisplayFor() helper methods now auto-scaffold simple properties (and do not render complex sub-properties by default).  This makes it easier to generate automatic scaffolded forms.  I’ll be covering this support in a future blog post.
  • The “id” attribute of client-script validation message elements is now cleaner.  With RC1 they had a form0_ prefix.  Now the id value is simply the input form element name postfixed with a validationMessage string (e.g. unitPrice_validationMessage).
  • The Html.ValidationSummary() helper method now takes an optional boolean parameter which enables you to control whether only model-level validation messages are rendered by it, or whether property level validation messages are rendered as well.  This provides you with more UI customization options for how validation messages are displayed within your UI.
  • The AccountController class created with the default ASP.NET MVC Web Application project template is cleaner.
  • Visual Studio now includes scaffolding support for Delete action methods within Controllers, as well as Delete views (I always found it odd that the default T4 templates didn’t support this before).
  • jQuery 1.4.1 is now included by default with new ASP.NET MVC 2 projects, along with a –vsdoc file that provides Visual Studio documentation intellisense for it.
  • The RC2 release has some significant performance tuning improvements (for example: the lambda based strongly-typed HTML helpers are now much faster).

Today’s RC2 release only work with VS 2008 and .NET 3.5.  We’ll shortly be releasing the VS 2010 RC (which will be available for everyone to download). It will include ASP. NET MVC 2 support built-in (no separate download required).

 

You can download it here.

 

Source: ScottGu

Visual Studio 2008 SP1 WPF Designer Crash

One Comment | Jan 08, 2010

I’m at it again. Yesterday when working on a simplistic WPF form for a user profile configuration scenario, Visual Studio did it again with a bang, crashing in all it’s glory when switching to designer view.

What solved the problem? Well… Erasing the solution .suo file so that the IDE doesn’t crash on startup if, by any chance, a xaml document is open in design view, and installing the following hotfix made available by Microsoft:

 

KB963035 - VS2008 SP1 sometimes hangs irretrievably after WPF Designer

 

This hotfix relates to the Visual Studio IDE hanging, not crashing. But nevertheless It can be a valuable contribution to solving the problem.

 

Cleaning the solution also solved the problem in some cases, from what I’ve read and heard.

Design-time width and height in WPF/Silverlight

2 Comments | Dec 15, 2009

When designing UI’s in WPF and Silverlight, you may wish to make your layout fluid and auto-expandable in order to take the most out changing UI context. For instance, when data quantity and quality changes, available space may also change and objects in the layout need to adapt to these changes. When an auto layout is needed, no width nor height are specified which can be troublesome since the design view tends to collapse available space. For instance, a variable size user control with a data-binded listbox has no child items in design time, so you’ll see nothing but a small dot. This is because Cider, the Visual Studio Designer, and Blend designer, have no reason to show it otherwise since there’s no content and, as such, the ability to publish and preview isn’t possible.

 

In expression blend however, there’s a nice feature that solves this issue, and it’s only enabled at design time. When you create a new visual object in Blend, you’ll notice the following xml namespace declaration:

...
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
...

 

This enabled a “d” keyword that can be attached to a visual object in order to set its size only at design time. At runtime, the regular sizing settings are considered. So, if we had the following User Control, with data items applied at runtime through databinding:

<UserControl x:Class="NetBrainwork.Temp"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ListBox Name="DemoListBox" ItemsSource="{Binding MyCollection}"></ListBox>
</UserControl>

This is what you would see in Blend:

 

usercontrolauto

 

So you see that testing background coloring, for instance, isn’t very practical with this view. So the solution lies in the two Blend design-time attached properties: DesignWidth and DesignHeight, available in the namespace specified above. Notice the two right and bottom rectangular adorners in the previous picture: they’re Blend’s way of settings these values. If you resize your control with those rectangles you’ll be setting your desired design-time size. The XAML output would be something like this:

<UserControl x:Class="WpfUnderTheHood.Temp"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="200" d:DesignHeight="200">
    <ListBox Name="DemoListBox" ItemsSource="{Binding MyCollection}"></ListBox>
</UserControl>

And this is what you’d see:

 

usercontrolauto2

Like I said earlier, at runtime this setting isn’t considered, and auto layout is applied.

 

Technorati Tags: ,

MEF and Prism on .NET Framework 4.0

Add Comment | Dec 09, 2009

David Hill from the patterns & practices team posted a very interesting article concerning MEF and Prism. It talks about the benefits your applications get from using these extensibility technologies, but also clears some misunderstandings regarding their purpose. I’ve been faced with this question several times and the online community reflects this same common overlap. While MEF is purely an extensibility API, Prism is a development pattern that allows you to organize and manage your WPF/Silverlight project through modularity and thus isolate requirement and functionality concerns in development teams.

 

They’re both part of .NET Framework 4.0 and a MUST if you are developing big applications and/or needing your software to be extensible.

 

Check out the article here.

 

Technorati Tags: , , , ,

Microsoft CDN now with SSL support

Add Comment | Nov 30, 2009

Following September's launch of the new Microsoft CDN (Content Delivery Network Service), in which you can reference ajax libraries in cache, Microsoft recently added SSL support, thus confirming Microsoft's announcement back then. This new feature is necessary in websites that have SSL enabled pages and script library references in them. What would happen until now was a message displaying "This page contains both secure and non-secure items...".

SSL support is now enabled with the scripts hosted on the Microsoft AJAX CDN. Simply use an “https” moniker with any script references on your site that point to the CDN, and they will now be served over SSL. For example, below is how you can reference jQuery over SSL:

<script src="https://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>

 For more information, check out the Microsoft Ajax Content Delivery Network website. At the bottom there's a useful list of ASP.NET Ajax Libraries hosted on the CDN.

 

Technorati tags:

Technorati verify: 4J8KBSATVCQV

jQuery Javascript Library Tutorial Part 2

Add Comment | Nov 24, 2009

Just posted another article from a series of articles regarding the jQuery Library. This next iteration explains all the major areas of this magnificent API, and targets element selection and filtering through some basic explanation and examples. Don't miss out if you're getting into jQuery.

 

jQuery Tutorial Part 1

jQuery Tutorial Part 2

Silverlight 4 Out-Of-Browser features

Add Comment | Nov 23, 2009

Saw a great vide this weekend about Silberlight 4’s new OOB capabilities. Joe Stegman, Director of Program Management on the Silverlight team, went on camera in Channel 9 to talk about Silverlight 4's Out of Browser improvements. OOB means that you can run a Silverlight application on your desktop, outside the usual browser enviroment.

 

Check it out here: Channel 9

 

Technorati Tags:

PDC 2009: Windows 2008 in 2012

Add Comment | Nov 23, 2009

At the Professional Developers Conference 2009, Microsoft announced public beta of Office Professional Plus 2010 productivity suite. In all the fanfare, Stephen Chapman of Microsoft Kitchen managed to grab a Microsoft roadmap pointing next Windows operating system's arrival.


windows8roadmap1[1] Microsoft launched Windows 7 on October 22 and now the plans of Windows 8 roadmap were spotted on the Interwebs. The successor to Windows 7 is tentatively codenamed as Windows 8 and is expected to be next "Major Release" in 2012. On the contrary, Windows 7 is being treated as "Release Updates" - to what? Maybe the wild Vista code.


This means there will be a three year gap till next Windows Operating system and their rival Apple believes in keeping two year gap - between Leopard and Snow Leopard. In these two-three years, both OS-making giants want the consumers to get used the new technologies incorporated in their OS and be ready for new ones.


Before you start making Windows 8 features wishlist, start using Windows 7 extensively and then list down what you wish to see next. Obviously, Gamers would love to go for DirectX12 and who knows what would be incorporated in it.

 

Technorati Tags: ,