Why I don’t like ASP.NET anymore

When I first started using ASP.NET (back in the .NET 1.0 days), I was in awe of the sheer capabilities it offered. For a humble ASP/VB developer, this was a welcome change. The Page object model seemed to be a work of art. “Code-behind” seemed to be one of the coolest things.  


Eight years down the line, my perspective has slowly started to change. All of a sudden, I have started to question the very design of ASP.NET (at least the object model and the Page Lifecycle). I have started to feel that the design is simply over-complicated. Let me give you examples of where I feel this pinch: 


1)       Code-Behind is always a mess


For ASP developers, developing screens using C# (or VB.NET) was a boon. Spaghetti code wasn’t there any more. All the power came in the form of code-behind. I liked code-behind to start with. But as and when we started to develop complex web applications (with rich UI), the code in code-behind files simply got worse by the day.



Web Client Software factory and the MVP pattern came in to reduce this mess to some extent. But I suppose, they arrived a bit too late. By that time, you already had applications with thousands of lines of code in each code behind file. WCSF and MVP only do so much. To date, I have never seen code behind classes which are not a mess. No matter how hard you try, you cannot keep code tidy. I know it is just not me, as I see hundreds of applications in the community which have the same plight.



Have a look at this piece of code, which I simply couldn’t do anything about. I have an editable grid with two editable columns – one a dropdown and the other is a text field (date). In this event, I am basically:


  1.      Binding a list to the drop down
  2.        Adding a delete command to each row, which is enabled conditionall
  3.        Disabling the fields if the record is not a new one.


protected void gvReports_RowDataBound(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList reportTypeList = (DropDownList)e.Row.Cells[0].Controls[1];
            reportTypeList.DataSource = _reportTypes;
            reportTypeList.DataBind();


            Models.Report report = (Models.Report)e.Row.DataItem;


            Button deleteCommand = (Button)e.Row.Cells[2].Controls[0];


            if (report.CanDelete)
            {
                deleteCommand.OnClientClick = string.Format(
                “if(!confirm(‘Are you sure you want to untag this report?’))return;”);
            }
            else
            {
                deleteCommand.Enabled = false;
            }


            TextBox reportDate = (TextBox)e.Row.Cells[1].Controls[1];
            reportDate.Text = report.ReportDate.ToShortDateString();


            if (report.ReportID > 0)
            {
                reportDate.Enabled = false;
                reportTypeList.Enabled = false;
            }
        }
 }


I don’t know about you, but I simply don’t like the above code block. Without hefty comments, you wouldn’t have a clue what is happening in this block. Help me make this better. Don’t even bother to ask how I did the validations for the fields in this grid. 


2) Spaghetti Code will not go away


One of the banes of ASP was spaghetti code – server script and client script being mixed together. ASP.NET removes most of the pain, but not all of it. I find myself bringing in server script in many places. Here is an example:


      ASP.NET randomly generates control ids at runtime when controls are placed in containers or they are in pages having a master. To access these controls on the client script (to do validations, changing style etc), you could end up doing something like this:


var divReports = document.getElementById(‘<%= reportsSection.ClientID %>’);
divReports.style.display = ‘none';              



3)       No Client Side Object Model


No matter how powerful the server side object model, tons of code still needs to be written in JavaScript. The bigger problem is that ASP.NET controls don’t really have a client side object model which mirrors the server side object model. Therefore, you end up either writing a lot of spaghetti code as shown above or hacking the DOM which the server side controls creates. I see that many 3rd party controls (like Telerik, Infragistics etc) actually offer a client side object model. I am not sure why it was not offered by the ASP.NET server controls to start with.


 4)       Painfully complex page lifecycle


For people who have developed “Portal” like applications, they would more than agree with me that the code would be horrendously complex, especially the container page which hosts the user controls. There are so many subtleties in the order of the events between the page and the user controls; you would get lost debugging through it. ViewState and master page add to the spice. Sometimes you begin to think that the idea of System.Web.UI.Control was a bad one. 


