Life, Surf, Code and everything in between
White Papers | Free Tools | Products | Message Board | News |

Ads Via DevMavens

ASP.NET Connection Session Slides and Samples Posted



aspnetConnections  I’ve posted my session samples and slides from last week’s Fall 2008 DevConnection conference. As always the conference has been great fun and I’ve been pretty happy with my presentations (except maybe the REST session which was just to short with the 15 minutes that were cut due to scheduling). This time around I had fairly easy sessions that I’d done a few times before which always makes things a little easier and I think the sessions went very well. Judging from feedback it looks like they were well received too…

Oddly though this year I didn’t feel terribly social – it seems like I was stuck in my room a lot working on sessions or various other things.Even the few times I got out it seemed the atmosphere with attendees seemed a little subdued – odd for a conference at Vegas. I’m just like Scott Hanselman pointed out in that respect: I don’t gamble, don’t drink or party much, and least of all do I like the cheesy excesses of Vegas. Not much that all that has to offer me especially on repeat visits. To me conferences are about hanging out with other developers but unfortunately there seemed to be very little of that at this conference. Even so I had a few interesting discussions at a few of the parties and with a variety of attendees after sessions. Frankly I really wish conferences tried to encourage more interaction between attendees and speakers by way of their scheduling instead of the separation of events and isolation with the ‘speaker room’ that seems to be happening so much at conferences that I’ve spoken at. <shrug>

Anyway enough bantering. Here are are the download links for my sessions:

Using jQuery with ASP.NET

This set of samples includes a number of different examples starting with the completed walk through client side example that demonstrates a wide variety of client selector operations, plus the interactive selection window and word search popups. This sample also includes a basic pulse plug in (shown), plus a Selector filter extension for :containsNoCase (not shown). There are a host of simple AJAX examples that take you through HTML fragment loading with $.load() including retrieving independent content, as well as a few variations of retrieving content from the same ASP.NET page and a basic example of returning JSON data from page output (which has many applications including MVC style apps). Then there are two identical samples that use WCF with jQuery – one that uses ASP.NET Ajax Web Service callbacks and one that uses only jQuery to access the same WCF service. Finally there are three larger sample applications provided: A WCF based StockPortfolio manager that allows picking stocks and adding, editing and deleting them from a portfolio view. Sample demonstrates a raw AJAX approach with all data loaded client side. Also demonstrates using jTemplates. There’s also a PhotoAlbum application that demonstrates a host of mechanisms for displaying and manipulating image display in a variety of ways. This is a reusable app that works simply with files in a directory that can be easily imported and updated. Demonstrates a number of plug-ins including Sortable and a custom Editable plugin used to edit captions in place. Finally there’s an Amazon Book Picker application which I use for selecting books from Amazon to display in my Web log. App allows selecting books from Amazon and using a window based layout allows adding, editing and updating of content. Demonstrates a more LOB type scenario and interaction of multiple floating windows of information. Also uses John Resig’s Microtemplate Engine.

This download also includes a jControls project which includes most of the West Wind Ajax Toolkit’s controls updated with jQuery. It includes a host of AJAX based utility classes including the ClientScriptProxy, ScriptContainer, ScriptVariables and other utilities covered here on this blog as well as a native callback control (AjaxMethodCallback) to make server callbacks easily with ASP.NET AJAX, DragPanel, HoverPanel and ModalPopup server controls as well as a rich client library of jQuery plug-ins that support the server side controls (although the client controls can be used independently of the server controls).

Download jQuery Slides and Samples


Using WCF for REST and JSON Services with ASP.NET

This set of samples includes a few simple REST urls that are directly accessed via URL demonstrating different result formats including XML, JSON, RSS as well as streamed responses. There’s also a second project that uses the WCF REST Starter Kit to publish a simple collection based list of items with basic display, list, add, update and delete support. This set also includes the WCF StockPortfolio example application I showed in the jQuery session, but done mostly with pure ASP.NET Ajax (with a little help from jQuery and a few helper functions). This example again is provided in ASP.NET AJAX and raw AJAX versions (implemented via jQuery) demonstrating different approaches to the same backend service to allow comparing behavior side by side. The examples in this session are not really self-explanatory – they rely somewhat on demonstrations and manipulation of the base code especially for the REST samples.

Download WCF REST Slides and Samples

 

Dealing with Long Running Requests in ASP.NET

This set of examples demonstrates a host of different operations to deal with long running requests. These examples take a very basic scenario of returning some data and displaying it with some delay. Different approaches are used to address various issues involved with slow running requests starting with user progress/working display options, moving on to asynchronous operations to reduce request times for IO centric tasks (Async Pages with Async Operations and Async Page Tasks). Then there are a several delegate and thread scenarios addressed from run and forget operations that can run in the background without feedback, to using a background scheduler for batching operations without tying up ASP.NET threads on seperate background thread. The final example then provides a comprehensive custom messaging solution that allows client and offloaded process server to communicate progress information while processing as well as pass complex data back to the client for working with or display. The messaging solution can be run inside of ASP.NET, in a separate process (sample uses a WinForm so you can see it run, but typically a service is more likely target) and finally running on a separate machine to offload processing completely of the current machine. The samples includes full source for the remote message manager.

Download Long Running Slides and Samples

 

Thanks for all those that attended my sessions in Vegas – I hope you got something useful out of them. I appreciate the many kind words many of you had afterwards… here’s giving back a little to you and the rest of the folks that didn’t make it. Enjoy.



Wipeout



[11/15/2008: updated with the full sequence of shots]

Just got a funny picture from the Gorge from last summer that I’d thought I’d share. It’s one heck of a of a wipeout sequence of a backloop on a big day at Arlington last year in the summer:

Nice start but oops there go the feet out of the straps.
wipeout1

No feet = no good, so let’s hit the emergency exit button. Toss, toss…
wipeout2 

Hey were are you going? Come back, come back!!! Gear took off another way. Airwalking home…Wipeout3

 

I remember this day and this wipeout on a backloop in particular – the board just  got away from me as I slipped out of the footstraps. I went one way (down) and the board went another. I particularily like the last picture with the horrified look, of the gear taking off even higher.

Fun… and no wipeouts like this are generally harmless although falling out of the sky from 15 feet or so can make for a bad landing at times. I’m used to it – there are more wipeouts than landings of these :-}.

Thanks to Richard Hallman who got these shots and also another nice sequence from a different day also at Arlington that I think is also extremely awesome and so memorable of last year’s epic sessions out at Arlington. And maybe not as embarrassing as the big wipeout above.

Big waves on the Columbia River at Arlington. This is NOT the ocean!

ArlingtonWave

And yes – I’m proud to say I made that cutback and sailed away from it. 
ArlingtonWave2_small

Also a memorable day. It was blowing mid 40’s (mph) that day and it was late in the day – by the end of the day I was the only one left out on the water after a 4.5 hour session in this madhouse wind. I think that was the best day I’ve ever had in the Gorge… ah memories.

Bummer I didn’t get to see any days like this year in my short time in the Gorge. Next year.



jQuery Intellisense Updates from Microsoft



In case you missed it: Microsoft released the second part of their support tools for jQuery in Visual Studio this week. The second part comes in the form of a hotfix for Visual Studio 2008 SP1 that provides built-in support for –vsdoc.js files to – when present – automatically provide Intellisense support in Javascript documents. In combination with the recently released jQuery Intellisense file that Microsoft released you can now very easily get jQuery Intellisense in Visual Studio.

Here’s what this now looks like when accessing jQuery elements in the page:

jqueryIntellisense[1]

