March 2009 - Posts

Over the weekend I hopped a train up to Waltham where I spoke at the New England Code Camp 11.  As usual, there was a vibe in the air that made everything seem almost make believe – or maybe that was just the green tint on the projector..  Either way, if you haven’t been to a Code Camp yet, there’s no better time.  Free content, free food, and great opportunities for networking.  That’s not to mention the give-aways. 

I spent a good amount of time in the Speakers lounge on Saturday and had the opportunity to meet some great people.  I was introduced to “Pocket Coffee”, a chocolate John Doucette’s his wife picked up in Rome.  It wasn’t any chocolate, it had an Espresso center.  After trying one, I was immediately hooked and started feverishly searching Amazon.com to see where I could buy a case.  Right before I had to leave to catch my train,I got into a long discussion about HDD’s and what the platters are made out of.  Turned out I was talking to somewhat of an expert on the subject.  Igor Moochnick told me about how he re-uses computer parts in his robotics side projects.  All in all good fun, and it was great meeting you all. 

The session itself was a hit (in my opinion).  I did an introductory level JavaScript talk, packing years of experience into an hour long discussion.  The session was more like a chalk talk than a standard “session”.  Actually I didn’t have a single PowerPoint slide prepared, and spent 99% of the time inside of Visual Studio or Internet Explorer.  If you were at the session and have any specific questions about the topics I covered feel free to use the comments section on this post. 

I’m not sure who traveled the farthest to get to the Code Camp, but I know that I lost out to Steve Andrews and John Baird who drove up from the Philly area. 

And speaking of Philly – The Philly Code Camp is coming up on April 18th.  I’ll be there, just not speaking this time.  I’m doing something a little different that I think you’re going to like.  I’m setting up in the cafeteria all day, and I’ll be there to answer any questions you have about the Infragistics tools.  Want to know what’s in the next release?  Need help figuring out how to do something with one of the tools?  Just stop on by.  Feel free to bring your laptop and projects with you, we can do some hands on debugging. 

It’s a time of change here at Infragistics.  We’re way past the half way point on our “Aikido” (Now officially just AJAX) framework, and with the 9.1 release many of the remaining classic Infragistics controls are being re-born as Web 2.0 loving replacements.  I’ve got some mixed emotions, since I’ve been here long enough to have worked on these classics, but it’s an exciting time to see what the dev team has produced.

I was just checking out the WebDataMenu for example, which replaces the WebMenu (part of UltraWebNavigator for those of you who have been with us from the start).  The WebDataMenu is databindable, and supports IEnumerable, XML, BindingList and ICollection.  Basically, if you’ve got data, the WebDataMenu can handle it.  DataBinding support is just the beginning though.  It’s also built with Web 2.0 ideals embedded in its core.  What do I mean?  Well, a quick look at the HTML source and you’ll see the WebDataMenu is composed of <ul></ul> elements.  Each Menu Item is a list item, meaning you can easily control the display of the WebDataMenu with CSS.

image

 

Add keyboard navigation (IE, FireFox AND Safari) and you’ve got a killer menu.  Take it one step further and allow Menu Items to be templated and you’ve got the menu to rule them all!  Seriously though, this menu is uber cool.  The release is right around the corner, so you should be able to download and try it out very soon.  Like before you do your income taxes soon..

Stay tuned for more about the 9.1 release.  You can also follow along on Twitter as I push out information over the next couple of weeks.

If you haven’t found us yet, Infragistics is on both Facebook and Twitter.  If you use either of these networks, you can follow us and get all of the latest information in a single place.

There are also a bunch of Infragistics employees up on twitter, for example you can follow me @igtony.  I’ve just started talking about the 2009.1 release which I’ve tagged with #na91.  Tagging allows you to easily search for a keyword.  The # (hash) is used to differentiate ordinary words from tags.  You can then use search.twitter.com to search for a particular tag. 

There’s your tip to staying connected for today!

BTW, I’m headed up to Waltham MA tomorrow to speak at THE New England Code Camp.  I’ll be up there tomorrow night (3/27/2009) looking for something to do.  Shoot me a message on twitter (a tweet) if you’re in the area!

One of the most common complaints I hear about the Infragistics tools is “there’s so much ViewState, it’s bloated!”.  One of the most common questions I hear is not surprisingly, “How do I reduce the ViewState?”.  Before I jump into the answer (which is a simple “turn it off”), I want to discuss what ViewState is.