I have provided a few examples why I have started to dislike ASP.NET. I strongly believe MVC is the right way to develop ASP.NET applications. But unfortunately, it has arrived way too late for ASP.NET developers and the damage has already been done. Moreover, application stakeholders would have concerns to develop stuff on MVC as there is not much 3rd party control support as of now. In other words, developing rich UI with MVC would just be too big a challenge to deal with. Be that as it may, code behind will stay for some more time to come, and that is unfortunate.


 

Using Interception with Dependency Injection: EL 4.1 and Unity Spring 1.2 create new avenues

It is customary to conceive any large application to be composed of several logical layers. It is also a practice to reduce the coupling between the layers. Typically, one would create an interface  for each class of each layer and they would be coupled to the downstream layer classes only though interfaces.


  
You would also typically want to unit test each class in each layer. By definition, each of unit test would test a method of only one class and not the downstream classes it depends on. To achieve this , one would need to mock out the instances of the classes which the class being unit-tested depends on.  To bring all of this to play effectively, you need  to implement the Dependency Injection pattern in your application. Dependency injection is provided by Unity application block.


The application we talked about earlier has in addition to a lot of business logic, several pieces of cross cutting logic – logic applicable to all classes – like logging, tracing, authorization, validation etc. The code for these concerns are usually generic. It would be an awful amount of work to implement these generic pieces of code in every class  (or a group of classes) of your application. What you really need is some way to stitch these concerns into the application without disturbing the application code too much. This style of programming is called Aspect Oriented programming (sort of).  This capability is provided by the Policy Injection Application block.


Now, to me, both aspects  discussed above are very important to any application. In other words, any application would need both dependency injection along with some type of aspect orientation (or policy injection or interception).  The question really is – how do you really bring them together?


I am assuming that you are familiar with these blocks I just mentioned. To have rubber meet the road, we really need to marry IContainer.Resolve<T> and  PolicyInjection.Create<T>(). This was not possible  as of EL 4.0 and left me hoping. It is possible now – with EL 4.1 (along Unity 1.2). The interception capability comes as a extension which can be associated with the Unity container. Therefore, you can have policies associated with the container which govern which call handlers are added. The real beauty is this – the application classes really work with the IContainer interface. In an actual environment, you need to associate the container which has the correct dependencies and interception policies. The unit testing environment would basically replace the container with one where the Interception extension is not applied, and therefore, the unit tests would not really execute any cross-cutting logic (of course, you can choose otherwise). Neat!


I have just started to dabble with this ability, and a lot of questions still need to be answered. One that comes immediately from the top of my mind is performance. When a class is set to be intercepted, what would be the performance overhead, and what would be the overhead based on the type of interceptor (TransparentProxyInterceptor, VirtualMethodInterceptor or InterfaceInterceptor)? Obviously, there are no definitive answers and it all depends on the policies and handlers you add to your application. I have a reason to believe that the cost of interception is not usually so high, that is deemed only for academic purposes. One would need to perform relevant tests to ratify the same in her application on the representative environment. The second question, but seemingly more subtle, is instancing. The new interception features either work on the RealProxy based interception, or dynamic code generation.  Obviously, it would be more efficient to reuse these instances at runtime instead of generating them for every call. Now, if you have singleton instancing, the real question is about thread safety. How does a dynamically generated instance serve multiple request threads at a time – as it happens in web applications in production?


Having opened these questions, I would write up a follow-up post which hopefully addresses these. In the meantime, I will dabble with more scenarios using these new capabilities, and keep you posted.

Best practices in Workflow Design – Part 2

Naming convention in Workflows


Workflow activities should be named appropriately. As much as possible, they should reflect names form the business domain. For example, it wouldn’t appear good to have names like ifelseActivity1 or callExternalMethodActivity1. Instead, it is good to have names like ifProductsExists and MakePayment respectively. Shown below is a good example of the same:


Good Naming conventions


