Sergey Shishkin

on agile software development

Archive for May 2006

ProfileTextBox – coding ASP.NET without ViewState

ViewState is bad! No, it’s not an axiom. It’s like to say ‘drugs are bad’ – in general, yes, but they are used in medicine as well. So, ViewState is a kind of drug – you may use it in some exceptional cases, but many developers are addicted to the ViewState and use it here, there and everywhere.

What about ControlState?

Fortunately, in ASP.NET 2.0 ViewState model was revised – ControlState was included and developers were allowed to disable the ViewState without any fear to break any functionality of built-in controls. ControlState is the same drug as a ViewState, but its usage became more complicated, compare to ViewState, to make developers of controls think twice before using it. 

Exceptional cases?

I was developing a web form with new ASP.NET 2.0 Wizard web control when I understood that I have to use some sort of a ViewState. Wizard control simply switches controls’ Visible property to show only appropriate controls for each step. This behavior has its drawback – on the last step there are no values from previous steps in the Request.Form. So I needed to store values of controls somehow through the whole wizard lifecycle until the last step where actual data processing takes its place. Just to avoid the use of ViewState I looked at the ASP.NET 2.0 Profile API and decided to use it as a storage for my data.

Why Profile?

Profile approach has number of benefits. Firstly, user can leave my wizard somewhere in the middle of it, return to the site after a week or two, and the wizard will show the same step that the user has left. Secondly, all the data persists on a server-side and the user is not able to corrupt the data. Finally, it allows the developer to avoid the ViewState and ControlState.

ProfileTextBox

To reduce the amount of code for managing persistence of controls values I inherited the TextBox control and implemented profile-based persistence in my custom web control. First thing I done was switching the ViewState off 🙂

    protected override void OnInit (EventArgs e)
   
{
       
this.EnableViewState = false;
       
base.OnInit(e);
   
}

Then I defined a property to map my control’s value to the user’s profile property:

    private string _profileProperty;
   
[Bindable(true)]
   
[Category("Behavior")]
   
[DefaultValue("")]
   
[Description("Profile property.")]
   
public virtual string ProfileProperty
   
{
       
get
       
{ return _profileProperty; }
       
set
       
{
           
if (string.IsNullOrEmpty(value))
           
{
               
throw new ArgumentNullException();
           
}
           
_profileProperty = value;
       
}
   
}

To serialize/deserialize typed profile property to the TextBox.Text string property the same approach was used that is used in Profile API itself – TypeConverter class:

    private TypeConverter GetConverter ()
   
{
       
TypeConverter conv = TypeDescriptor.GetConverter(
           
ProfileCommon.Properties[ProfileProperty].PropertyType);
       
if (conv != null
           
&& conv.CanConvertFrom(typeof(string))
           
&& conv.CanConvertTo(typeof(string)))
       
{
           
return conv;
       
}
       
return null;
   
}

The most interesting part of this control is in the next two methods:

    private void DisplayValue ()
   
{
       
object value = Context.Profile[ProfileProperty];
       
if (value != null)
       
{
           
TypeConverter conv = GetConverter();
           
if (conv != null)
           
{
               
base.Text = conv.ConvertToInvariantString(value);
           
}
       
}
   
}

    private void PersistValue ()
   
{
       
string value = Page.Request.Form[this.UniqueID];
       
if (value != null)
       
{
           
TypeConverter conv = GetConverter();
           
if (conv != null)
           
{
               
Context.Profile[ProfileProperty] =
                   
conv.ConvertFromInvariantString(value);
           
}
       
}
   
} 

I think the code is self-explanatory, so only few comments on it: TypeConverter object is created using type information from profile property. To display the value the TextBox.Text property is used – After a post back the value is read out of Page.Request.Form. The only thing left to do was to include this persistence logic into web control object model:

    protected override void OnLoad (EventArgs e)
   
