Archive for March, 2005

Working around the worst of the .Net 1.x years

I came across this post on our link blog by K. Scott Allen, where he details the worst of the .Net 1.x years now that .Net 2.0 is on the horizon. #3 on his list is the tight integration of Visual Studio and IIS, if you’ve ever created a web application using Visual Studio, you’ll understand right away some of the gripes that he points out, including FPSE, security issues, versioning, and disappearing folders. I know that for me, learning .Net was made much more difficult having to overcome the exact problems that Allen describes, and I think that it does the developer a disservice by hiding them from the inner workings of how a .Net web app works, with the assemblies, the bin directory, etc.

The way that I make my web apps now is so much simpler. I don’t create a web project in Visual Studio, instead I simply create a class library solution, very plain, no extra directories, references, no nothing. Then I open Dreamweaver (actually VWD Express lately) and use that editor to create/modify my .aspx files. This way I can compile my code into a .dll, copy the .dll into my web apps bin directory, and I’m all set. This gives the developer more control over the build process, versioning, and it forces more understanding of how asp.net works. I’ll admit it slows you down at first because you cannot hit F5 and run your web app, but it pays dividends in the long term by affording the developer the control, rather than delegating the control to VS.  Another benefit of developing in this fashion is that I can actually not use VS at all if I don’t want to.  I use nant to build my projects anyways, so I have completely decoupled any dependance on VS to manage my project at all, which is important to me.

I guess my point is that I agree with Allen that the tight coupling between VS and IIS is not good, (VWD Express does an excellent job however), but by having a deeper understanding of how an asp.net application works, you can easily work around the shortcomings of the development environment.

Hijacking __doPostBack

I’ve often found myself wanting to do run some javascript before my form posts to the server, which is rather difficult to do when you have a .net server form.  It always auto generates that __doPostBack javascript method that submits the form. Placing an “onsubmit” handler on the form doesn’t help because that event doesn’t fire when you submit a form using javascript.

I cam across this post a little bit ago that solved the problem perfectly.  Its so simple, I’m embarrassed that I didn’t realize it before.  Since every function in javascript is also just a variable, you can reassign the variable anytime you like.  You can test this by creating a function called “test” with some trivial method body.  Then, at the bottom of your page you can write “alert(test);”  and in the alert box, you will see the method body.  So what Victor is doing in his example is storing the generated __doPostBack method into a new variable, then overwriting the method with his own method, while inside the new method, he calls the old method, so that the form still gets posted, did you follow all that? :)

Anyways, its a nifty trick for anyone who needs to insert some custom javascript code for your postbacks, just thought I would share.

My Take on “Ajax”

There has been a lot of talk lately about making web applications
more like client applications using javascript and asynchronous xml web
requests. I first noticed the buzz after reading this article at Adaptive Path by Jesse James Garrett.
Garrett does an excellent job of explaining how web apps can take
adavantage of asynchronous requests to give the user a better
experience.

Then on Thursday, Mary Jo Foley wrote her own article
about this, linking to the Adaptive Path article, and speculating on
the future of Microsoft’s fat client strategy.  She wonders if
these improved web applications will be good enough for people to not
want to revert to client apps, instead just continuing to push the
envelope with web based technologies.

Scoble and Joseph Cooney
have also weighed in, and they both think that while these web apps are
cool and interesting, they cannot be as good as client apps. Cooney
gives some excellent reasons, including the fact that javascript is not
compiled or statically typed, and that unless you use some sort of
obfuscation, your javascript source code is freely available.

We have used this technology since late last summer in building our
current custom web application that manages contacts, email, calendar,
leads, and host of other business processing data. From my experience I
also think that using asynchronous calls are very cool and they do
enhance the user experience a ton, however, I do not think that this
can replace client applications completely.  Javascript is still
slower than compiled client code, and if the user is ever offline,
javascript cannot handle the situation gracefully.  Unless you are
using some ActiveX, you cannot touch the users local disk, so you
cannot cache large amounts of data locally.  When we test our
application on slower computers, the browser consumes much of the cpu
when it is rendering complex controls, and while we certainly aren’t
the best at using this technology, Outlook has no problems running
smoothly on the same computer.  Client apps have the
advantage of having the entire local machine as a resource, getting
benefits like threading and IO, while any web based approach has to
deal with the browser as an intermediary, which is inherently less
efficient.

