Friday, September 12, 2008

Presenting @ DevTeach

If you needed one more reason to register for DevTeach here it is. I'm giving a talk about parallel extensions. Here is the teaser:
A glimpse into the parallel universe
There is no more free lunch! The Moore's law is over. If we want more power we need to cross the processor barrier and do work in parallel. In June 2008, Microsoft released its second CTP of Parallel Extensions library. Come with me to see how easy it will be to make that leap of faith into the world of parallel processing. We will see how Task, concurrent collections, lazy initialization, parallel Linq and other tools can help us in this endeavour.

Wednesday, July 9, 2008

New SQL to XML in SQL Server 2005, no more FOR XML EXPLICIT

Did you ever try to spit out XML data from a SQL database? Of course you can use the “FOR XML AUTO” clause but it will throw you something like this:

SELECT TOP 6 ProductID, ProductName, UnitPrice
FROM Products 
FOR XML EXPLICIT
<root>
	<Products ProductID="1" ProductName="Chai" UnitPrice="18.0000" />
	<Products ProductID="2" ProductName="Chang" UnitPrice="19.0000" />
	<Products ProductID="3" ProductName="Aniseed Syrup" UnitPrice="10.0000" />
	<Products ProductID="4" ProductName="Chef Anton's Cajun Seasoning" UnitPrice="22.0000" />
	<Products ProductID="5" ProductName="Chef Anton's Gumbo Mix" UnitPrice="21.3500" />
	<Products ProductID="6" ProductName="Grandma's Boysenberry Spread" UnitPrice="25.0000" />
</root>

With “FOR XML EXPLICIT” you can have a little more control:

SELECT TOP 6 
	1 AS Tag, 
	NULL AS parent, 
	ProductID AS [Product!1!ID], 
	ProductName AS [Product!1!Name!element], 
	UnitPrice AS [Product!1!Price!element]
FROM Products 
FOR XML EXPLICIT

<root>
  <Product ID="1">
    <Name>Chai</Name>
    <Price>18.0000</Price>
  </Product>
  <Product ID="2">
    <Name>Chang</Name>
    <Price>19.0000</Price>
  </Product>
  <Product ID="3">
    <Name>Aniseed Syrup</Name>
    <Price>10.0000</Price>
  </Product>
  <Product ID="4">
    <Name>Chef Anton&apos;s Cajun Seasoning</Name>
    <Price>22.0000</Price>
  </Product>
  <Product ID="5">
    <Name>Chef Anton&apos;s Gumbo Mix</Name>
    <Price>21.3500</Price>
  </Product>
  <Product ID="6">
    <Name>Grandma&apos;s Boysenberry Spread</Name>
    <Price>25.0000</Price>
  </Product>
</root>

But this SQL syntax is a bit odd. It gets worst when you try to add multiple level in your XML output. SQL Server 2005 provides another constrct: “FOR XML PATH”. Here is the same sample:

SELECT TOP 6 
	ProductID AS [@ID], 
	ProductName AS [Name], 
	UnitPrice AS [Price]
FROM Products 
FOR XML PATH('Product')

This will produce the same output as the previous EXPLICIT sample but the syntax is much more understandable.

Monday, January 7, 2008

Volta: Redefining Web Development

Volta: Redefining Web Development

via Yet Another Language Geek by wesdyer on 12/5/07

Anyone who writes web applications knows that web development is not easy. Developers wrangle with a soup of technologies distributed across multiple tiers. We live in a world where programmers accept the fact that they need to know four or five different languages, tools, and environments just to get a site up and running. In ancient times, the Egyptians built marvelous structures despite the primitive tools that the workmen used. Building a pyramid took most of the national resources of wealth and labor. Today, we build structures which are vastly more complicated and yet require only a tiny fraction of the resources. The difference is in the tools and the infrastructure.

In a similar way, Volta significantly improves web development. Programmers write web applications using familiar .NET languages, libraries, and tools. Volta splits the application into multiple parts potentially running on different tiers, say, client and server. Client code needs only a minimal, JavaScript-enabled browser, though Volta will take advantage of additional runtimes that may be present.

Programmers simply refactor classes that need to run on tiers other than the client and Volta injects the boilerplate code for communication, serialization, synchronization -- all the remoting code. The developer enjoys all of the benefits of .NET: a great debugger, test tools, profiling, intellisense, refactorings, etc.

Just how simple is Volta? Let's write an application that uses a button to query the server for a string and displays that string to the client: the hello world web application.

image

Now, let's write the code for the web page. We need a Div for the output and an Input for the interaction. Of course, we could have constructed the page elements with HTML/CSS instead.

using System;
using Microsoft.LiveLabs.Volta.Html;
using Microsoft.LiveLabs.Volta.Xml;