ViewState is an encoded string that is actually a list of serialized properties and values for a given control.  Viewstate is how ASP.NET knows if a property has changed between postbacks.  Suppose I had a textbox and I wanted to change the background color to red when the user clicks on a button.  Easy task right?  Well, what happens if the user then clicks on a different button.  How does the application know to make the textbox red again?  The button click code isn’t going to fire again, and the Page no longer exists on the server (it’s torn down and re-created with each request/postback).  The fact that the background should now be read has to be stored somewhere – that somewhere is ViewState.  Here’s some code:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="Infragistics35.WebUI.WebDataInput.v8.3, Version=8.3.20083.1002, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb"
    Namespace="Infragistics.WebUI.WebDataInput" TagPrefix="igtxt" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <igtxt:WebNumericEdit ID="WebNumericEdit1" runat="server">
        </igtxt:WebNumericEdit>
        <asp:Button runat="server" Text="Red" OnClick="TurnRed" />
        <asp:Button runat="server" Text="Submit" />
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void TurnRed(object sender, EventArgs e)
    {
        this.WebNumericEdit1.BackColor = Color.Red;
    }
}

Clicking on the “Red” button turns the background of my textbox red.  Then if I click on the “Submit” button and force a postback, the textbox background remains red.  We often just take for granted that this works, but it’s ViewState that makes this work.  Look at the HTML source in your browser and you’ll see something like:

<input type=”hidden” name=”__VIEWSTATE” value=”…..” />  The ViewState is stored as a string value in a hidden field, and it keeps track of all changes made at runtime to a particular object.  When you set the backcolor to Red on the WebNumericEdit, that information gets encoded into the value seen in the __VIEWSTATE field.  The next time the page is posted back, the control peeks into the __VIEWSTATE field and re-serializes itself based on what it finds.  Let’s take a look at the example code above.  When the page initially loads, this is what the __VIEWSTATE field looks like:

<INPUT id=__VIEWSTATE value=/wEPDwUKLTIyOTMwMjU4MmQYAQUeX19Db250
cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgEFD1dlYk51bWVyaWNFZGl0MROii26o
a0DVPVO5sgHn/KcNr5fL type=hidden name=__VIEWSTATE>

That’s not very telling, so lets use a tool to decode this into clear text.  You can download ViewState Decoder which is what I’m using below.

image

Notice that there’s not really anything of note in the ViewState except for what looks like some sort of identifier key.  Now let's click on the Red Button and see what we get.

image

Notice there’s now an entry for a Color [Red].  If this value was not included in the __VIEWSTATE field, the next time the Page posted back, the background color would reset to White (the default).  To understand more about how this works, lets take a look at the control lifecyle.

image

The first phase after a Page is requested is the Init, where the objects are created from the ASPX markup.  After the Init the ViewState is “rehydrated” (deserialized) and the values are loaded into the appropriate controls/properties. 

Note: This is a simplified model.   There are additional steps like loading ControlState, and firing Events that I’ve left out for clarity. 

Notice that the last step before Render is “SaveViewState”.  When this method is called, every property that was ‘dirtied’ during the Page_Load will be persisted into the Value attribute of the __VIEWSTATE hidden field.  Yes, I said – EVERY property.  That’s the drawback of using ViewState, and the reason why it’s important to understand how it functions.

If you’re used to putting all of your initialization code in the Page_Load, inside of an if statement that checks that it’s not a PostBack, chances are you don’t need ViewState.  Removing the if statement and having that code execute on each Page_Load will allow you to set EnableViewState=False on a particular control, or even the entire Page.  The only case this doesn’t cover is the “I want to change the background-color of my TextBox to red on a Button Click”. 

So you’ve read this far, I’m guessing you want an answer to the questions at the top?

Q: “Why is the ViewState field so big for Infragistics controls!?”

A: The size of the ViewState field is directly dependent on the number of properties you’re setting – as per the definition of ViewState.  We’ve gone to great lengths to ensure that each property of our components participates in the ViewState lifecycle.  That means if you set 200 properties in your Page_Load, 200 properties will end up in ViewState, just as you should expect.

Q: “Do I need ViewState?”

A: The short answer is no.  In the scenarios above, I highlighted when and where you would want to use ViewState.  Infragistics controls are meant to work with or without ViewState.  If you don’t need to support the “I want to change the background-color of my TextBox to red on a Button Click” scenario, you can simply turn off ViewState.  The only time I use ViewState is when I forget to turn it off.

Q: “How do I reduce the ViewState?”

A: Turn it off.  There’s no in between for ViewState*, a control either tracks ViewState or it doesn’t.  Even though controls don’t need ViewState to function, it’s turned on by default for every control (as part of the .NET Framework).  Set EnableViewState=False on the control or Page level to turn off ViewState tracking.

 