Some of the benefits of using the web are ease of deployment and
cross platform compatibility and these benefits have never been shared
with client applications until recently.  Now, you can run
your .Net applications on Unix, Linux, and Macs by using Mono,
an open source implementation of the .Net framework, including asp.net,
written in C#. Deployment is also getting much better with the release
of ClickOnce
If you can update your users with a new version quickly and seamlessly,
as MSN Messenger does, then I would pick to use a client app over a web
based one any day.

Automate your BlogJet publishing

A few days ago, Raymond wrote
about how he wanted to be able to write a bunch of blog posts, but then
schedule the publishing, so he can space the posts out.  I do the
same thing, as I will sometimes have the time to write a few, but I
don’t want to publish them all at the same time, or sometimes you
want to write an entire series, and then publish one a day.

Raymond suggestion prompted me to write a little window service that
does just this.  You can download it here.  All you need to
do is install the service, set a few application settings in the
.config file, and you can start automating your blog posts.  This
only works with the SimpleBlogService that .Text has, so if your blog
runs on .Text, or any other blogging application that has the same web
services, you can use it.  You also must be using BlogJet,
as this is the only application that the service can read right
now.  I’ll put the source up there too, so people can modify it
for their needs.

All you do is place your completed drafts inside a folder, where the
name of the folder is the date/time that you want your post to be
published, i.e. I could have a post called “post.bjd” inside a folder
named “3–15–2005 5_00 PM” and the post will get published at 5 PM on
March 15.  There is one warning about the names of the folders,
since a colon “:“ is an illegal character for file names, I
replace underscores “_”, with a colon, so this facilitates putting
times into the date, other wise your posts would all get published at
midnight :). 
So do not use an underscore as your date separator, i.e. this is an
illegal folder name “3_15_2005 5_00 PM”, because all the underscores
will get replaced by colons, and it won’t recognize the resulting
string as a date.

UPDATED: Fixed link to download

Learning project setup

After reading this post by Mike Roberts, I used his method to set up a new project I was starting.  One of the best pieces that I got from this article was to include my building/testing tools right into my versioning.  At first I would just put the basics, my source code, any project files, web forms, etc.  Now, by including nant and nunit, you can get someone new setup to use your project really quickly, and theres no issues like “which version of nant is this .build file targeted for?”, because whatever version of nant is checked out is the version you need!  Excellent post, very helpful to me, subscribed!

Caching of static files

We had a caching problem at work awhile back, and I wanted to detail our solution here and see what people think.

The problem was with javascript files, stylesheets, and static xml files.  At the time we were updating our web application nearly everyday, so were adding/editing these files pretty consistently.  When people would log in, they wouldn’t see the changes in these files, because the browser was caching them, especially if the styles or javascript were not inline. 

Now, in other places in our application, we would append the current date to some urls in order to make sure the user would get a fresh copy of the page, by appending the date to the url, it guaranteed that the browser wouldn’t have a cached page that matched that url.  We couldn’t simply use that same technique for these files however, because we WANTED them to be cached, but we also wanted them to be updated as soon as a new one showed up on the webserver. So what we did was add a “BuildID” key into our web.config file, and added a “BuildID” property to our base page class.  Everytime that we included a script, stylesheet, or xml file, we would append “?BuildID=<%=BuildID%>” to the url of the file.  Now everytime that we make updates to the files, we increment the BuildID variable in the web.config so that the browser will see a new url for the files, and grab the updated copies.

Heres an example:

<script type=”text/javascript” src=”commom.js?BuildID=<%=BuildID%>” ></script>

This has worked out perfectly for us, the files get cached, and when we update the files, we never have people who don’t get the new copies!

This seems kind of hacky, but it works, what do you think?