{
       
base.OnLoad(e);
       
if (Page.IsPostBack)
       
{
           
PersistValue();
       
}
       
DisplayValue();
   
}

Now this control with defined ProfileProperty property can be added to the web form.

<nt:ProfileTextBox Runat="server" ID="tbAttendees" ProfileProperty="Tmp.AttendeesCount" /> 

And don’t forget that one need to define Tmp.AttendeesCount property profile schema in web.config to use the control.

Conclusion

Returning to the initial problem I can now replace all TextBoxes with this new ProfileTextBox control and forget about them – all the data will be automatically synchronized with the current user’s profile. Along with the ability to use this control in complex UI scenarios like Wizard control, one can use it to create user’s profile editing web form without any line of code-behind. Also, nice feature would be to use AJAX to save values to the profile on the fly. 

This ProfileTextBox control is definitely not a universal ready-to-use solution for persisting and displaying any type of data, but this is only an example of approach one can use to build web UI avoiding a ViewState when it is possible.

ProfileTextBox.zip (1.2 KB)

Advertisements

Written by Sergey Shishkin

11.05.2006 at 12:34

Posted in Uncategorized

Open XML Framework presentation on the user group meeting

On Tuesday, May 18, I’ll present Open XML Framework project to the VfL-NiederRhein user group. It will be my first experience of speaking on public here in Germany. The meeting will be informal, it will take its place in Diebels Sports Bar Düsseldorf, and I hope for a friendly audience. Here is the abstract of my speech:

Office Open XML Formats are standardized by Ecma International and provide specifications of Microsoft Office 2007 file formats along with the general infrastructure for storing data as a set of XML documents compressed into one ZIP-package. WinFX brings a new Packaging API (System.IO.Packaging) to support this infrastructure.

The Packaging API allows the developer to open, create, manipulate and save Microsoft Office 2007 file formats and any other Open XML -based file formats. As a drawback it makes the developer to care about such low-level things like package parts, package relationships, IO streams, and XML.

This project is aimed to provide a framework for easy development of Open XML file formats. As an example of using the Framework the project delivers a class library that is compatible with Microsoft Office 2007 file formats. In other words this library could be used as an Office documents reader/writer.

Written by Sergey Shishkin

09.05.2006 at 17:03

Posted in Uncategorized

Open XML Framework

This is a story of one project I started personally in my spare time in a train on the everyday way from home to work and back…

Open XML
Microsoft has announced its new Office 12 Open XML file formats and I decided to develop a small application to play with them. There are a lot of “simple Word”-kind examples on the net (BTW, check them out on the http://www.openxmldeveloper.org), so I tried to build a “simple PowerPoint Viewer” just to be unique.

PresentationML Viewer
I implemented such a viewer that could open .pptx files, load some of their content into the simple object model, build HTML using this object model, and render HTML in the WebBrowser control. It supported text shapes (titles, subtitles and ordinary text lines) and colors (only solid filling). Unfortunately, I don’t have it now because it was spontaneously refactored into something ‘slightly different’.

Office Open XML API
Refactored? Developing this viewer I decided to split it into two parts. First – object oriented Office Open XML (actually, only PresentationML and some pieces of DrawingML) library to wrap Packaging API and XML structures with end-developer-friendly DOM-like API. Second – the viewer itself which should only work with that API and render the document.

Code in a train
Development process was very creative and productive with short everyday coding sessions in a train (40-45 minutes) and daylong thinking at work during breaks 🙂 So, I did refactoring and complete redesign almost every day! This process of generalizing common code practices forced me to separate a new project from Office Open XML API – generic Open XML Framework.

Open XML Framework
So, currently this is the main project and all other mentioned (including API for Office and PresentationML Viewer) are just examples of using Open XML Framework. In my next posts I’ll write more about the project itself and its features and plans. It was only an introduction…

Written by Sergey Shishkin

09.05.2006 at 16:47

Posted in Development