Custom activity names should really represent their purpose. Good examples are ApproveRequestActivity. It is customary to suffix custom activity classes with “Activity”. It is also a good practice to provide apt descriptions to all the activities in the workflow. This would help better understand the nuances of the workflow logic.


Transactions in Workflows


In application design, avoid scenarios in which transactions need to span between business components and workflow logic. Either a database transaction should complete before the workflow is invoked, or should be started and completed within the execution of the workflow sequence (using a TransactionScope activity, for example). In either case, care must be taken to ensure that the transaction (and hence, locks on shared resources) should not be kept alive for a long period, as this adversely impacts performance. For long running transactions, compensation should be employed for undoing the effects of a prior transaction. WF has adequate measures (compensatable activities) to incorporate the same.


Sequential versus State machine workflow


It is definitely possible to represent a workflow scenario of a business application as a sequential or a state machine workflow. State machine workflows appear to be the right logical choice for long running business operations, which can typically be visualized as those transitioning from one state to another by means of some human or process input. A sequential workflow may be harder to represent the entire workflow process in these cases, thought it is not impossible. 


State machine workflows also provide for better tracking of the process. Since each state in a state-machine workflow represents a logical step in a business process, the give better visibility into the current state of the process. Refer to this blog post for more info on the usefulness of state machine workflows.
Note that states within a state machine could have sequence of activities. Sequential workflows have their own place and have to be used appropriately.

Workflow Instance Identifiers – Correlation


Workflow scenarios, typically state machine ones, involve persistence. Here, the workflow waits for an event to occur and hence its state is persisted into the database. The workflow is brought to life by another part of the application which loads the hydrated instance and executes the workflow (perhaps transitions it to the next state). The unloading and loading is correlated by the WorflowInstanceId (a Guid value). It is the application’s responsibility to keep this value and then use it at a later point of time. It could be a wise thing to store this value in the database alongside with the other information and then fetch the same for correlation at a later point of time.


It is perhaps, not a good practice to wait for the runtime to start the workflow so as to get the WorkflowInstanceId. A better approach is to generate the Guid value and pass it the workflow runtime as a parameter. This helps in situations where starting a workflow is a small part of a transactional business operation, and you need to keep the WF runtime pieces out of transaction scope.


Error Handling in Workflows


It is important to understand that error handling in workflows is as important as those in the business components.  Workflows that encounter an error, either through an exception thrown by a called service or that is raised internally, go in faulted state and complete prematurely. Since the execution nature of workflow is asynchronous, unhandled exceptions are not raised to the application and go unnoticed. This could adversely affect several parts of the application expecting a persisted workflow, especially in the case of a state machine workflow. Since the faulted workflow goes to the closed state, the application expecting the workflow to be in a specific state is left in an unrecoverable situation.


To avoid such situations, one should include appropriate fault handlers in the workflow.  An example of the same is shown in the figure below. It would be the responsibility of the fault handler to take the workflow to a recoverable state. The fault handler could do things like logging the exception details, persist recovery information and so on, analogous to a catch block in C#.


Error handling in State Machine Workflows


State machine workflows are to be handled in a different way. State machine workflows do not have a workflow-wide fault handler collection (it does not make sense either). Here, one should provide a fault handler for every sub-activity within each state activity. It is a good practice to keep the semantics of a state machine even in the event of a fault. This can be done by having the fault handler move the workflow to a user-defined error state. At this state, the workflow could either wait for recovery to happen, might just log and exception and move to completed state, or whatever is the apt handling logic for the application. You could consider options for retrying for a fixed number of times as well.
 
Workflow Cancellation


Cancellation of workflows is handled similar to Faults. One should wire appropriate Cancellation handlers to every activity. Like a fault handler, the cancel handler should gracefully close a workflow by taking the appropriate action.
 

Best practices in Workflow Design – Part 1