Note that you get Intellisense all the way down the chain – ie. not just on the first element. Notice also that plug-ins (draggable in this case) shows up as well in the list of ‘attached’ jQuery wrapped set functions.

Just as useful is Intellisense on individual members:

jQueryIntellisense2

with all parameters and some fairly detailed information provided on methods, and their parameters although there are a few functions that have missing parameters (all the before/after/append/appendto/prepend/prependTo etc.).

Notice also that the jQuery parameter stored into an instance variable is also recognized as a jQuery instance and so Intellisense works on the win variable as well as an explicit $() function.

What you need to use jQuery Intellisense effectively

Here are all the things that you need to get the best jQuery support in the VS 2008 editor:

  • Visual Studio 2008 SP1
    SP1 provides a number of enhancements to the Javascript Intellisense engine in Visual Studio and is the basis on which all the other enhancements run on. These improvements include faster parsing and some built-in knowledge of a few common Javascript libraries including jQuery to find and provide at least basic Intellisense support.

  • jQuery Intellisense document file (jQuery-1.2.6-vsdoc.js)
    This .js file is an annotated version of the base jQuery.js that includes detailed Visual Studio style Intellisense information about each of jQuery’s functions and utilities. None of the code is changed – only comments have been added. This file isn’t meant to replace your original jQuery.js file, but rather to serve as a more complete Intellisense file during development. Not meant for production.

  • VS 2008 SP1 Hotfix for –vsdoc.js Intellisense (KB958502)
    This hotfix enables any file with –vsdoc.js extension to act as an Intellisense background file for any referenced .js file. So if you have a script src referencing jquery.js all you have to do is have the jquery-vsdoc.js in the same folder as jquery.js and Visual Studio will automatically find and use the –vsdoc file for Intellisense. This works for jQuery with the above mentioned files as well as your own files.

This is all very nice and it works great.

I’ve talked about Intellisense some time back and I basically did something similar to my copy of jquery.js by providing a few key functions with Intellisense markup. So then when referencing the .js file I’d get the same Intellisense. With this support provided in Visual Studio it’s now become a lot easier to provide documentation in an external file simply by having a –vsdoc.js file for the same file.

The way this works is that Visual Studio first looks for the –vsdoc.js file, then a debug.js file and finally for the file you actually specified in the source.

In Practice

This works in basic script includes in the <head> tag or inside of the document inside of ASPX pages:

<head runat="server">
    <title>jQuery 101</title>   
    <script src="scripts/jquery.js" type="text/javascript"></script>
</head>

in script manager code:

<asp:ScriptManager ID="ScriptManager" runat="server">
    <Services>
        <asp:ServiceReference Path="~/MsAjax/MsAjaxStockService.svc" />            
    </Services>
    <Scripts>
        <asp:ScriptReference Path="~/scripts/jquery.js" />
    </Scripts>
</asp:ScriptManager>

or in script source files:

/// <reference path="~/scripts/jquery.js" />
/// <reference path="~/scripts/ww.jquery.js" />

Note that I prefer NOT to use the version number with my jQuery.js file to avoid later update renaming issues so my Intellisense file is actually jquery-vsdoc.js. Match whatever your main file name is version nunmber or not. Here’s what this looks like in a project in Visual Studio for me:

jqueryIntellisense

Note that I also have a jquery.min-vsdoc.js file in case I decide to embed the minimized version. Visual Studio is not checking for .min.js files and mapping them to the plain –vsdoc.js file – yet. So the following also works, but only if jquery.min-vsdoc.js is provided.

<head runat="server">
    <title>jQuery 101</title>   
    <script src="scripts/jquery.min.js" type="text/javascript"></script>
</head>

I spoke with Jeff King last week at PDC and there’s a chance that support for .min.js files might be added in the future natively. But even having the extra file should be no big deal because they are not used at runtime anyway so they don’t need to be deployed.

The Intellisense file and the hotfix in combination are a welcome addition to Visual Studio – it makes working with jQuery inside of the VS Javascript editor a lot easier because the documentation provided through the Intellisense interface is pretty rich and helpful in many situations.

Check it out if you haven’t already.

The Release/Debug Problem for Javascript

Although jQuery is specifically targeted here – this mechanism works with any .js file, so if you want to create Intellisense script for your own javascript files you can also store them externally. The downside is that maintaining comments in an external javascript code file is pretty much a maintenance nightmare – Intellisense currently needs to have a running Javascript file in order to interpret the comments. A better solution for this is eventually needed or else some built in tools that can strip comments automatically.

I’m doing the latter now through a custom script manager that minimizes and zips output, but really this should just not be necessary. ScriptManager does something similar but even it requires both debug and release scripts. Either way these  approraches work well only if you use resources or otherwise have ASP.NET serve the scripts rather than letting IIS optimize and do it. It’d be really nice if IIS natively could somehow participate in this process and provide minimization. I know I can do this with some ASP.NET code in a module myself, but IIS is already extremely optimized for the compression scenarios where it builds temporary files that are served with compressed data. This is something that ultimately would be nice to have native support for so developers can set a switch and not have to think about whether you’re working with release or ‘debug’ versions of files.



From ASP Stock Projects to Web Application Projects in VS 2008



As I’m working on some of my demos for DevConnections next week I decided that some older demos I’ve had should be moved to Web Application Projects (WAP) rather than running on stock projects for consistency. I tend to use only WAP projects in production, but for demos I’ve often stuck with stock projects because they worked with Visual Web Developer Express. Now that SP1 has shipped though even VWD supports WAP projects (a big thanks for the ASP.NET team to making that happen!) and so there’s no need to use the stock project model any longer.

Other than consistency with other projects, there was also the issue of WCF Web Services behaving badly under stock projects. For whatever reason I still can’t figure out with stock projects WCF Services often simply stop working with a compilation error even though there have been no code changes. It appears the problem is related to the temporary file storage in the ASP.NET temp folder with copies of the App_Code assemblies getting out of sync with the service in some  way. There’s a slight fix for this in stock projects by turning batch compilation off:

<?xml version="1.0"?>
<configuration>
    <system.web>
        <compilation debug="true" batch="false">
        </compilation
>
    </system.web>

</configuration>

This has helped make the problem less frequent, but it still occurs from time to time.  Using Web Application Projects this is not an issue because the full compile causes everything to be compiled into the same assembly so there’s no real chance of a version mismatch.

Anyway, so I decided I need to move this rather large demo project that contains many diverse examples from a stock to WAP project and as it turns out it took quite a bit of work to do it.

Converting from Stock to WAP in VS 2008

Prior to VS 2008 stock projects had an option to allow you to convert projects to WAP projects via a menu option – Convert to Web Application Project. That option is no longer available so the process has become fully manual.

