AjaxPro – first try

As i mentioned in the previous post, i now start with my code base for stories of version 1.0. In general, it contains 3 layers as other software. Data access, Business, and Presentation. Let see what i have in my mind:

  1. Data access: wow i can do all the stuffs with NHibernate. So just ignore it for now. I have tried NHibernate before, so it is not so difficult to get started.
  2. Business layer: As you can see all user stories in this version. Simple, right? Yes, it is. So just build the objects i need first, however, no need to say them here.
  3. Presentation: I want to keep it as simple as possible. However, it should be a rich web page which means that there is no real postback. Yeah THIS IS THING I TALK IN THIS POST.

AJAX! sure i should use AJAX to communication between client and server. The thing is which one? There are some options:

  1. ADO with data service: implement as webserver when you want to ask something from server.
  2. AjaxPro: Call method in the current page without posting back. I vote for this.

Basically, this technique was not new to me since in the company i have been working with a technique called ServerMethods, developed by our talent developers, except me. It has been there sine i went in. However, AjaxPro is much better which many types supported and it is constantly developed by the community, go in ajaxpro for more detail.

All you need to do to get it works is:

  • Add ajaxpro.dll into web reference.
  • Register type of class you are using ajaxpro.
  • And then call it from the client site.

Here what i did, simple:

Server side:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxPro;
using AnhDuc.BookStore.BusinessLayer;

namespace AnhDuc.BookStore
{
   public partial class Default : Page
   {
      protected void Page_Load(object sender, EventArgs e)
      {
         AjaxPro.Utility.RegisterTypeForAjax(typeof (Default));
      }

      [AjaxMethod]
      public Book GetResult(string searchKeyWord)
      {
         return new Book() {Name = searchKeyWord};
      }
   }
}

And from client side:

   function startSearch() {
          try {
             var _book = AnhDuc.BookStore.Default.GetResult($("#_txtKeyname").val()).value;
             alert(_book.Name);
          }
          catch (e) {
             alert(e.message);
          }
       }

That’s enough! I am going to finish the user story 1 soon.

Write a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.