It’s been a awfully long time since I have published a blog entry, and it seems like the world of technology has changed quite a bit since! The hiatus is over now, and I would like to starting putting down some thoughts and experiences on my new interest – Connected Systems. WF and WCF are the pretty much the atoms for building connected systems on the .NET platform, and a large percentage of my upcoming posts would be focused on these technologies.


In this post, I want to talk about some aspects of workflow design based on my experience, which I think can be classified as idioms or best practices.


Workflows should contain only ‘flow of logic’ and not the logic itself


It is easy to visualize workflows as business logic which is modeled using a designer. It is common to see developers transform their typical business logic into workflows by separating different parts of the logic into separate workflow activities. Here, we see lots of procedural code gets transformed into workflow models. This is clearly not the right thing to do. Workflows are not be be plain replacement for existing business logic components. Workflows are to be used in specific scenarios – like long running business processes, rule driven logic, dynamic flow changes etc (I’ll cover this in a later post as well).


The figure shown below contains a snippet of a bad workflow design where procedural logic is transformed to a workflow through usage of code activities (in an order processing application):


Bad Workflow Design


Avoid business logic code within workflows


Ideally, workflows should not contain busines code themselves. In other words, as much as possible, usage of CodeActivity should be avoided (at least, it should not contain business logic). One way to take out business logic contained in code activities is to replace them with CallExternalMethod activity, where you invoke upon a method of a registered service. A better way would be to create a custom activity, where we override the Execute method, and within it delegate the business operation to a registered service. This way, taking individual pieces of business logic out of the workflow also helps us to unit test them in isolation. If a workflow is designed well, it would not have any code in its code-behind logic. This approach would also help us in scenarios where we have only XAML-based activation of workflows.


Given below is a snippet of a custom activity which is used in the Purchase Order creation workflow, and updates the status of a workflow in the database. The Execute method contains the usage of a registered service to which the business operation is delegated to (updating of the status field in this case).


public partial class UpdateOrderStatusActivity: Activity

{

        private static DependencyProperty PurchaseOrderProperty = DependencyProperty.Register(“PurchaseOrder”, typeof(PurchaseOrder), typeof(UpdateOrderStatusActivity));

 

        public PurchaseOrder PurchaseOrder

        {

            get { return (PurchaseOrder)base.GetValue(PurchaseOrderProperty); }

            set { base.SetValue(PurchaseOrderProperty, value); }

        }

 

       

private static DependencyProperty OrderStatusProperty = DependencyProperty.Register(“OrderStatus”, typeof(string), typeof(UpdateOrderStatusActivity));

 

        public string OrderStatus

        {

            get { return (string)base.GetValue(OrderStatusProperty); }

            set { base.SetValue(OrderStatusProperty, value); }

        }

 

          public UpdateOrderStatusActivity()

          {

                 InitializeComponent();

          }

       

 

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

        {

            IOrderProcessWFService orderService = executionContext.GetService<IOrderProcessWFService>();

            orderService.UpdateOrderStatus(PurchaseOrder,OrderStatus);

            return ActivityExecutionStatus.Closed;

        }

   }


 


I’ll try to cover more best practices in my next post. If you have better suggestions or comments, please do share it!


 

WCF – A Perspective

.NET 3.0 has been made available to all the developers recently. With it comes a set of new set of API, which are simply there to unleash the .NET developer’s potential to create the next wave of powerful applications. These API, formerly called WinFX, take the form of four pillars – WCF, WF, WPF and WCS. In this post of mine, I would like to give a perspective on the Windows Communication Foundation (WCF). 


Most typically, one starts off an introduction to WCF by explaining the four tenets of SOA, and how WCF embraces them from the ground-up. While this is very reasonable, I would like to think about WCF from a slightly different perspective which, of course, would eventually flow into confluence of the SOA tenets. To get a great insight into these tenets, I would urge you to look into this paper by the venerable Don Box.


WCF, in a nutshell, is all about facilitating message exchanges between endpoints. Note that ‘message’ and ‘endpoint’ are underlined. Well, these are but, what WCF is all about. These are concepts through which SOA is realized. Let me give you my understanding on these concepts:


If any entity needs to communicate with the world, it does so with Endpoints. Endpoint comprises of three elements (commonly called as ABC):
Address (Where)  – Where can I find the service, the URI?
Binding (How) – How can I communicate with the service? What are security requirements, encoding requirements etc.?
Contract (What) – What is the structure of the messages that I need to communicate with the endpoint?  What are the message exchange patterns (like Request/Reply, OneWay etc)


A WCF service would therefore, have at least one endpoint; it could have multiple based on different combinations of bindings, contracts or endpoint addresses.


If you really look at it, the concepts embodied by WCF aren’t new. They were pretty much present in ASMX. What lacked was that these concepts/abstractions were not explicitly codified in the ASMX (ASP.NET) framework.  ASMX just seemed to be a quick way to get .NET classes to talk SOAP. So, caught in a world of classes and attributes, developers lost sight of underlying concepts that manifested as wsdl:portType, wsdl:port, wsdl:message and wsdl:binding.


WCF, on the other hand makes these concepts explicit, as seen in the ServiceEndPoint, BindingContractDescription, and EndPointAddress classes in the System.ServiceModel namespace. It is this object model that makes WCF very powerful. The number of out-of-the-box bindings available, and the existence of several extensibility points speak of the raw power of the WCF programming model. Also, this architecture is what makes unification of several programming models like MSMQ, COM+, WSE etc. possible.


“Message”, which can simply be thought of as the data/document/information exchanged between endpoints, is a first class citizen in the WCF programming model. Unlike its predecessor, where messages meant no more than an XML document (or stream) representing the SOAP envelope, WCF provides much better representation and finer control over messages. Messages are represented by the Message abstract class in the System.ServiceModel.Channels namespace.


Having seen how endpoints and messages are dealt with in WCF, it can be safely asserted that we are now consciously moving from the procedure call paradigm to a message exchange paradigm.


To find out more about WCF architecture, and the core classes that make up WCF, refer to this excellent paper by Yasser Shohoud.


Update:


As a part of the community launch of Windows Vista and Office 2007 in the B.NET User Group, we, a group of MVPs and other active community members authored a small e-book, comprising of articles on Vista, .NET 3.0 and Office 2007. Based on this post of mine, I wrote an article on what I thought the underpinnings concepts of WCF were. You could download the PDF of the e-book here.


 



Implementing Timeout & Retry

I have come across several situations where I need some sort of Timeout functionality for a particular operation. At the same time, I also needed a provision for retrying the operation a few times before calling quits. I thought about a solution using ManualResetEvent and a Timer which I have outlined below:

const int MAX_RETRIES = 5;
const int RETRY_INTERVAL = 10;    //10 seconds
const int OPERATION_TIMEOUT = 1; //1 minute

