Tuesday, March 18, 2008

GWT(google Web Toolkit)

Recently I was doing feasibility study of implementation GWT for our next project.

The tool kit allows programmers write plain Java which be compiled into JavaScript for rich web front-end application. There is a Java to JavaScript compiler will compile the Java code to JavaScript while the library that comes with the tool kit will hide the any browser quicks for you. The resulting JavaScript code will only be load once on the client and only the data will be dynamically requested by the JavaScript code. I think it is wonderful programming model.

The issue I am running into is that since Javascript has so called Same Origin Policy . It prevents any scripts that try to request data from any servers other than the server that the scripts are loaded.

It is not much a problem for the GWT, the front-end development server happens to be the same back-end server if you are using built-in RPC calls. For me, I was going to use Microsoft IIS as my development back-end server. Since I am going to use Eclipse with GWT plugin as my front-end development environment, the JavaScript will be loaded from my front-end development server and will not be able to request any data from back-end server on a different port (it will violate the Same Origin Policy).

To work around the problem, I use a tool called Fiddler which allows you to modify the request on the fly. What you need to do it just download and install the software from the web site. Upon launching the Fiddler UI, press CTRL-R to open customized script editor (it can also be open via menu Rules | Customize Rules... ). Locate a function call OnBeforeRequest and insert the following lines:

        if (oSession.HostnameIs("MYAPP")) {
            if (oSession.PathAndQuery.StartsWith("/search")){
                oSession.host = "www.google.ca";
            }
            else {
                oSession.host = "127.0.0.1:8888";
            }
       
        }

It is quite self-explanatory. It basically says that if the host name is MYAPP, then rewrite the host name based on the requesting URL. In the example, if the URL is starting with "/search", it will send the request to "www.google.ca" otherwise it will send requests to localhost port 8888. It needs to customized to suit your needs.

After you set it up, you can now requesting paging via Internet Explorer (unfortunately it works only on IE). Try the following:


This will overcome the Same Origin Policy restriction for you since your JavaScript and back-end server will all appear as it originated from the same server.

It has the following drawbacks.
1. It does not work on Firefox.
2. The breakpoint has no effect if you invoking the page via the proxy. (I do not know why?)








Friday, February 22, 2008

Microsoft Web UI

Microsoft has so many web technologies to do the same Web UI AJAX thingy. It does not even funny. Here is the (not exhaustive) list :

Sometimes it is a tough decision to make, especially they are still new and you have to overcome some quirks that are not very well-documented. I used the ASP.NET/AJAX in my most recent project. It turned out that in order to write a AJAX control extender you have to learn a whole framework/library in a completely different language (JavaScript). What a nightmare to maintain the code.


Tuesday, February 05, 2008

Is object assignment statement thread safe?

It really depends on the type of the assignment.

" The CLR guarantees that for types which are no bigger than the size of a native integer, if the memory is properly aligned (as it is by default - if you specify an explicit layout, that could change the alignment), reads and writes are atomic. In other words, if one thread is changing a properly aligned int variable's value from 0 to 5 and another thread is reading the variable's value, it will only ever see 0 or 5 - never 1 or 4, for instance. For a long, however, on a 32-bit machine, if one thread is changing the value from 0 to 0x0123456789abcdef, there's no guarantee that another thread won't see the value as 0x0123456700000000 or 0x0000000089abcdef. You'd have to be unlucky - but writing thread-safe code is all about taking luck out of the equation."

From link

It does not mention object reference assignment. However the size of object reference is always equal to the native integer which means on 32-bit machine the reference size should be 32 bits and on 64-bit machine the reference size should always be 64 bits, therefore it is thread safe.


Monday, February 04, 2008

Best practice for database version

I was always curious about how other people implement the database versioning in their software development process. Today I tumbled across the following article about best practice in database versioning.

The way I am doing it is quite strange forward and involves 2 steps.
  1. Create base-line database.
  2. Create and maintain a single change script file which will be checked  into source control.
Apparently it works well for us. The difference between the method mentioned in the article and my approach are (A) whether we create multiple files to maintain the changes in the source control (B)whether to create a separated table to maintain the current schema version. Everything else works the same way, including branching and merging.

Point A has no semantic difference.

For point B, it is not necessary because when you apply the script which has a version number in the source control (I am using SubVersion which will generate a unique repository-wide incremental number each time you make change(s) to the source repository and our build number is tied to this number), and therefore you know what is the database schema version after the script applies. If customer reports a problem with build 213, I know exactly what the database schema should be and all the source code that associated with this build.




Sunday, February 03, 2008

Session for ASP.NET

I ran across  this article about some hints and tips about session state server in production environment. It is not programming related but I think as a ASP.NET programmer should be aware of it if the site is high-traffic site that uses web farm. It also provides some links to cache server product. I think it is quite useful.

Thursday, January 24, 2008

Generic Wrapper for Session variable in ASP.NET

I found a way to wrap around Session variable in ASP.NET. It provides a strong-type return type and make code much easy to understand.


Usage:

            GlobalAppParameter gp = GetSession<GlobalAppParameter>("gp", delegate {
                return GlobalAppParameter.NewGlobalAppParameter();
            });

Declare/implementation

        protected delegate T CallBack<T>();

        protected T GetSession<T>(string sessionName, CallBack<T> callBack ){

            T ret;
            if (Session[sessionName] == null)
            {
                ret = callBack();
                Session[sessionName] = ret;
            }
            else
                ret = (T) Session[sessionName];

            return ret;
        }

Tuesday, January 22, 2008

Order By Sub-query

In case you never heard of this, you can actually create a select statement in which the Order By clause can be a sub-query too.

SELECT BranchID,

( SELECT TOP 1 InstitutionName FROM Institution WHERE Institution.InstitutionID=FIServiceLocation.InstitutionID) InstitutionName

FROM FIServiceLocation

Order By (SELECT TOP 1 InstitutionName FROM Institution WHERE Institution.InstitutionID=FIServiceLocation.InstitutionID)

The feature actually is very useful for me in a project I am working on, since I wanted to be able to sort a computed field.