Here are the steps that you should go through followed by a few gotchas you need to watch out for:

  • Open your Stock project in a Visual Studio Solution
  • Add a new Web Application Project to the Solution
  • Drag and Drop all files from the Stock project into the WAP Project
  • Remove the BIN folder (or don’t copy it in the first place)
  • Manually add back ALL assembly/project references to the WAP Project
    It’s best to do this first to make sure that related files are found and any auto-compiled components (like Linq to SQL models or Web References) get properly properly translated when files are converted in the next step.
  • Right click on the root node of the project and Convert To Web Application
  • Rename APP_Code to something else if the conversion didn’t do it for you. I use Classes as my ‘code only’ folder
    APP_Code is an illegal folder name for a WAP project – if you don’t rename you’d end up with duplicate compiled classes which will cause problems. Ideally you shouldn’t have much code here – it’s best to put this sort of stuff into a separate project. Note that you can move any CodeBehind files (like Web Service, Handler or other ‘known ASP.NET files’) back into the actual content folders – no need to keep them in the App_Code/Classes folder since they’re easier to find with the actual content files.
    Note that Convert to Web Application should rename the APP_CODE folder and it did the first time I did this. Just now when I walked through this one more time to verify for writing it up however it didn’t create an OLD_APP_CODE folder. Further I couldn’t rename the folder at first. I ended up explicitly running Convert to Web Application on the old APP_CODE folder. After that I was able to rename it.

  • IMPORTANT: Select all files that were in the old APP_CODE folder and set their Build Action to Compile.
    Files in the APP_CODE folder in stock projects are compiled by ASP.NET rather than Visual Studio so the default action for APP_CODE files in stock projects is content which doesn’t compile. When moved to a WAP project that same action is passed forward which means that Visual Studio is not compiling APP_CODE files by default. Make sure you switch the Build Action to Compile or else you’ll end up not finding the classes when compiling your project. This one threw me for a loop because I got compiler errors pointing at classes that I knew were in the project.
  • Compile your WAP Project – rinse wash and retry
    There are bound to be a few compilation errors in your project when you do this. Pay especial attention to missing classes (check that the classes in question are set to Compile not as Content) or missing namespaces (make sure that classes other than pages have a namespace). Also make sure that you have all necessary assemblies referenced. You’ll likely get a bunch of errors and go through this cycle of compiling fixing and a host of errors until all references and classes are properly added. Read the next two bullets for a few more hints on possible compilation issues.
  • Remove and Reattach Web and Service References 
    The Conversion imports Web and Service references but it attaches them into a separate folder. For me the Web References didn’t work correctly so instead I ended up removing them and re-adding them using the standard Web Reference/Service Reference tools. Note that in WAP this will cause Namespace changes – from only the name you assigned in Stock projects to the Project’s default namespace + the name you assigned. IOW, you’ll have a little bit of refactoring to do to fix these services.
  • Linq To Sql Models
    My project had two Linq to SQL models and while both of the models transferred, they didn’t immediately compile. The problem is that the old designer file is detached and a new attached and somewhere in that process the actual class is not visible so that any reference to the model fails.

    LinqToSql
    The solution for me was to go delete both of the code behind files (the .designer files) both in the project and from disk. Then going into the model and resaving it. You’ll end up with the TimeTrakker1.designer.cs file which is odd, but works properly. The key is to make sure that the original file is deleted and removed.

So as you can see doing a WAP conversion is by no means a trivial task. I ended up spending a good two hours converting this project by the time I had everything back up and running as it did before. And that’s having done this before and having some idea what to look out for. The hardest part for me were the auto-building files – Web Services and the LINQ to SQL Models that just didn’t want to work after initial conversion.

Part of the problem was that the compiler wasn’t being helpful. I had the L2S model in the project for example with proper namespaces set on the model, yet the compiler failed to see the actual model’s classes. No error during model generation – I could see the classes in the model’s codebehind but the compiler failed to see the classes. When this happens the thing to check is to make sure that the Build Action is set properly and/or that there isn’t another file that is interfering with the namespace resolution.

The good news is that the issues are all related to file location/switch settings and once those were resolved the project just kicked right in and worked as expected. No further tweaking required. Personally I much prefer WAP’s more formal compilation approach and the reliability that comes from having a single code behind assembly that knows where are code behind code lives in one place instead of scattered through multiple temp assemblies as is the case with Stock projects.

I have a couple of other projects that I should do this to as well – not sure if I’m willing to put in the time again though. <shrug>



Debugging a WCF REST/AJAX Serialization Problem



I’ve been working on my DevConnections demos today and as I was going through my demos I ended up significantly reworking a few of the samples and moving around projects. This actually went fairly smoothly but when I got to my AJAX StockPortfolio sample that mixes jQuery and WCF I got into a nasty mess of a problem that took me a good two hours to resolve. It turns out it was a typical problem of operator error on my part, but this still is one to write up and make others aware of.

My sample project consists of a WCF AJAX service that uses ASP.NET AJAX semantics for access from the client. This means the service is configured like this at the service level in the .SVC file:

<%@ ServiceHost Language="C#" Debug="true"
                Service="WcfAjax.JsonStockService"                
                Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"                                  
%>

This tells WCF to publish the service by default using similar semantics as the old ASMX style services. Note the Factory attribute which is set to WebScriptServiceHostFactory which provides a pre-configured factory for typical MS AJAX style behavior. This factory means that all requests by default use WebInvoke() attributes with POST as the method to send data to the server and using wrapped message format that the ASP.NET AJAX client expects messages to be sent and recieved with. Using the factory makes the process very easy as you don’t have to create any web.config entries to configure the service although you can still do that to override particular settings.

With this done setting up the service is as easy as creating a class and plopping the [ServiceContract] onto it. Since this is an in-application Ajax service only I’m defining both the implementation and contract on a single class rather than seperate service contract interface and implementation class:

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    [ServiceContract]
    public class JsonStockService 
    {
        private StockServer Stocks = new StockServer();
 
        private User User = null;
 
        [OperationContract]
        public StockQuote GetStockQuote(string symbol)
        {
            return Stocks.GetStockQuote(symbol);
        } 
 
        /// <summary>
        /// Returns a full list of items to the caller in the Items
        /// property for the PortfolioMessage
        /// </summary>
        /// <param name="userToken"></param>
        /// <returns></returns>
        [OperationContract]                
        public PortfolioMessage GetPortfolioItems(string userToken)
        {
            // Validate and get this.User instance
            this.ValidateToken(userToken);
 
            busPortfolioItem portfolioItem = new busPortfolioItem();
 
            IQueryable<PortfolioItem> items = portfolioItem.GetItemsForUser(this.User.Pk);
 
            PortfolioMessage msg = new PortfolioMessage
            {
                TotalValue = portfolioItem.GetPortfolioTotalValue(this.User.Pk),
                TotalItems = portfolioItem.GetPortfolioItemCount(this.User.Pk),
                Items = items.ToList()
            };
 
            return msg;
        }
 
 
    }
 
    /// <summary>
    /// Message Item sent to client. Contains Portfolio detail
    /// about a single item or complete portfolio
    /// </summary>
    [DataContract]
    public class PortfolioMessage
    {
        [DataMember]
        public decimal TotalValue { get; set; }
 
        [DataMember]
        public int TotalItems { get; set; }
 
        [DataMember]
        public PortfolioItem SingleItem { get; set; }
 
        [DataMember]
        public List<PortfolioItem> Items { get; set; }
 
    }

My service has about 20 different methods of varying complexity, but I ran into a problem with the GetPortfolioItems() method. When called from the service my client would receive an empty response from the server. No data returned, but no error either. No headers, no data – just a null response. This is a bit difficult to debug as you might expect and this is what ended up taking me 2 hours to figure out.

So what can I do to figure out what the problem is? As you can see in the method  above the result returned is a complex object. It’s basically a result message that contains a few pieces of information and all portfolio related method results all return this message object. The message object either has a single entity or a list of entites set along with a coupe of subtotals that require less code on the client.

First debugging step: Set a break point in the method and surprise, surprise the method actually completes properly. The code in the service fires to the server, but the the response still bombs with no result data. I checked the msg in the debugger and it’s fine no issues there.

So the problem most likely has to do with serialization. First to verify that the method is properly routed and working I returned null and that made it correctly all the way back to the client. No issue there. I then changed the methods WCF attributes to try and access it through a plain HttpGet:

[OperationContract]       
[WebInvoke(Method="Get")] public PortfolioMessage GetPortfolioItems(string userToken)

which should allow me to access the request directly in the browser with a url like

http://localhost/jquery/stockportfolio/StockService.svc/GetPorfolioItems?userToken=2322121321

Here’s the unfortunate result:

Connection Interrupted

The result is an incomplete page – or to be exact a page that didn’t return any output. It seems browsers handle this inconsistently. In FireFox I saw the above page as well as a browser based “Resource Could not be Found” error. Regardless, the bummer on this is that you get nothing back. No error, no message of failure – just no response at all.

In experimenting around with the problem method it seemed clear that the problem is with serialization. The question is just what. Next step for me was to enable WCF tracing and tracking errors. To enable tracing you can use something like the following:


<system.serviceModel>
  <diagnostics>
     <messageLogging logEntireMessage="true" logMalformedMessages="true"
       logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true"
       maxMessagesToLog="3000" maxSizeOfMessageToLog="10000" />
  </diagnostics>   
</system.serviceModel>
<system.diagnostics>
  <sources>
    <source name="System.ServiceModel" switchValue="Error,ActivityTracing"
     propagateActivity="true">
      <listeners>
        <add type="System.Diagnostics.DefaultTraceListener" name="Default">
          <filter type="" />
        </add>
        <add initializeData="c:\projects2008\articles\jquery\traces.xml"
         type="System.Diagnostics.XmlWriterTraceListener" name="messages">
          <filter type="" />
        </add>
      </listeners>
    </source>
  </sources>
</system.diagnostics>

You can also use the WCF Configuration Tool which makes this process a little easier at least once you’ve set up the basic diagnostics settings. You can find this tool in Visual Studio off  Tools | WCF Configuration and then specifying the .config file.

WcfConfiguration

You’ll probably want to just log errors which is by setting the switchValue to log just errors – otherwise the log will become very verbose with entries that’s difficult to read.

Looking at the logs turned out to be not very helpful other than confirming what I already knew: WCF was unable to serialize my message structure. I got a full stack trace of the error on the server, but no indication of what actually went wrong. Also there’s no indication on why there’s no error response to the client. It looks like WCF just quit processing the message after the serialization failure and doing an immediate output return.

Not very helpful.

At the beginning of this post I mentioned that the problem was operator error. I played around with different results from my message structure. Returning null, returning an empty message structure all of that worked. But as soon as I returned any entity data from my Linq To SQL model the failure would kick back in. It turns out when I copied the model somehow I failed to set the serialization options on the Linq to Sql Model:

L2SSerialization

Somewhere in the copy process or maybe when mucking around with the model I failed to set the serialization options for the generated entities. Setting the switch to Unidirectional turns on the [DataContract] and [DataMember] attributes on the generated properties of the generated model:

    [Table(Name="dbo.PortfolioItems")]
    [DataContract()]
    public partial class PortfolioItem : INotifyPropertyChanging, INotifyPropertyChanged
    {
 
        private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
 
        private int _Pk;
 
 
        public PortfolioItem()
        {
            this.Initialize();
        }
 
        [Column(Storage="_Pk", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true, UpdateCheck=UpdateCheck.Never)]
        [DataMember(Order=1)]
        public int Pk
        {
            get
            {
                return this._Pk;
            }
            set
            {
                if ((this._Pk != value))
                {
                    this.OnPkChanging(value);
                    this.SendPropertyChanging();
                    this._Pk = value;
                    this.SendPropertyChanged("Pk");
                    this.OnPkChanged();
                }
            }
        }
   }

Without this option the entities are not serializable. This makes perfect sense. Flog the developer for the oversight :-}.

However, what doesn’t make perfect sense is why WCF is failing so gracelessly in this scenario. Other exceptions such as user thrown exceptions in the code or errors in plain code actually return a proper error response to the client, but the serialization error does not.



My PDC 2008 Wrap up



Ugh, I’m finally back and settled from a long 3 week road trip. Ah, it feels good to sleep in my own bed again :-}. PDC last week was the last stop of the trip and as always it’s been an interesting experience. More than anything events like these are great to catch up with other developers and meeting people I’ve only seen virtually. I made face contact with a bunch of folks I only know through Twitter or comments on this blog. It’s always great to match a face with the message.

As usual PDC as an event was full of new technology and announcements. To me this PDC felt maybe a bit less revolutionary than previous ones in that a lot of the content covered has been previously announced or is even already in some sort of pre-release state available to the public. And that’s actually a good thing – I think it symptomizes Microsoft’s more open approach in product development which solicits early feedback and is generally good for all parties involved. For me this also worked out rather well – in the last half a year or so I’ve been pretty slow to play with new technology because I’ve been swamped with work and frankly have gotten a little tired of playing with the latest and greatest that’s not quite ‘there’ yet. A lot of stuff that’s been coming out of Microsoft in the last couple of years has been less than stellar and so for me the last year’s been mostly about getting back to the basics and working on core development rather than trying to gape at the latest and greatest.

Usually when I go to conferences I’m either speaking or otherwise involved in some official capacity that I rarely have time to attend sessions. This time around though – being purely an attendee – I had lots of time to go see sessions which has been great fun and even though I spent most days in sessions I wish I could have made to more sessions. Luckily most of the PDC sessions are actually online although finding them can be a little tricky. I’ve been making good use of this and catching up on additional content I missed.

PDC Impressions

So here are a few impressions from some of the sessions and keynotes I went to:

Windows 7
The second day’s keynote started off with preview of Windows 7 and well frankly it was a pretty bland presentation and show of new stuff. And yeah it wasn’t just me going by audience response and feedback. Bland yes, but maybe that’s a good thing. I’m starting to get into the mindset that less is more for Microsoft – the more Microsoft can focus on fixing/optimizing existing features across its products I think the better of we as users will be. I’d like to really see Microsoft focus more on fixing what’s there rather than keep building more and more crap nobody really needs.

As such the changes shown were fairly minor. The ones that stood out to me were native support for the VHD file format (used for Virtual PC Images) both for directly mounting virtual drives as well as booting from them. The remote desktop stretching over dual screens is useful too as is the better taskbar management. There will also be multi-touch support which will be cool if hardware vendors actually implement this stuff. Given how badly the tablet platform has fared over the years (no thanks to Microsoft’s inability to market this platform properly) I’m not having high hopes. I do think though that having touch support in screens would be pretty cool. Recently had a chance to play with an HP TouchSmart machine and it sure is nice to be able to work with the OS via touch in some situations.

Maybe the biggest news is that Microsoft is apparently doing some work to make Windows 7 run on less hardware (they showed a sub-notebook running with 1 gig of RAM running well with Windows 7) and improving core performance and startup. To me I don’t really care about anything else but a stable base platform, so if nothing else gets done but make sure that Windows 7 runs fast, stable and maybe has an Explorer that doesn’t crash and copies at a snail’s pace it’ll be a win.

All that said I think Microsoft’s goal needs to be polish the seriously tarnished image of Windows as a resource hog as well as the public image imposed by of all thing Apple marketing.

I personally don’t have any serious issues with Vista now, but I surely would like the resource usage in general to decrease. There are still too many times when even my dual core 2.4ghz machine is dragging because the OS performing some task in the background.

Windows Azure

Windows Azure was the big announcement at PDC this year. Azure is meant to be new hosted platform that provides scalable hosting of Web based applications. Azure can host traditional ASP.NET content as well as providing a host of service based components that are managed on the remote platform such as a blob and data storage mechanism, that effectively amounts to a remote file system. It also includes back ground worker processes that can be easily queued for background tasks, which is an interesting concept.

