|
|||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionDoes the internet programming have to be awkward and user unfriendly? Yes it does until the internet programming model is changed from HTTP/HTML to another one. I already hear very angry criticism saying that this model is being successfully used by thousands of programmers all over the world. Yes that is true. However have you ever asked yourself the questions like: It is strange that after more than 10 years of internet existence there is no a decent HTML editor. Development of HTML page is always a tweaking with HTML tags. Were these editors developed by incompetent beginners? No , they were designed by very skillful people, but the HTML language is not up to the task. And that is not all. The data exchange between the server and the client is ultimately primitive. There are no common objects. Even with ASP.NET the situation has not improved much. Unfortunately the model can not be changed instantly. We have to live with it for a few more years and this article is about how some internet data exchange problems can be solved. Unification of the client and server programmingWould it be nice to program the client the same way the server is? In objects, not in HTML and DOM terms? What is the main problem in the development of rich web clients? The problem is that rich application (no matter Windows Forms or Web Forms based) always relies on rich objects. Saying rich means well structured hierarchal classes and the means to manipulate these structures. Unfortunately noting exists on the client side to facilitate this task. We have Java script language - pretty adequate language that syntaxically allows for the manipulating the structured data, but there is no mechanism to deliver these structures from the server to the client and back from the client to the server. At best we can use XML parser. But do we have to? Presented framework solves these problems. Hence all the objects originate on the server, we create the objects (classes) of virtually any complexity on the server, initialize them, register them with framework (RWCDF) and then the framework will generate already initialized objects for the client. After the data manipulation on the client side is complete, the objects get sent back to the server using traditional PostBack or [and ] AJAX. You can also invoke the methods directly on the server using AJAX calls passing the objects (the classes) as parameters. For the efficient data manipulation we need collections as well. Without dynamic structures like ArrayList and Hashtable the implementation of any serious data manipulation engine is very time consuming. Java Script ArrayList and Hashtable have been implemented and included into the framework. In other words the framework unifies the server and the client data manipulation. With this framework the programming of the web client is almost identical to the programming of the server side.But be aware that it is not a panacea. The performance of Javascript is the main showstopper. Apart from that Javascript has a very loosely defined data types. Framework ProgrammingThe first logical step is to create the data structures that will serve our needs. public class EmployeeAddress { public string City; public string Country; public string Street; public string Phone; public ArrayList PhotoAlbum = new ArrayList(); } public class Employee { public long somelong = 8598065; public double Salary = 787878.344; public float Scale = 8.66F; public string Name; public string SName; public string Title; public string Position; public int Age; public DateTime Date; public string PictureUrl; public EmployeeAddress Address; public bool Married = true; public ArrayList SomeDataCollection; public Employee { Address = new EmployeeAddress(); } } Note that the instance of the EmployeeAddress class is the member of the Employee class. ArrayList MyEmployees = new ArrayList(); Employee emp = new Employee() emp1.Name = "Bob"; emp1.SName = "Smith"; emp1.Title = "Mr"; emp1.Position = "waiter"; emp1.Date = new DateTime(2003, 2, 6); emp1.Address.City = "London"; emp1.Address.Phone = "(043)8984 63535"; emp1.Age = 33; emp1.PictureUrl = "BobSmith.jpg"; //add employee to ArrayList MyEmployees.Add(emp); //....then create more employees and also add them to ArrayList MyEmployees.Add(emp1); After that we need only to register the object (s) which we want to use on the client - in our case Convertor.RegisterObject("MyObjectKey", MyEmployees); The number of registered objects is not limited. var EmployeesArrList = _Get("MyObjectKey"); Get the first employee: var emp = EmployeesArrList.GetAt(0); alert(emp.SName + ", " + emp.Name + "," + emp.Title + "," + emp.Position + "," + emp.Address.Street); Same with other Employees. var Params = new Array(); Params.push(EmployeesArrList ); var Ret = r.Invoke("ReadEmployeesMethod", Params);You can create the instance of the Employee (s) and initialize it : var Emp1 = new Employee (); Emp1.Name = "Peter"; Emp1.Age = 20;And so on ... Add this employee to the collection How it worksThe business objects on the server are initialized and then serialized to XML
format. Then this code is injected into the HTML page body and sent to the client
for rendering.
Data SynchronizationNow we have the data from the server in the form of the JavaScript objects, like
Employee object.
emp.Position = "waiter"; The next logical step is sending the objects back to the server. This can be done
in two ways PostBack and AJAX.
_Put("MyEmployeesArr", EmpArrList);before the PostBack takes place. Another way to update the objects on the sever is sending the collection via XmlHttp In order to do that just call r. Send ("some string data from client");In this particular case the string 'some string data from client' will be sent to the server. The objects defined on the server (and recreated on the client) like Employee can be also sent r.Send(emp);Or the collection of the objects r.Send(EmployeesArrList);wher r is Remoting (JavaScript object defined in Rwc.js) Also the method on the server can be called Using method Invoke (also defined in Rwc.js) var Ret = r.Invoke("ReadABCD", Param1);Where ReadABCD is the method's name on the server Param1 is the object (any object that is defined on the server and registered) Example of Invoke: var Params = new Array(); Params.push(EmployeesArrList ); var Ret = r.Invoke("ReadEmployeesMethod", Params);You can create (on the client) the instance of the Employee(s) and initialize it : var Emp1 = new Employee (); Emp1.Name = "Peter"; Emp1.Age = 20; ..... And so on ...Add this employee to the collection
var EmpCollection = new ArrayList();
EmpCollection.Add(Emp1);
Add other employees and send this collection to the server [PostBack/Ajax]
On the server side the objects are received as the instances of the objects which have been previously registered. On the server side the objects sent via AJAX are received by l_DataReceivedEvent handler() private object l_DataReceivedEvent(object obj) { if (obj is Hashtable)// chech the type of the object sent from the client { return "Server: Update OK"; } return "Return a string "; // can be returned any registered object }If the standard PostBack was used, the object is received using l_PostBackDataReceivedEvent handler private void l_PostBackDataReceivedEvent(object obj) { Hashtable ht = (Hashtable)obj; // all objects on the client are packed to hashtable when PostBack is used ArrayList emps = (ArrayList)ht["MyEmployeesArr"]; Convertor.RegisterObject("MyEmployeesArr", emps); } Data TypesNaturally the data types that do not have Javascript representation are absolutely useless. For instance if you have a class with the member field defined as a TimeSpan, it can not be translated to Javascript. Supported data types
int
The framework and ASP.NET componentsThe framework has nothing to do with asp.net components; it is just a generic structured data delivery mechanism. However this framework may be very useful for such components design - it can provide the structured data for the components and can be easily integrated with component framework.2>Live demo Live demo Live demo is here
RWC_Live
Current version is 1.0.2
For better performance the framework uses Javascript compressor by Eric Woodruff
If a new version is released, it can be downloaded from www.dotnetremoting.com along with other
useful components
|
||||||||||||||||||||||||||||||||