namespace HelloWorld
{
    public partial class VoltaPage1 : Page
    {
        public VoltaPage1()
        {
            var output = new Div();
            var b = new Input();
            b.Type = "button";
            b.Value = "Get Message";
            b.Click += () => output.InnerHtml = C.GetMessage();
            Document.Body.AppendChild(output);
            Document.Body.AppendChild(b);
        }
    }

    internal class C
    {
        public static string GetMessage()
        {
            return "Hello, World";
        }
    }
}

But we want to produce the message on the server. Time to refactor.

image

Browser clients call the server, the "Origin", because this ensures the message will come from the same server that supplied the HTML.

[RunAtOrigin]
internal class C
{
    public static string GetMessage()
    {
        return "Hello, World";
    }
}

That is it. Try it out.

image

Now, click "Get Message".

image

Great. But is it cross-browser compatible?

image

Yes. And you can debug it.

image

You can even debug across tiers.

image

There is a lot more to Volta in the first technology preview which was made publicly available at 11 am PST today and there will be a lot more to come.

Still skeptical? Try it out for yourself.

http://labs.live.com/volta

image

Thursday, December 6, 2007

Debugging Windows Service without deploying it

Have you ever tried to build a Windows Service? Did you get it right the first time? Chances are that you had to modify it a couple of time before getting it the way you want it. Each time you had to build the installation package, uninstall the old version and reinstall the new one. Hopfully you can get the installation package to remove old version before installing new one. How did you debug it? Your only option was to start the service and attache a debugger to it. Although this is a good way to do it and you must go to that process before going into production with your service, this is way too much time consuming for the deveopment process. I invite you to take look at a simple class I built to enable any Windows services to be debug from Visual Studio using F5. Here are the step you have to follow:
  1. Build or open your own Windows Service project
  2. Change it's build setting to be a console application
  3. Add ServiceDebuggerHelper project to your solution (can be download from CodeProject)
  4. Add a reference to ServiceDebuggerHelper project in you project
  5. Modify your Program class to start your service with ServiceRunner if it is started with /debug command line argument
  6. Modify your service to be debuggable in one of two way:
    1. Implement IDebuggableService interface
    2. Inherit from DebuggableService base class
  7. Start it in debug mode

See Debugging Windows Service made easy on CodeProject for details.

Saturday, November 24, 2007

Passing anonymous to and from method's call

As posted by Alex James on Meta-Me blog.

T CastByExample(object o, T example)

So earlier today I was lamenting that an anonymous type can't be
shared between functions with
Wes Dyer, when he said "Well actually they can..."

Cue me learning something cool.

The first step is to create a seemingly innocent method:

    public static T CastByExample<T>(this object o, T example)
    {
        return (T) o;
    }
Seems innocent enough right? Well it is until you start using it with anonymous types. Imagine you had this function, that returns an anonymous type as object, because that is your only choice:
static object GetAnonymousType()
{
    return new { FullName = "Cosmo Kramer" };
}
Normally if you called this function anywhere you wouldn't be able to get at the anonymous type without using reflection... This is where CastByExample<T> comes to the rescue. If you know the shape of the anonymous type, you use that to do a CastByExample...
        object o = GetAnonymousType();

        //get the original anonymous type back again
        var v = o.CastByExample(new { FullName = "" });

        //Use the properties of the anonymous type initialized in another
        //function directly !!
        Console.WriteLine(v.FullName);

This works because when an anonymous type is used the compiler first checks that one with the same signature (i.e. all fields are the same name and type) hasn't already been used. If one has the same CLR type is used.

Hence if you pass in an example that is the same shape as the original anonymous type to the CastByExample(..) method will get you back to the original anonymous type... and var magic does the rest.

Nifty huh?

Wednesday, November 21, 2007

Quick Tip: Knowing if you are running from visual studio or not

I was trying to know if my program is running from visual studio or not. The reason for that is I want to add a pause in my console application only if it runs from visual studio. Otherwise I want it to run normally.

Here is a quick tip I found. I’m not sure if there is another way to do this but this works fine.

if (AppDomain.CurrentDomain.FriendlyName.Contains(".vshost.")) Console.ReadLine();

If you add this line in a console app, it will wait for a enter only if you un it from visual studio.

Wednesday, November 7, 2007

Thursday, November 1, 2007

Tired of stepping through properties while you debug?

If you are tired of stepping through every properties in your code while you are debugging it here are some cool things you can do



Add a DebuggerHidden attribute.



This attribute applies to Properties, Methods and Constructors. So you can do this:






Add a DebuggerStepThrough attribute.



This attribute applies to Methods, Constructors, Structs and Classes. You can use it on a single property but if you have a simple DO (Data Object) you can mark it so the debugger will step through it.


Of course you can also use the entire assembly in release mode.

Source Download: http://www.docdotnet.com/portal/LinkClick.aspx?link=TestSkipDebug.zip&tabid=129&mid=459


Have fun.