The big sell of the platform is the scalability – Azure runs on a load balanced platform that makes it easy to add new instances of applications to scale out easily and without having to reinstall and reconfigure anything. Azure effectively provides for packaging and replication of an application via what is called Fabrik Controller. This packaging also allows for smooth updates to staging and deployment servers which lets you push up a deployed app to a staging site, test it and then hot swap it across all configured instances.

A lot of what Azure does is pretty cool in concept. Certainly the packaged scalability is a nice and the main talking point of discussion I found with several other developers – if you’ve ever built applications that needed to span multiple machines you know what a pain it can be to scale out and make sure everything gets configured properly across machines.

However, in discussing with various developers I still am not sure just how applicable this sort of environment will be. Basically it’s NOT a typical ASP.NET hosting scenario where you can just park existing applications. The services deployed to Azure need to use the Azure data services (Sql Data Services?) for storage rather than a more traditional database. This means existing applications will have to be redesigned. The Azure data storage mechanism isn’t a SQL like store – rather the data mechanism is more like an object database that works by modeling types which get translated into single record entities in the database (like a reverse OR/M modeling wizard). It’s an interesting concept – you end up modeling your data as classes entirely and having the platform generate the data structures for you in the storage backend. The data stores are also accessible directly via REST endpoints so that any data stored is both application local and globally accessible which is also nice.  The format currently looks more like a ISAM type engine rather than a relational engine – there are no implicit relationships supported although nothing stops you from referencing foreign keys explicitly. The bottom line is that you have to work with the provided data model.

One concern I see is that servers deployed today often do more than just act as Web application servers. You may have background services running and code may be interacting with a wide variety of technologies outside of the basic Web application’s scope. All of this doesn’t look like it’s going to be supported on Azure at the moment. As it stands it looks like a platform for very specialized Web applications and that can buy into the very specific data format supported by Azure. I’m not clear how the data access actually works in applications – it looks like the access is all over HTTP and REST points, but I wonder if the data access on the live deployed server may be more direct? It’s hard to tell. Certainly I can’t imagine HTTP based data access scaling very well.

Overall the concept sounds intriguing, but I’m not sold on this idea. How many IT shops would really like to give up this much control over their platform and give it to Microsoft with the potential ability to control the pricing of the service? You’d be forever a slave to Microsoft’s pricing policies. Also how many sites actually need massive scalability? With the high end hardware available today it’s much less likely than it once was

The key thing that popped into my mind as I was watching the keynote and demos was: Lock in. Short of existing Microsoft fan boys I think this platform would be an extremely tough sell for Microsoft given that it’s based on all proprietary black box technology running on the server…

Keynotes in General

The keynotes in general were pretty dry and uninspired. Some of the speakers where painful to watch as they laughed at their own jokes – alone. There were a few highlights but most of the presenters where just spewing undefined marketing terms one after another. At a developer conference I found this pretty disappointing. Most of the presenters just sounded way too over-rehearsed. Heck even Scott Gu seemed to be just rushing through his presentation at a million miles and hour and his was probably the highlight of all the keynotes I saw. Don Box and Chris Anderson were entertaining but I’m not sure if this is a good ‘keynote’ presentation format either. It ended up feeling like filler more than anything. Hey but at least we got to see some code in their presentation (which without more context wasn’t all that useful either).

Honestly for a developer conference I would have hoped to have seen more detail on technologies. For all the time that was spent on Azure on the first day’s keynote I walked out of the room thing WTF does this really mean? It wasn’t until I went to the two follow up sessions that I had a reasonable idea.Not a good start to an idea. You know what they say “If you can’t define what it does in a few words…”

Luckily though the actual sessions were much better. This year I thought that the presentation skills of presenters were much better than in past MS conferences. A lot of presentations I saw were extremely professionally  presented with lots of time for questions at the end which is in stark contrast to the last PDC where there was lots of good content but often bad presentations.

C# and Dynamic Language Enhancements

Went to Ander’s Future of C# session and as expected C#’s main improvements are going to be based around dynamic language support. In the next version of .NET C# (and VB.NET) are going to provide services sitting ontop of the Dynamic Language Runtime (DLR). For C# the big improvement will be a ‘statically typed dynamic’ keyword. In essence the dynamic keyword allows assigning any type of variable and ‘dynamically’ access the members of that type as long as the members exist without having to hold a fully cast reference. What this means that you can access an object of type object and access its members directly without having to wade through Reflection.

The services that provide this functionality is calling on the DLR to provide the dynamic member resolving rather than direct calls to Reflection. The apparent advantages of this approach are that there can be better error handling, and improved performance through caching of Reflection type information as types are addressed. This functionality will be useful for any type of user provided code (plug-ins and the like) and especially useful for things like Interop with COM. Special attention has been given to the COM interop scenario since dynamic invokation there works a bit differently than for native .NET types. One additional improvement there is that the type hierarchy can be dynamically generated so no COM Interop Assembly needs to be generated.

VB folks must be snickering just about now because most of what the dynamic keyword functionality has been available for some time by using Option Expiclit Off, but still I think this is highly useful to get this capability in C# as well.

There’s also an IDynamicObject interface and an abstract DynamicObject class that can be implemented by developers to create objects that simulate Expando properties in C#. In effect this provides the ‘method missing’ functionality through explicitly implementing a class and intercepting all member access including access to members that don’t exist and returning a result that is effective conjured up out of thin air. The example Anders used was to implement a dictionary that uses properties instead of indexers to get and set values which is a little contrived but illustrates the point well. This can be quite useful in some situations.

I’ve talked to quite a few people who took the “So what?” attitude on this stuff. I can see why: the dynamic keyword should be something sparingly used in day to day code that you write in applications. However, if you build framework level code, scripting related code or do a lot of COM interop (all of which are still heavily in my operational domain) having an easy way to invoke non-typed members can be extremely convenient and make code much more readable than having to wade through the related thicket of Reflection commands. For me personally this will make life a lot easier in a few applications and API interfaces.

Another related feature is that C# will finally support optional parameters by allowing to assign default values to parameters. C# traditionally accomplishes this by way of method overloads and using named parameters should make it easier to reduce method clutter. Be curious to see how this works out for Reflection parsing.

Finally there was also a demo for the compiler as a service that should make it much easier and transparent to compile and execute code on the fly. It basically brings Eval() type functionality to C# in a simple manner. Anders then proceeded to build a Command Line interpreter (can you say FoxPro Command Window? <s>) that basically let him build up all sorts of constructs – classes, forms, ui, data access etc. – completely on the fly and executing that code.

Again this is special case technology, but it’s surprising how often it’s useful to be able to dynamically execute a stub of dynamically provided code. Doing things like meta driven validation rules using full or again building up scripting code are primary targets for me personally. This feature is also important to me as I have several scripting services I have built for internal apps and while I have this working now the code that does it is pretty messy and very resource intensive because effectively the compiler fires up out of process to compile the code. Having the compiler in .NET and available as a service that can be called should make this process much easier.

Unfortunately the compiler as a service is planned for C# 5.0 rather than the next release – bummer on that!

All of this dynamic stuff is very nice, but for most folks this is borderline functionality that will be useful only at times. However I think this stuff will be more important as you start looking at the whole language abstraction concepts that Anders also talks about in his talk – abstraction into things like LINQ and Lamda expressions that express intent instead of actual code. In many of those situations strongly typed values can get in the way because it’s not always clear exactly what results are typed as when the results return. For example, being able to return results like a list of anonymous types from a LINQ query gets a heck of a lot easier if you can access those values as dynamic as oppposed to having to use Reflection.