static void DoOperation()
{
    
int retries = 0;
    
int timeout = 0;

    ManualResetEvent waitHandle = 
new ManualResetEvent(false);

    
//Use a timer to retry the opeation periodically
    
Timer retryTimer = null;
    retryTimer = 
new Timer(
           
new TimerCallback(
               
//The operation to carry out is represented by this anonymous method.
               
delegate(object state)
               {
                   
try
                   
{
                       
if (retries > 0)
                       {
                           
//The operation has failed before. So you may want to handle 
                           // that situation here.
                           
Console.WriteLine("Retry attempt: {0}", retries);
                       }

                       
//Do the operation which may fail or timeout. 
                       
Console.WriteLine("Doing some major operation now.");

                       
//throw new Exception("Some exception");
                       //Thread.Sleep(1000);

                       //Checking whether the operation timed out. In this case, the
                       // thread isnt forcefully aborted, so you can exit gracefully
                       //Else, you may need to handle the ThreadAbortException
                       
if (timeout == ManualResetEvent.WaitTimeout)
                       {
                           Console.WriteLine("It seems that the operation timed out. Need to exit gracefully");
                       }
                       
else
                       
{
                           Console.WriteLine("Operation completed");
                       }

                       
//Set the waithandle if the opeation is successful
                       
waitHandle.Set();

                       
//Everything went well. Close the timer.
                       
retryTimer.Dispose();
                   }
                   
catch (Exception e)
                   {
                       
// Handle the exception

                       
retries++;

                       
//Check if the number of retries has exceeded the maximum allowed
                       
if (retries > MAX_RETRIES)
                       {
                           
//Do some logic to handle this situation
                           
Console.WriteLine("Operation has exceeded maximum attempts");

                           
// Set the waitHandle so that the method returns
                           
waitHandle.Set();
                       }
                       
else
                       
{
                           
//Retry the operation or any do alternative logic here
                           
Console.WriteLine("The operation failed and will be retried in a short while.");
                       }
                   }
               }),
           
null,
           
new TimeSpan(0, 0, 0),
           
new TimeSpan(0, 0, RETRY_INTERVAL)); // The timer retries the operation for the specified interval

    //Wait for the operation to complete or timeout.
    
timeout = ManualResetEvent.WaitAny(
       
new WaitHandle[] { waitHandle },
       (
int)new TimeSpan(0, OPERATION_TIMEOUT, 0).TotalMilliseconds,
       
false);

    
if (timeout == ManualResetEvent.WaitTimeout)
    {
        
// The operation timed out. Abort the operation thread or exit gracefully.
        
Console.WriteLine("The operation timed out.");
    }

    waitHandle.Close();
    retryTimer.Dispose();
}

Let me explain this code section which may look a little convoluted at first. One of the easiest ways of implementing the timeout functionality is using WaitHandles. In the sample above, I have used a ManualResetEvent, but I guess you may use other WaitHandles like AutoResetEvent** etc, as you see fit. The idea is to spawn off a thread to execute an operation (which could possibly be a long one), and then wait for the thread to signal completion through the ManualResetEvent’s Set method. The ManualResetEvent provides a static method called WaitAny which can be used to wait for a specified period of time on one or many waithandles (In our case, it is just one). 

Retry logic can be implemented using a Timer (System.Threading). The Timer event is where the meat of the work is done. The exception handler basically does the work of checking if further retries are required by checking a counter. Once, the process successfully completes, the WaitHandle is Set and the main thread is signaled.  

**At first, it may be difficult to differentiate  or choose between ManualResetEvent and AutoResetEvent. AutoResetEvent is analogous to the doorbell switch, which goes back to the off state once you press and release it. AutoResetEvent resets itself automatically once you set it. ManualResetEvent, on the other hand, does not reset automatically – it is like a normal switch. Therefore, ManualResetEvent can be used when signaling happens once and for all, and subsequently, threads need not wait again for another signal. AutoResetEvent , on the other hand, can be used for finer coordination between threads, when signaling can happen intermittently.

*Disclaimer:
I don’t suppose this is the best way to achieve timeout and retry – this is just one way. Do let me know if there are better techniques!

Predicates in Action

Generics offer tremendous flexibility – in more ways than you would ever know. Predicates are a good example for this. .NET 2.0 BCL comes with certain generic delegates – Action<T>, Predicate<T>, and Converter<Tin, Tout> respectively. Their definitions are given below:

public delegate TOutput Converter<TInput, TOutput>(TInput input);

public delegate void Action<T>(T obj);

public delegate bool Predicate<T>(T obj);

If you look at the definitions, you can guess that the instances of these delegates hold references to methods that do type specific operations. For example, the Predicate delegate represents a method which takes an instance as parameter, and determines if the instance meets a particular criteria. Quite obviously, these delegates would be most applicable to generic collections and arrays. The List<T> class for instance, employs them  in Find, FindAll, Exists, FindIndex, FindLastIndex, ForEach methods. You could find similar usages in the Array class.

