-
Twin Cities Code Camp
-
Yesterday was the first
Twin Cities Code Camp
. The event was attended by almost a 150 developers. Amazing turnout to say the least. I presented a session on Generics Support in .Net 2.0. The session was standing room only, amazing to see how many people are interested in this. I also attended sessions on Ajax programming, implementing REST architecture and Ruby on Rails with RubyCLR. The sessions at Code Camp where a nice mix of .Net and Java development.
The Ruby presentation was really interesting. It has occurred to me that while VB.Net and C# are the predominant development languages, in the future we are likely to switch to dynamic language to minimize the amount of code we need to write by building on a richer platform then we have to today.
The code and demo projects are available
here
. You will need to register with this site to download the samples.
-
Playing with JSON
-
I have been playing a bit with JSON ( JavaScript Object Notation). It's pretty cool stuff. Thanks to
http://json.org
I was able to find some pretty cool support widgets. The first is
NewtonSoft JSON library
. While a JSON serializer is part of the ASP.Net Ajax library, you may not want to unleash beta software on your web site or clients. This is a very well written library and offers a lightweight high performance utility. On the browser side, json.js library adds prototypes to the string ojbect to support JSON serialization and de-serialization.
What is JSON? JSON is a compact javascript notation for representing objects. The syntax is very simple. {}'s delimit an object, [] delimit arrays and properties and their values are denoted as "propertyName" : "propertyValue" and name value pairs are seperated by commas.
var myAddress = { [ "street_address":"123 Any Place", "street_address":"Penthouse"], "city":"Emerald City", "state":"CA", "zipcode":"55555" };
within Javascipt, I can then script with this object in a very rich manner.
alert( "myAddress: ", myAddress.street_address(0) );
So that's cool. By using the eval statement, I can create rich javascript objects on the fly as well.
var myAddress = eval( '(' + myJSONstring + ')' )
Ok, so that said, why am I getting jazzed about this? One of the patterns for ajax applications is to implement a model view controller. From your ajax calls from the client page, you reference a REST (REPresentational State Transfer) page ( or httpHandler) that updates a model and then sends back the appropriate response to the client. This requires some state between the main page and the REST pages and this is typically Session State is used for this. Session is not a bad option, the amount of memory per session is not very large, but it's only good a single server deployment. Once you add a web farm, you have to move your session out of process ( State Server or SQL Server). All of a sudden, this lightweight mechanism is not so light primarily do the binary serialization overhead.
So the question is how can this be done to avoid using Session objects. Store the session object on web client is one-way. You could declare hidden fields on your web page to hold this information, but if the object has a lot of properties and collections this can be very messy. But it can be done with single field using JSON. Simply create a JSON string to representing your model object and store that in a hidden field on the web page. When you make you Ajax call, add the JSON string as a parameter in the web request. De-Serialize the JSON object on the REST page, make your changes to the Model object, serialize it back to JSON and return it back to the client. I now have a rich object that works very much like session but is stored completely on the client page.
So where does the JSON.Net dll fit in, it provides the JSON serialization and de-serialization support for .Net objects. The beauty here is you don't have to write any additional code into your objects to support this.
Serialization is a simple as:
Dim strTmp as String = JavaScriptConvert.Serialize( myObject);
De-serialization is equally as simple:
dim myObject as myObjectType = DirectCast( JavaScriptConvert.DeSerialize( strJSON, myObjectType), myObjectType)
Conclusion, if you are looking for a high performance way to move objects between your server and your web client, JSON has many advantages over XML or other strategies. Always nice to have extra tools and methods when trying to solve coding challenges.
-
Whirlwind of .Net 2.0 Presentations
-
Whew, it's finally over. So far August has been an extremely busy month. I have presented at three different .Net user groups in three different states. The first was in Des Moines, Iowa on August 2nd. The turnout was really good, got to meet a many very cool people and all with a passion for .Net. August 3rd was the Minneapolis .Net User Group, again another fun people. August 16th, I presented at the Chippewa Valley .Net User Group in Eau Claire WI. and again another group of fun people.
I did an architectural deep dive into .Net 2.0 framework, showing the benefits of separating business logic from presentation logic. For the basis of the demo, I wrote a demo application that works with RSS feeds. The focus of my talk was on populating object collections from various data sources. In this case, the feed information could come from the internet via an XML feed, or the feed could be populated from stored procs in the database.
The basic layers of the application are the data storage tier, (the database), the data accessors ( stored procs), the data access layer ( DAL). This consists of static methods using the Microsoft Enterprise Data Library. Up from that is a collection of business objects modeling the RSS feed information. A custom generic collection class I wrote is put to work to manage the collections of items.
The Visual Studio 2005 solution consists of two projects, one for the business logic and a second for the demo web site. You will need to create a virtual directory for the RSSDemo web site. I have also included a SQL script that will build all the tables and stored procedures used by the demo. Armed with this and a bit of luck, you should be able to play with my little demo.
The web site demo code is available for download
. The business logic demo code is available
here
.
Note:
Since a lot of folks may be new to the site, in order to download the files, you will first need to register on the site. Please follow the Join link in the upper right hand corner.
Enjoy.
-
Generic Binary Serialization
-
Almost all of the examples for generics are examples dealing with collections. But generics are applicabale in many other type of situations an example of which is binary serialization. Imagine being able to save a rich object to a disk file with just a single line of code. For example:
GenericSerialization.ToBinaryFile( currentChannel, "currentChannel.dat");
And to reverse the process
RssChannel rc = GenericSerialization.FromBinaryFile("currentChannel.dat");
This will work for any class that has a empty constructor and has been marked as Serializable. The source code is available for download
here
.
-
Having a bit too much fun with Generics
-
One of the coolest things in .Net 2.0 has to be generics. What a welcome addition to anyone's toolkit. Over about the last six months I have been seriously exploring what is possible. Code re-use is perhaps the greatest benefit. Collections have never been richer or easier.
One of the first things I wrote was a generic collection base class. What is unique with this class is that a collection of like objects can be created with a single line of code. For example:
GenericCollectionBase feeds =
new GenericCollectionBase( RSSDBAccess.GetRSSFeed(0));
The class I have been working with as a number of interesting constructors, in this example, the GetRSSFeed method is returning a DataTable, My collection base class is able to interate through the table and create a feed item for every row in the table. Other constructors have been defined for example to accept a DataRow object and a DataRelationName, in this case, the GetChildRows method is called and in a single line of code, I now have a strongly typed collection of the child ojects. If I was working with an purchase order for example, this collection would represent the line items.
With my days of writing collection classes behind me, it makes sense to contine to develop and enhance the single instance of collection code. To this base collection, I have added sorting, searching and paging.
The code is a available for download here
GenericCollectionBase Library
-
Thanks to Ryan
-
Finally a blog of my own. Many thanks to Ryan. I think this is a site I will feel right at home.