I also went to Jim Hugunin’s Dynamic Language talk which followed Anders talk to get a little more background on what exactly goes on in the DLR. I’ve been meaning to dig into the DLR precisely because I’ve had the needs to access dynamic content as mentioned earlier but I just haven’t gotten to it to date. It’s an excellent talk and I encourage folks to check it out either online or whenever you have a chance to see Jim present to give some background to those new features coming in the main .NET languages.

ASP.NET

I also spent a bit of time attending ASP.NET sessions just to keep up with all that’s changing. Nothing terribly new here because the ASP.NET team’s been very busy pushing out just about everything to the Web.

The highlights of ASP.NET features that I’m excited about:

Client ID Improvements
In ASP.NET 4.0 there will be a mechanism to specify a client id explicitly to avoid the Container based ID mangling that’s currently occurring. If you’re writing JavaScript code today you know that this is a big problem if you have any sort of content containers like Master Pages that mangle the ClientIDs when rendering.

In ASP.NET 4.0 the ClientID is a writable property and in combination with the ClientIDMode determines how the control is rendered. The following always renders a client id of txtName even if rendered inside of a naming container:

<asp:TextBox runat="server" ID="txtName" 
            ClientID="txtName"             ClientIDMode="Static">
</asp:TextBox>

The ClientIDMode supports a few settings including Static which forces the name you provided, Preditable which is a fully qualified naming container style name based on the clientID (ie. the path is still there but your name is used), Legacy and Inherit. Inherit appears to not work currently but I suspect that it will let you inherit the ClientIDMode from the nearest container control.

There’s also supposed to be a mechanism to allow for predictable names for template controls, but I haven’t seen an example of this.

This is encouraging, although I still hope that this will get simpler. If I understand this correctly the current behavior would require 3 attributes to be set in order to get a simple ClientID – ID, ClientID and ClientIDMode. I much rather would like to specify the ClientIDMode on a container and push it into the child controls and specify just an ID overriding only when necesary ( on things like list controls ).

ASP.NET Ajax Focus on Client Centric Javascript

Several talks at PDC talked about Javascript related enhancements and there are a few to mention. As you know if you read this blog I’ve been less than enthusiastic about ASP.NET AJAX mainly because of its server heavy focus. That’s changing a bit with the next version with Microsoft investing in a nice set of client side features.

The most prominent and amply demo’d feature are client side templates which lean themselves to a data centric AJAX approach where you load and render your data primarily on the client rather than using server rendering to draw the initial page and updating the page with HTML content retrieved from the server which is the premise on which most of ASP.NET AJAX has been based.

Client Templates bring a client side binding mechanism along with a client list control that makes it much easier to render content once on the client. The template binding syntax looks a bit like WPF binding semantics so it should be familiar if you’ve played with WPF or Silverlight.

In addition to templates there’s also the concept of Live Bindings which provides real time databinding to data – when the data changes it automatically updates anything that is bound to the data. This stuff relies on some of the object semantics of the MS AJAX client libraries, but regardless the client side stuff is purely client side which is nice.

The new client side features look fairly clean and not overengineered at least at first glance which is nice, but they do still rely on the core MS Ajax libraries so library size will continue to be an issue. The concepts interestingly enough look somewhat similar to the former XmlScript stuff from the Atlas days (and now buried in the Futures releases), but with a more friendly interface based on raw text based markup with JavaScript script embedding. The script language used is JavaScript so all expressions and operations eval JavaScript which seems like a sensible way to get flexibility with a familiar model that doesn’t require learning yet another markup/template language.

For me personally I’m glad to see Microsoft provide this functionality although I’ve been doing similar stuff for some time with just jQuery and some helpers. I’ve been using jTemplates with jQUery for some time and recently switched over to John Resig’s MicroTemplating engine which provides basic templating. No doubt the ASP.NET Ajax templating is richer and provides support for direct databinding where with the templates you have a little more work to do, but not much. The live binding stuff is really cool and useful and I don’t think that this sort of thing can be implemented without the sort of infrastructure (property get/set routines that intercept assignments) that MS Ajax provides, so that part at least would be difficult to do outside of ASP.NET AJAX I suspect.

Also went to Stephen Walther’s jQuery session which was well presented, although a bit on the basic side at least from the jQuery end (and not just for me <s>). Stephen’s approach was more of interoperating of ASP.NET AJAX and jQuery rather than using jQuery by itself and a lot of demos were using functionality that could have easily been done natively in jQuery, but that is somewhat expected at a Microsoft event. Stephen’s session also highlighted the ASP.NET 4.0 Client Templates extensively which was nice to see as well. It’ll be interesting to see what Microsoft will do with jQuery in ASP.NET and whether it will be used internally in the framework or whether it’ll just be an external component for developers that is supported. I suspect the selector engine would be extremely useful to the core engine.

Not sure if this is enough to lure me back to ASP.NET AJAX but I do think they ASP.NET AJAX team is heading in the right direction here. Obviously a lot can be done here with the platform that’s slowly coming together tying both the client side and server side data services together. But still, I just can’t get excited about the static control approach that the framework uses where everything has to be declared up front (ie. controls can be $create() once only) and the complexity of the client control model. I’m starting to see where this is having some benefits now with the smart binding stuff taking advantage of the notification events, but I’m still not sure if it’s worth the bother. We shall see where it goes from here…

Other stuff

I went to a few other sessions as well a few which were a dud for me. Attended the Oslo session which is about domain language creation and while that was interesting to some degree I fail to see where this is going to a be a good fit except for tool and language vendors. This looks to be a way to map domain specific objects to custom sytnax and designers that can then execute and operate on those classes. While that’s interesting in concept I don’t see that as something I can see myself using a lot of. It’s like WorkFlow – it looks great on paper until you actually sit down and try to conform yourself to the limited development options that this environment allows you. I suspect there are good use cases but I also suspect they are aimed at language and tool vendors rather than average developers. The last thing I want to see is a new inconsistent languages/syntaxes being created by developers…

But the most productive time of the conference I probably spent in discussions with other developers or some of the devs at Microsoft. These more personal discussions are really the reason to go to these types of conferences for me so you can get multiple points of view and the occasional Aha moment that escaped me perhaps during a session.

If you didn’t go to PDC you missed out on the personal interaction, but all the content for sessions is online and the quality of the presentations (using Silverlight) is excellent. I’ve been busy watching quite a few additional sessions that I missed due to conflicts or simply spaced on. This is great to have online.



Amazon Kindle – a few Things I would improve



Scott Watermasysk has a blog entry about a few things he thinks could be done to improve his Amazon Kindle. Like him I love this device and I’m ever so glad I bought it because I’ve been travelling a lot this year and always having something to read along in a small package has become a must have affair.

Wireless AccessScott makes some good points about getting the Kindle into more people’s hands and he points mainly at some marketing issues and improvements to make the Kindle a better value. Indeed the point about Kindle books often being only marginally cheaper than paper books irks me at times pretty badly given that it must be significantly cheaper for publishers – and Amazon itself – to not have to print, stock, package and ship physical books. The discount tends to be somewhere between 10-20 percent for most books (often less), which seems really low to take out all of book production and handling.

I love having paper books to hold and read, but they do accumulate and take up space and sometimes get thrown out (or second handed in a 2nd hand bookstore). For me the Kindle is mainly for the convenience of getting it immediately and the fact that the environment impact for paper use, packaging and shipping is reduced. After all it’s the content that counts not the package.