The real cool thing is using predicates in concert with anonymous methods. At first, it may look convoluted, but actually, that makes coding real easy (and fun). The oversimplified code snippet given below does a couple of things – given a list of accounts, it finds all accounts with a zero balance, and for each of these accounts sends a mail notifying the same. The FindAll method uses a Predicate<T> delegate and the ForEach delegate employs the Action<T> delegate.

List<Account> accounts = GetAllAccounts();
accounts.FindAll(
        
// Uses the Predicate<T> delegate
        
delegate(Account account)
        {
            
return (account.Balance == 0);
        }
    ).ForEach
    (
        
//Uses the Action<T> delegate
        
delegate(Account account)
        {
            SendMail(account, "Be aware, you now have a zero balance");
        }
    );

What you may have noticed is that we have completed avoided nested for/foreach loops here. I have used anonymous methods inline here, but you can easily replace them with delegate instances so as to accommodate different criteria or action items at runtime. In more advanced cases, you could generate predicates at runtime based on user provided business rules or something like that.

Thus is the amount of flexibility could can reap out of generics! I would strongly recommend one to go over this interesting MSDN Magazine article on Predicates by Ken Getz to get a better idea.

Musings on Web 2.0 – Part 2

In my previous post on Web 2.0, I mentioned that one of the new trends in the web today is that of “mashups”. The idea is about getting data from multiple, disparate sources and collating them in interesting ways. For example, let’s say we need to create a mashup involving a map showing all theatres playing the highest rated (most popular) movie in a particular city. Now, to develop this, one would need several pieces of data from several sources. First of all, you need to use map services (like Google Earth or Virtual Earth) to show a map. Secondly, you need a service that lists all popular theatres in a particular city, and what they are currently showing. You would also need ratings and reviews for all the movies that are being screened. Today, all these pieces of information are currently available, but mostly in several different websites. Each of these web sites could have their own revenue model revolving around special services they provide (maybe reviews, for example). So, our mashup which we conceived earlier would receive a setback if there is no data/service available which provides that information in an easily consumable form. In other words, you need structured data to make magic happen.


HTML traditionally, has not been considered as structured data. Perhaps, that is why we have XML to represent complex data forms. But how do you represent information in XML for the web? One answer would be SOAP and Web Services. As revolutionary as web services are for business applications, I feel they are not that conducive to AJAX-based Web 2.0 applications of today. The reason is that SOAP isn’t simple anymore; or at least, for naïve JavaScript-based clients. The evolution of WS-* has morphed SOAP into a monstrous stack of composable standards, which is beyond the reach of simple scripting today.
 
So, the order of the day is XML, but in simpler formats, and which are accessible through simpler protocols. Perhaps, this is the reason why RSS and ATOM were born, and are very popular today. Perhaps, this is the reason why REST is gaining momentum. Today, we see a large ecosystem of applications built around RSS and REST, and this can be attributed to their simplicity.


While XML is really powerful, the inherent concern of web applications to incorporate XML to express semantics could seem to be additional investment in time and space. If we go a step backwards towards HTML, and look for ways of expressing semantics, we wouldn’t have an answer, right? Wrong. Apparently, HTML has certain attributes like rel, class, and rev that are rich enough to express, and at the same time could be decoupled from visual markup. This is the idea underpinning a new way of semantic expression on the web – Microformats. Microformats, like RSS, are creating a whole new ecosystem of standards and applications (like crawlers and bots) that make structured data even more pervasive and accessible.


Microformats are a Web 2.0 phenomenon, and are testimonial to what I had to say earlier – Web 2.0 has brought about new “innovative techniques” of harnessing existing technology stacks. Be it through XML or Microformats, hopefully, the World Wide Web will evolve into one large blob of structured data available for everyone to create mashups like never before!

Musings on Web 2.0

Web 2.0 – undoubtedly, is one of the most debated software buzzwords in the recent times. [The other term, in my opinion, which has attracted a lot of debate (and abuse) in the recent history is SOA]. To many, it is just about embracing new techniques, which are DHTML and JavaScript centric. But excavating the early manuscripts of Web 2.0 (like Tim O’Reilly’s canonical Web 2.0 article) reveals a bigger picture. 