*This brings me to one last topic, and one that takes the ViewState model to a whole new level – Grids.  The WebGrid has a commonly overlooked feature built into it where it can persist the entire datasource into ViewState.  Why do you need this you may ask?  When a grid is editable it needs to track the old value and the new value – that way it can fire the appropriate Update events and give you a chance to revert back to the ‘old’ value.  The old values can be persisted in ViewState in case your DataSource isn’t available the next time you post back.  There may be cases where you simply can’t fetch your DataSource on each postback (for performance reasons), or you manually populated the grid with data, and there is no “DataSource” to bind to.  In the WebGrid, you would turn off ViewState if you didn’t want the values to be persisted into the ViewState field.  Again, the default for all ASP.NET controls is to have ViewState enabled, so in your typical application you likely have this turned on (and you most likely don’t even need it). 

In the WebDataGrid we’ve made the ViewState serialization more granular and added a property to EnableDataViewState.  The property is self explanatory I think, but just to be clear – set this to True if you want to embed your data into ViewState, set it to False to keep data out of ViewState.  BTW, If you haven’t seen the WebDataGrid yet, go check it out now

So the next time someone says “The ViewState is too big”, not only can you save the day by showing them how to turn it off, but you can also explain how ViewState works and why it's there in the first place. 

If you haven’t seen, there’s a ton of new downloads available from Microsoft.  IE8 has finally been released!

There were also some nice announcements made at Mix09.  Most anticipated was the Silverlight 3 features.  Some of the highlights are below.

Silverlight 3

  • Video & Audio enhancements (H.264 support!)
  • Perspective 3D graphics
  • Pixel Shader effects
  • Dynamic Styles with Inheritance (cascading Styles)
  • Element to Element binding
  • Data Validation
  • RIA Services support
  • Out of Browser Capabilities

That’s an impressive list.  If I were to pick some of my favorites, it would have to be improved Data Binding support and Out of Browser Capabilities.  From day one when I heard the plans for Silverlight, all I could think of was how the browser was just this lame shell used to launch the application.  I guess the folks at Microsoft were thinking the same thing.  Plus, you don’t have to worry about back button support anymore.. ;)

I installed SL3 bits today, as well as IE8.  I already experienced a crash in IE8, which is strange, and I’m not yet ready to blame IE but I’ll certainly have to do some more diagnostics.  Haven’t had a chance to build with SL3 yet.  My only complaint so far is that the install is SLOW.  Actually the uninstall for SL2 Tools seemed to be really slow, the Install for SL3 tools was just slow.  I’ll keep you posted!

So the other day I was helping out a coworker on a project and was taken aback by the complexity of the project.  I was expecting a hacky WinForms app with a single form and one monolithic method.  Instead, I saw an organized solution with 6 projects, including a test project.  It took me a good 30 minutes to follow through the logic from one class to the next, across projects and assemblies.  This of course made me feel dumb, so my first reaction was – this project is too complex! 

So while discussing the project with some others to get ideas of what could be wrong, I happened to say - “there’s a time and a place for best practices”.  There was a moment of silence and then I got looks like I had just walked into a courtroom wearing a blue velour suit.  Because of the various reactions, I thought this would be a good topic to discuss with you.

I’ve always been a believer in the “do what it takes to get the job done” camp.  I like to follow best practices until they get in the way.  There, I said it..  I’m not going to hide it anymore!  I believe that a “best practice”, just like most patterns has a certain solution set that it’s best fit for.  I’m not saying throw caution to the wind and forego every bit of programming common sense, but would I use MVVM the next time I want to write a quick utility application?  Probably not.  I follow a rule of thumb – if the pattern I’m using is more complex than the solution the software is supposed to be providing, I’m not going to use it.  It all boils down to ROI – what benefits do I get by following this pattern and how much is it going to cost me to do it? 

Here’s a perfect example.  MVC is getting a lot of buzz right now, and seems like a good solution for a certain set of applications.  Does that make it better than ASP.NET’s classic MVP pattern?  I’ve grown fond of the idea of dropping a control on a form, writing a couple lines of code and being done.  Am I about to dump all of that because MVC is the new hot pattern?  I think you know the answer to that already.  That’s not to say MVC isn’t useful – as I said earlier there is a problem/solution set that MVC is perfect for.  It just turns out, that’s not the problem set I’m normally involved in..

So what do you think about all of this?  Are you a pattern / best practices follower?  Have you found certain ones worthwhile, or vowed never to use a one (or many) ever again?  Leave your thoughts in the comments.

More Posts