Everywhere I go people check out my Kindle and think it’s the coolest thing since sliced bread. Heck people often bug me when I’m reading it in public places and want to know what it is and how it works. Often they don’t even think that it’s too expensive at $359, although I think that’s a pretty steep price to pay for the honor of mostly purchasing books from Amazon (although you can also pick up books from other sources such as Project Gutenberg and a few other open source e-book sources). The interest is there, but few of those people actually follow up and get a Kindle.

A few Things that need Help on the Kindle

As I said I love the Kindle and I wouldn’t want to be without but there are a few things that do bug me about it:

  • Image support.
    The biggest issue by far for me is that if a book has any sort of image or illustration it’s really hard to decipher these images. There's no way to zoom and so images are usually way to small to decipher what is going on. I've been reading a lot of history and foreign affairs books lately and a lot of them contain maps that  could not be deciphered. The same can be true for tables or number charts (in financial books) and other non plain text references.
  • Technical Book Handling
    After I wrote about the Kindle last Spring after I just had gotten it, several friends asked me how I liked it and whether they should get one. They all came at me from the perspective of cutting back on their tree consumption for 1000 page technical books which are frequently one time read and throw out material. I checked out several technical books on the Kindle and they all sucked pretty badly. The main shortcoming is that if you look at source code (unless it’s really, really short) and it starts scrolling across even a single page it becomes really hard to follow the flow of code. The font differences also make source code hard to read in the first place. Along the same lines tables and charts don’t really work well which is problematic for coding related books. I have a few design books that are mainly text and those work fine of course – it’s just as soon as non plain text content is involved the Kindle falls short.
  • Too many Buttons that are too sensitive
    From a physical perspective one thing that gives me problems is the handling of the book. The sides of the Kindle are all buttons and it’s way too easy to accidentally hit a button or a wrong button. The button’s functions are also labeled a bit confusingly – Previous and Back for example do to very different things and while it’s easy to figure out when you use the thing for a while newbies invariably will use the back button to go backwards.  The back button’s behavior is not all that useful – it cycles backwards through ‘open’ books, but I can’t figure out how to go forward again.

    The buttons are also way too sensitive. As an experiment hand a Kindle to anybody who’s new to it and see how many times they accidentally page the content you're currently on. Good luck finding that page again. Even for plain reading I've often paged too much. It’s good the device has many buttons so it’s easy to get to them no matter how you hold the book and that’d be my guess why the book is all buttons. But maybe the buttons should involve a little more effort or maybe should be smaller and not beveled off the edge which just seems to invite accidental clicking.
  • Where the Hell am I?
    The Kindle uses some obscure page numbering scheme and if you happen to lose your page (because you handed it to someone to check out maybe?)  it’s not easy to find your way back to the place where you were. In a physical book I often peripherally track the page number I’m on because it’s a single simple number, but the Kindle’s numbering scheme is too long for that. There are no real page numbers but only something that looks like chapter and character offsets that shift by what looks like 10 for each page I actually read. It’s not very conducive to finding a particular spot in the document. Paging is an interesting problem for a device that can display different page sizes, but there’s gotta be a better solution than cryptic numbers. Like recalculating pages based on the font-sized used maybe (since that’s not likely to change often).
  • Paging is slow
    The above is made worse by the fact that paging is pretty slow. It’s acceptable, but noticeable when you’re reading and just flipping the page, but if you need to skip over a few pages the reader is agonizingly slow. It’s not just rendering on the screen, but the actual page flipping that’s slow – there’s a lot of ‘think’ time before the device actually switches the page.
  • Book Availability
    Although there is a good bit of content popular available for the Kindle on Amazon and outside of it, it still seems that I’m regularly hitting books that aren’t available for the Kindle. This isn’t really Amazon’s problem but publishers who likely don’t see it yet worth their effort to produce Kindle content (or electronic content in general) quickly. I would hope that Amazon is providing publishers some incentives to get books published for the Kindle which would seem a prudent idea for getting it into more people’s hands. This feeds back into some of the points Scott pointed to in his post in how to get more people into Kindles by ways of promotional deals and some free or cheap content.
  • What are my Book Rights?
    There are also some legal issues that may have to be resolved with this technology. Physical Books have intrinsic value – they can be sold or passed to others to read whereas the licensing of e-Books is to the single reader. If this really holds then the pricing for books should be even further reduced because the effective terms of use are much more limited with e-Books and the resale value is 0 especially in comparison to physical books.

In the end what it comes down to for me is that the reader is convenient as hell, and the actual reader display quality is excellent. The warts are definitely issues, but one that I can overlook to a large degree because of the convenience. Some of these points may seem fairly major, but while they are annoying they are not show stoppers. I still use the hell out of my Kindle on the road and even when I’m home. There’s definitely something satisfying about figuring out that a book I want to read is available right now and I can download and start reading immediately. Of course I don’t get the satisfaction of hearing the brown truck pull up and rip open a package in anticipation. <s>

But I think Amazon should really spend time on making sure that richer content displays better on the Kindle – I think that’s vital. I spoke with several people who wanted the Kindle for developer books and for for kids going to college to replace their text books – which both are prime targets for books that are fairly single use before they get tossed out or become obsolete. Prime candidates to reduce environmental impact of paper consumption. Not to mention delivery and packaging impact (which Amazon is often horrible on).

I’d be curious to see where this goes with Amazon or other book readers. There are not a whole lot of options out there but maybe we can hope that additional choices and a generic publishing format will eventually arise.



Dealing with Complex Client Interfaces: Html or RIA?



I’ve been working with a client whom I’ve been working for almost 10 years now in an advisory capacity. The customer has built and I’ve helped with systems that for the most part are highly meta data driven with application ‘service’ based interfaces that have light weight front ends talk to server heavy applications over the wire. These aren’t service based application in the SOA sense but lightweight, custom service implementations that serve data over HTTP.

What’s different with this customer (for me at least is) that they’ve always taken the lightweight UI tack, starting originally way back with a FoxPro front end talking via HTTP to the server (also FoxPro at the time), then moving to a Flash driven UI talking to highly efficient ASP.NET custom handler implementations. A couple of the apps created with the Flash front end are very rich in UI behavior although not terribly ‘flashy’ – rather these UIs are creating a desktop type environment in the browser with a window centric desktop format where you can look at many pieces of information simultaneously.

Currently there’s another project on up for discussion and again we were sitting around a table discussing UI options for this application. The app already exists as a desktop app that talks over Http to the service interface and it works. But the hassle of distributing, deploying, updating and maintaining a desktop application is proving to be a royal hassle for this company and they want to get away from that and on to a pure Web environment that runs completely in the browser without any sort of installation or configuration of environment.

A lot has changed: Ajax, Silverlight, Flash, RIAs

A lot has changed since the last time this came up so I threw out the options of going with a pure Html based interface vs. using Flash (also discussed Silverlight). After some back and forth though the client ended up more or less insisting on the Flash solution and after some detailed discussions I ended up concurring that this is probably the right choice for them.

If you read this blog occasionally you probably know that I’m a big fan of HTML based client interfaces and I believe that we’ve come a long way for building nice user interfaces that rival some of the functionality of what you can do with RIAs like Flash and Silverlight. However, I also have to admit that HTML has some serious limitations when it comes to building very complex user interfaces, especially when you start talking about Single Document Applications that are effectively driven through a single desktop-like scenario. The problem with HTML in this environment is that there’s really no clean way to build components and isolate them from each other or even build them separately as independent markup and code components.