So, what exactly is Web 2.0? Here’s another very brief perspective to add to thousand others already existing in different flavors. I would start by putting it this way (the world’s smallest definition of Web 2.0): A set of new ideologies combined with a set of novel techniques for building today’s web applications. Another definition could simply be – a new ecosystem of applications (or say “services”) built on the WWW platform.


The new ideologies:


1) The Web Platform


Websites today are not just places where static information is presented, or which offer a single service (like e-mail, storage, forums etc). Websites today are platforms in their own right. They offer the capability to “compose” disparate applications to build a very powerful whole. The word “Mash-Up” is a popular term used to represent this ideology of mixing (or re-mixing) data and functionality. Now, part of this realization is capable by seeing our large world of applications as services, which can be consumed in non-proprietary ways. Not just SOAP, but plain old JavaScript itself is turning out to be that duct-tape technology making this possible. Live, Google and PageFlakes are great examples of the same.


2) De-centralization of the Authority & Data


Websites of the past era basically owned the content and its presentation entirely. With Web 2.0, the authority on the source of the data and its presentation shifts to the user. The website or web application basically provides a canvas which the user leverages to consume (and compose) services from disparate sources to meet their needs. As an example, you could cite msn.com (of the yester years) giving way to Live.com of today.


3) Collaboration


This is new trend to surface in recent past. Websites today are opening the doors to the world to participate, contribute and collaborate. In this model, users directly contribute to the data/knowledge base of the website. The exemplar of this ideology is “Wiki”s, and Wikipedia, which is one of the most popular websites today. Tim O’Reilly’s article rightly calls this “Harnessing Collective Intelligence”.


The Novel Techniques:


HTTP, DHTML, JavaScript haven’t changed much in the recent times. But these days, you get to here a lot of new jargons popping all over the place, all of these based of these fundamental technologies. At the lead of this jargon parade is AJAX (Asynchronous JavaScript and XML). Some others in this list are Comet, JSON, etc. An extensive examination of these would reveal that these are nothing but simple tweaks and hacks to existing methodologies like XMLHTTP and JavaScript’s own eval function. Therefore, I term these seemingly “new technologies” as novel techniques. 


New or otherwise, these techniques and ideologies definitely have resulted in new possibilities and opportunities for web application developers. We are seeing new revenue models which transcend the notion of simple buying and selling which we saw in the recent past. A new journey has just begun, it is time for us to get on the bus and be a part a long adventure!


 

Tip: Debugging Windows Services

The other day, I was watching the PDC presentations on WCF and one of them was on hosting WCF services by Steve Maine. Part of the presentation was about hosting Indigo on Windows services (or NT Services). One of the demos had a small and very interesting tip on debugging service startup, which I shall try to elaborate here.


I always wondered how the service startup code could be debugged. There are two problems which you would face invariably. First of all, there is no process to which you can attach a debugger to start with. I have done stupid things in the past to step into startup code – by adding a Thread.Sleep in the OnStart method, assuming that this would give me enough time to attach the debugger to the newly started process. This isn’t very smart as you would hit the second problem – the Service Controller Manager (SCM) would time the service out way too soon. Not to worry much – there is any elegant solution to both these problems.


You need not add a Thread.Sleep anymore – just add a Debugger.Break in your code. This isn’t new, but I simply hadn’t found a use for this before. This method is actually pretty useful if you want to conditionally attach the debugger in situations like this.


The solution to the problem of the SCM timing out is a method in the ServiceBase class (System.ServiceProcess) that has been added in .NET 2.0. This method is the RequestAdditionalTime, which takes a single parameter – the amount of time in milliseconds that the SCM has to wait additionally before pulling the plug on the service. Note that this method can only be called inside of OnStart, OnPause, OnContinue and OnStop (basically all the commands you can forward to the SCM).


Neat!