Saturday, June 19, 2010

PageMethods vs UpdatePanels

While sitting in just another meeting with another lead developer, I was told that they are using UpdatePanels with jQuery just because they read it somewhere over the internet.

I threw them with PageMethods alternate solution; (although it can be script'ize using ScriptManager.RegisterClientScriptBlock), especially when used with jQuery. To my bewilderment she did not know about the PageMethods; and I was gladly eager to tell her more! (0:

It is not "just another" way of interacting with server; its reliable, and communicates in the shortest forms possible - lowest client/server data exchange; plus, the response by the WebMethod gives the freedom to manipulate and interact with the HTML the way we want.

Its a static method that is exposed using WebMethod attribute; and is called using Javascript. Follow the following steps:

1. Set the `.EnablePageMethods` property of ScriptManager to True.


2. Mark the static method with WebMethod attribute
3. Call method using javascript. Like:

PageMethods.MyMethod(
 Arg1, 
 Arg2, 
 ArgN, 
 Incase_Success/*Optional*/, 
 Incase_Failure/*Optional*/);

function Incase_Success(results/*Returned by the method call*/ , userContext, methodName) { 
    alert(results); 
}

function Incase_Failure(errors, userContext, methodName) {
    alert(errors.get_Message()); 
}

The PageMethod calls the web method MyMethod with the defined number of arguments, and upon successful completion of the function call, it redirects the response to SuccessFunction or FailureFunction according the status that it has.

When calling the method, the first N parameters in the PageMethods call matches the parameters of the class method. In this example we have only only one parameter.

There are two optional parameters that takes the names of the methods to be called in case of failure or success. The callback for success, followed by the callback for failure.

Following is a quick comparison between UpdatePanel, and PageMethod from Encosia.

PageMethod data exchange,

An important point to note, is that most of us relate the the term web service with SOAP and XML. But in this case ASP.NET AJAX doesn’t use SOAP and XML.

Btw, this article is a MUST READ!

Happy coding!

Thursday, June 10, 2010

Aspect Oriented Programming

Waiting at Dubai airport, for the connecting flight back home to Karachi... reaping the fruits of last-minute-flight-schedule sown(read: reserved) by my admin department! I have a tweltve hour wait for the next "connecting" flight! Yep, those who have been-there-done-that know very well about such an exhausting experience.

With all the "unlimited" time that I have, I opened my 'Blogs' folder... searched through my incomplete blog related to aspect oriented programming.

I have been working over it off an on, adding multiple angles of understanding so that it would be easiest to digest.

Though this has been a pretty old subject now; but some once again was dragged into the technical argument among peers. Quite a couple of hours ranting about what is it, what it does, and when to use. On heading home I thought its a "must blog" (0: so here it is.

To understand the aspect oriented programming, it is important to understand the term aspect. Aspect generally means an area of particular interest/concern; a much widely used term that is the jist of AOP is cross cutting concerns.

So what actually is a "cross cutting concern"?

To understand that, lets discuss a small example. Consider following set of classes:

1. Products - This class add/update the products
2. Customers - This class maintains customers
3. AuditTrails - This class maintains the audit trail
4. UI - This class renders the UI
5. Logger - Logs the activity in a text file

A business "concern" could come up to implement a policy that would validate the length product name, and it should not be more than 20 characters. Or...
  • A system level concern may be to provide logging in all of the modules.
  • Another concern could be to ensure a database transaction should not result in loss of data - across all the data layer related modules.
  • Or that authorization is required to perform certain operations; for instance authenticate/authorize for Funds Transfer in case of a banking application.
The above are called "concerns" - specific area of interest; and since these concerns "can" span across the application(in case of logging, for instance), so this is how it gets the name "cross cutting concern".


A couple of practical scenarios where AOP could be utilized:

  • Logging invocation and public property access, as discussed earlier.
  • Handling exceptions in a structured manner
  • Validating parameter values; function parameters.
  • Caching method results and property values
  • Authorizing method and property requests
  • Measuring target method performance

Typically, if you want to enforce a behaviour across many different classes in a transparent way, AOP comes a best practice.

If you are a Java guy AspectJ might interest you; and for .NET/C# a good open library is Aspect.NET. Though Microsoft does provide Policy Injection Application block which can be utilized with Unity (Dependency Injection Framework - DI) application block that I reviewed sometime earlier in my posts.



Terms that are used in AOP:

From Wikipedia:
Cross-cutting concerns: Even though most classes in an OO model will perform a single, specific function, they often share common, secondary requirements with other classes. For example, we may want to add logging to classes within the data-access layer and also to classes in the UI layer whenever a thread enters or exits a method. Even though each class has a very different primary functionality, the code needed to perform the secondary functionality is often identical.
Advice: This is the additional code that you want to apply to your existing model. In our example, this is the logging code that we want to apply whenever the thread enters or exits a method.
Pointcut: This is the term given to the point of execution in the application at which cross-cutting concern needs to be applied. In our example, a pointcut is reached when the thread enters a method, and another pointcut is reached when the thread exits the method.
Aspect: The combination of the pointcut and the advice is termed an aspect. In the example above, we add a logging aspect to our application by defining a pointcut and giving the correct advice.
Bear in mind, you will pay a performance penalty if you're injecting behaviour into many fine-grained objects in your application, so you may want to consider if having this feature is worth the performance trade-off. Fortunately, one of the benefits of AOP is that you can enable/disable pointcuts fairly easily, which would allow you to measure the performance difference. If interested in knowing the code level implementations, here is a good article to start with.

Happy coding!

Thursday, June 3, 2010

Crawlable AJAX application

Briefly, the solution works as follows: the crawler finds a pretty AJAX URL (that is, a URL containing a #! hash fragment). It then requests the content for this URL from your server in a slightly modified form. Your web server returns the content in the form of an HTML snapshot, which is then processed by the crawler. The search results will show the original URL.

Wednesday, June 2, 2010

TDPE2: Toughest Developer Puzzle Ever 2

http://toughestdeveloperpuzzleever.com/ - Second puzzle with 100 levels, no one has completed it yet! It started two days ago, May 31st.

I'm stuck at level 7 - looping around!
Following are the answers till 9 :(

Related Posts

Popular Posts