Scoble might know me

Google defections

Okay, I know that this has been beat to a pulp out there, but I
wanted to give a little more global view of this.  Most people
have read that Mark Lucovsky
has moved to Google recently and there is a lot of speculation about
what he’s working on, a hailstorm clone and a Google OS are the
obvious choices since that is what he specialized in at Microsoft.

Some people are not so sure that this is whats going on.  However, if you recall other people that Google has hired in the past year:

Joe Beda, a MS developer working on their new graphics engine, Avalon, and, more interesting, Adam Bosworth,
formally of Microsoft and BEA.  Bosworth was the leader of MS’s
XML strategy and a kind of pioneer of web services.  In his blog post about moving
to Google Adam talks about how he is excited to work on services that
millions of people will use.  Since he went to Google, they
haven’t really released anything ground-breaking, GMail was already out
there, which was their last major release IMO.

These hires, and the fact that Google hasn’t had, IMO, a major
release of any new service, leads me to believe that they are working
on something pretty special, special enough to lure all these people
away from their otherwise successfull careers, these are the type of
people that don’t interview for jobs, instead they interview the
company that is trying to hire them.

Now playing: 12 - Smash Mouth - Defeat You.mp3

Code Generation & Partial Classes

I have a code generator that we use to map our database tables into objects.  The code generator works in the typical way, suppose you have an Employee table, and you want to map it to an object.  Your generator creates a file, Employee_Generated.cs, which contains all the generated data access code.  You then manually write a class, Employee.cs, that inherits from Employee_Generated, and there you can put all of your custom code for your employee object.

I’ve been updating my code generator to use partial classes and generics in whidbey, and I ran into a problem.  The generated code puts the initialization code into the default constructor, i.e., inside Employee_Generated().  This was fine when I used the inheritance model, because I could have my own default constructor in my manually coded class file.  Now, using partial classes, I can’t have my own custom constructor, because both files represent the same class! 

What I did to fix the problem, was to put my initialization code into a protected virtual method call Init(), and then I can initialize my object from different places as needed.  In hindsight, this is probably the way that I should have structured my generated classes all along, its just that I never had a problem with it until now.

Now playing: Oasis - 09 - Be Here Now.mp3

More serialization

Amongst the talk about xml serialization, I thought I’d add to the conversation.

I’ve mentioned in previous posts about using xsd.exe to generate c# class files that the XmlSerializer can convert to xml.  I’ve been using these classes in our web application that makes use of the XmlHttpRequest javascript object.  We make the javascript calls to url that return xml and then parse the xml to run the application without having to post the page back.  It works really well in some situations to give the user a real software feel, as opposed to a web page feel.

After learning how to use xsd.exe, along with .xsd files, I wanted to take what I had done one step further. Typically, you would use the c# class files from xsd.exe to serialize your object, and then deserialize the data on the client side, using the same classes. Since I deal in a c# to javascript environment, I couldn’t make use of the deserialization on the client side. What we used to do was write a javascript function that could parse the xml that comes down, and convert it into javascript objects.  It works pretty well, but we end up spending a lot of time writing these parsing functions, as opposed to real code.

So what I ended up doing, was writing my own console application, jsxsd.exe, that does exactly what xsd.exe does, except it writes out the code to deserialize the xml into javascript objects.  This is WAY cool for us, because now when we update our schemas or add new ones, we can immediately get not only the c# class files, but also the javascript objects that go along with them.  The original impetus for the idea was from this blog post, where he wrote an actual javascript implementation of de/serialization, using reflection and the whole bit, and its really, really cool, except that its written in jscript, so it will not run on firefox, which is a huge requirement.

jsxsd.exe is not fully complete yet, I just got it working for the schemas we have, and it doesn’t do any type checking, in the javascript objects, every property is a string, no numbers or dates yet, but it does get all the related xml data into arrays, I’m very pleased with how its working out so far, its great.

BTW: How do you guys post your code in those fieldsets? I’d like to post some code, but I’m not sure how you get it into that format, thanks