The application in question is a data visualization and management application so there’s lots of interdependent information being displayed and the way that this is handled is by having multiple windows open that show this information, with the information refreshing in all windows as the source data is updated or queries are modified. The effect is that you make a change in one window and the display is affected in one or several other windows.

One thing that I’ve found is that it becomes very difficult in pure HTML environments to build complex window based interfaces. Not a big deal for popping up a few simple pop up windows (say for entering data or a query/filter box etc.). That stuff is easy. But once you talk about building a complex windowing environment with many windows where windows need to be self-contained and where the same window can be open in multiple places, it becomes very difficult – at least in my mind - to manage in HTML.

With ‘rich’ applications there’s a lot of expectation to build interfaces that rival desktop experiences and while you can get there with the basics fairly easily even just with raw client side coding plus some JavaScript libraries, to build a more complex ‘environment’ is still something that is very painful with HTML.

The problems here are many: There’s no effective native object model for encapsulating something like a ‘window’ or ‘widget’ that has component parts in HTML. While you can create classes that create window behaviors, managing the child HTML controls and content effectively and independently is not easy and certainly there’s no standard model for it. There’s also the issue of HTML control names – if you create two ‘windows’ that have the same content and are active simultaneously how do you ensure that you have unique control names for all the child controls? How do you reference these child controls if there’s some custom naming mechanism used. HTML provides nothing for control encapsulation or management of DOM components independent of the document itself – everything is top level.

For me the result is that when I build even relatively simple window heavy applications that there’s a lot of code that deals with managing the content updates of each of the windows cluttered into the main code with little to no abstraction. While I can create classes to isolate each window and its behaviors and operations, even that doesn’t really help with encapsulation of logic, naming and operation of composite DOM elements.

RIA’s tend to be more adept at this because they effectively allow for encapsulation of components that can be coded against in isolation and that define their own behavior. Where HTML has basically a single document that defines the unit of work, these component technologies allow a much lower level of abstraction and so make componentization much easier.

It’s possible that this could be done with HTML and some high level AJAX libraries have actually addressed this with a component architecture that wrap the HTML model, but at the plain HTML level there’s really nothing that provides this functionality to your day to day application development.

HTML is not always the Answer

In the end for this application that the customer is building, which is effectively an Multiple Document Interface type app where many windows are popped up, using a Flash or Silverlight based front end that allows isolation of these components into separate logical units is much more appropriate for this type of interface. I’m not a big fan of Flash and I couldn’t build the type of interface they are asking for by a long shot – but the customer has the in house talent to build the UI along with components that provide the UI building blocks from previous applications to present this type of interface. It’s hard to suggest alternatives in light of what is basically a proven formula that has been used in two other applications and admittedly has ended up with very slick results.

It’s not often that I run into this scenario in Web based applications where HTML is a questionable choice, but it’s happened on a few occasions and each time it makes me realize there are still a few areas for which pure HTML is not necessarily a good fit or at the very least a difficult fit. I’m sure some folks will say outright that a ‘desktop metaphor’ for Web apps is a bad idea anyway, but personally I’ve seen this come up in a few different situations because the interactivity between various interactive displays is vital.

I’d be curious to hear if others have run into this and how they are approaching componentization of complex HTML interfaces. Quite frankly I’ve not been in a situation where the UI for a single page was complex enough that it got completely unmanageable with JavaScript and a single page interface, but I’m pretty sure if we were to go down this path with the aforementioned application it would happen very quickly fall into the unmanageable realm. I have worked on a few applications where the page associated JavaScript went into a few thousand lines of code and most of it had to do with the window/component management aspects.  It gets difficult to deal with this much code in JavaScript especially given the currently crappy state of JavaScript editing/management tools. This might get better with better tool support, but the HTML issues of control naming and component encapsulation remain.

So here’s a question: Have you build really complex, client driven interfaces before and if so what approaches are you using to componentized the individual page components like windows or even separate edit/viewing areas to make them truly self contained and manageable through client code? Have you fought the battle trying to convince customers of one approach over another (RIAs vs. plain HTML) and which side did you argue and why?

It’s interesting to see how with the addition of choices the answer often becomes more cloudy rather than clearer <s>…



Using jQuery to search Content and creating custom Selector Filters



Last week at Southwest Fox a number of people asked whether it’s possible to use jQuery to do selector searches based on element content. The basic functionality is provided through the :contains() filter function in jQuery which provide a case sensitive search of element content. If I want to find all elements in a set of table row that contain a certain bit of text I might do something like this:

$("#gdEntries>tbody>tr:contains(SQL)").addClass("selectionhighlight");

which in this case will highlight any row that contains the case sensitive SQL string:

HighlightCommands

Nice. This can be be pretty useful. For doing Interactive searches for example. If I add a dialog to the page and hook up a little bit of script code to fire on the keyup event I can dynamically select rows as you type matching the text typed into the textbox:

// Set up key search
$("#txtSearch").keyup(function() {
    $("#gdEntries>tbody>tr:not(:first-child):not(last-child)").removeClass("selectionhighlight");
    $("#gdEntries>tbody>tr:not(:first-child):not(last-child):contains(" + this.value + ")").addClass("selectionhighlight");
});

Talk about a cool effect. Try it out for yourself, it’s pretty neat how easy it is to search and select content interactively.

Got this idea from one of the plug-ins Dave Ward Posted, specifically jQuery quickSearch, but you can provide basic search similar to the plug-in with just these few lines of code.

Creating your own Custom Filters

One very cool feature about filters in the jQuery selector engine is that you can create your own. The :contains selector  works, but unfortunately the search performed is case sensitive. If you wanted to create a case insensitive version of :contains you can do so pretty easily either by replacing :contains or creating a new filter altogether.

Here’s a new filter implementation called :containsNoCase:

$.expr[":"].containsNoCase = function(el, i, m) {
    var search = m[3];
    if (!search) return false;
    return eval("/" + search + "/i").test($(el).text());
};  

A filter selector expression adds to the .expr object and an operator member (in this case the colon :). All filter expressions are defined on $.expr[“:”] as new functions (or strings that are eval’d). The function called receives 3 parameters: The matched element, an index of the element matched, and m which is an object that contains parse tokens as an array. element 0 is the full filter string including parameter, with token 1,2,3 being the :, filter name and parameter respectively. m[3] is the first parameter which in this filter is the string we’re searching for.

The code then uses an regEx to find the expression in the document using case insensitive matching and returns whether a match is found. The result should be true or false to indicate whether the element should be included in the jQuery matched set.

You can declare this code anytime after jQuery has been loaded in a page and then use it like this on the same code above:

$("#gdEntries>tbody>tr:containsNoCase(sql)").addClass("selectionhighlight");

Notice now I specify a lower case sql string which would have failed with plain :contains, but using :containsNoCase finds the match properly.

Here’s another useful filter I’ve used:

$.expr[":"].startsWith = function(el, i, m) {
    var search = m[3];        
    if (!search) return false;
    return eval("/^[/s]*" + search + "/i").test($(el).text());
};

which instead of finding all matched content only finds content that starts with a given string. This requires a more focused search to hopefully select individual elements rather than elements that contain other elements. In the example above I might want to find only the columns or the bold text inside of the columns to search for starting text:

$("#gdEntries>tbody>tr>td>b:startsWith(fix)").addClass("selectionhighlight");

But wait… there’s more.

While I was searching around today I also found out that you can extend the selection operators themselves. If I get really lazy and decide that :containsNoCase is really too wordy for my lazy typing fingers I can shortcut this by creating a custom selector operator, say $. The following code does just that:

$.expr[":"].containsNoCase = function(el, i, m) {
    var search = m[3];
    if (!search) return false;