Code

Logging and Rewriting Service Calls

I love web services.  I love how easy it is to wire up a JavaScript client either through jQuery or through a ServiceProxy.  I love how I can point a .net client at a service, and I get strongly typed command parameters.  And my latest love of asmx services in C# is the SoapExtension.  A SoapExtension is a class that allows you to inspect outgoing or incoming services to get at the data involved.  I use them for logging service calls (frequency, duration, errors), and for rewriting output (fixed a "double html encoded" bug, remove whitespace, rewrite SoapFaults, etc). The technique...

Testing: Methodologies, Best Practices, and Tools

I had the privilege to speak at the SEVDNUG yesterday about Testing. When we typically think of "Testing", we usually use the term "Unit Testing", yet we usually mean "Integration Testing" or "Functional Testing". This was definitely not a demo on how to use NUnit, but rather a specific look at the reasons for testing, the best practices for writing tests, and a look at popular and obscure tools to facilitate the task. During the presentation, we looked at many tools: NUnit, TestDriven.NET, Resharper's test runner, TypeMock, Selenium, Ivonna, NUnit's RowTest TestCase (I still think 'RowTest' is more descriptive than 'TestCase',...

jQuery demo at SEVDNUG

It was a blast to present deeper concepts of jQuery at the SEVDNUG this evening. We enjoyed discussing ajax in jQuery, calling web services and page methods, building and using plugins, animation, and general best practices for JavaScript. The content from the demo can be found here. Note that because of a server misconfiguration, the file will come down double-zipped. Firefox seems to download the file just fine, while IE seems to choke every time. For quick reference as well, here's the contents of the Resources page: Tools FireBug http://www.getfirebug.com YSlow http://developer.yahoo.com/yslow/ ...

IoC: What is it and why do I care?

It finally sank into my melon why Dependency Injection, Inversion of Control, and IoC Containers exist. The 'a-ha' moment came, and I wanted to share it. Here's my "explain it to your mom" definition of IoC Containers, and the back-story to make it make sense. First, we start with bugs ... Bugs: These are when a program malfunctions. They aren't good. An abundance of bugs means the program is broken, and users are grumpy. Trying to solve bugs more proactively leads us to ... Unit Testing: Automated testing of software leads to catching and solving bugs faster. ...

jQuery - LINQ to JavaScript

I had the privilege of presenting jQuery at Desert Code Camp today.  jQuery is the do more, write less JavaScript library.  Because it runs client-side, it can be used easily with any server framework.  There's nice support for intellisense in Visual Studio 2008 as well (with a bug-fix update). jQuery basically breaks down into two operations: picking things, and doing something with those things.  Picking things is done via a CSS / XPath strign describing the DOM nodes you want to do.  This returns an array of DOM elements (wrapped nicely in a jQuery object) that get passed into each of...

Conditional Compile based on .net framework version

Ok, I've been after this one for quite a while. I want to include stuff like Moth.Linq and ExtensionAttribute in .net 2.0 compilations, but exclude them from .net 3.5 compilations. In effect, I want to cross compile to .net 2.0 and .net 3.5. Not only do I want to do this from within Visual Studio, I also want the Continuous Integration server to do it too. Tall order? Yes. A bit of background: - Moth.Linq is a mechanism for producing LINQ functionality in assemblies compiled in Visual Studio 2008 targeted to the .net 2.0 framework. You...

Thinking in JavaScript - A-ha!

I had the great honor of presenting 'Thinking in JavaScript' at the Java Users' Group last night. I love leading a discussion of learning, and this was a great example of it. I was even asked during conversation afterwards if I was a member of 'the club' [the Java Users' Group -- JUG]. Well, do I have to know Java to be part of the JUG? I guess not. What a fun and welcoming crowd. Our premise was thus: you know object-oriented development. You know C-style syntax from either C, C++, Java, C#, or any...

CSS A complete Journey at Java Users Group

I was asked on short-notice to fill in for the Java Users Group, and to present CSS. I have so much fun speaking, teaching, and learning with fellow developers that it was a no-brainer to say yes. However, what was so unique to me is I'm not a Java developer. I'm a C# / ASP.NET developer. It really emphasized to me one of the really cool truths of internet development: there are 1000 server-side technologies and web servers you can use to host your content, but at the end of the day, there are just 3...

Validator Callout doesn't display on second control after fixing first control

There's a bug in the Validator Callout in the AJAX Control Toolkit documented by bugs 10900, 12342, 15698, and 11701 on the Code Plex site.  All 4 bugs describe the following undesired behavior: Pull up the AJAX Control Toolkit Sample site's Validator Callout example (here) Push submit Note that both fields are invalid, and the first field shows the Validator Callout Add text to the first TextBox Push submit The Validator Callout for the top text field is gone, the Validator Callout...

Compile time reminders

For ages I've wanted to be able to put a comment of some kind in my code that I could get to come up during a build.  "Hey, you haven't finished this" or "This is an assumption we need to run by the suits" or "you dork, how could you even think to do it this way" or similar -- notes to myself and others that there is work left to be done here. Well, after trying unsuccessfully to look towards subclassing or hacking the Obsolete attribute the answer was handed to me: 1. I build comments in...

Thinking in JavaScript for the C# Developer

I had the great privilege and honor of presenting "Thinking in JavaScript" at the SouthEast Valley .NET Users Group this evening.  All in all, I had a ton of fun.  If anyone else got anything out of it, all the better. The main point of my discourse was that JavaScript isn't hard by any means, it just takes an introduction to how it thinks.  Microsoft's ASP.NET AJAX has made it feel much more like the .net languages we're used to.  But there are still some inherent differences between a compiled language and a dynamic language. The main focus of the discussion is...

DropDownList's SelectedValue in JavaScript

Ok, I've forgotten this more times than I care to remember, and each time, I have to re-Google it.  I hereby commit it to digital memory: If you have a DropDownList control like so:      <asp:DropDownList ID="mycontrol" runat="server"></asp:DropDownList> and you want to get it's selected value in JavaScript, do it like so:      var control = $get('<%= this.mycontrol.ClientID %>');      var selectedvalue = control.options[control.selectedIndex].value; And there ya have it.  I hereby have forgotten it already... :D Rob

Update Statistics for all tables

Statistics in Sql Server are awesome for improving performance of queries.  There's a lot of voodoo behind the scenes, but the end result is fast, and that makes users smile. From time to time, table and index statistics get stale.  Updating statistics regularly is a good use of a sql agent job or a db maintenance plan step.  In the cases where I want to do it manually, I frequently have to search for a way to extract table names from sys tables, construct the update statistics command, and execute. The 'a-ha' moment came.  The answer is in.  It need not be...

JavaScript namespaces without ASP.NET AJAX

In ASP.NET AJAX, you create a namespace by calling Type.registerNamespace('My.Namespace.Of.Choice'); The necessary object dictionaries are built, names resolved, everything is groovy.  But what if you can't use that for what ever reason... I'm working in a project that can't use the ASP.NET AJAX Client Libraries for reasons that are irrelevant.  I want to create a namespace, so my objects don't collide with others.  The answer turned out to be uber-simple: var MyNamespace = MyNamespace ? MyNamespace : {}; ( idea spurred from an irrelevant post at http://www.lixo.org/archives/2007/09/14/javascript-put-everything-in-a-namespace/ ) Now, I need to do that for each level: var My = My ? My : {}; var My.Namespace...

RegEx match content unless it is inside quotes

I recently had the need to match content outside quotes, but avoid content inside quotes.  For example, I have content like so: this is content to replace, "but don't replace this string, it's \"important\"", but feel free to replace this. The answer came from http://thisworkinglife.blogspot.com/2006/04/net-regular-expressions-finding.html supported by http://regexadvice.com/forums/thread/36369.aspx and http://www.regular-expressions.info/lookaround.html and http://aspnet.4guysfromrolla.com/articles/022603-1.aspx The expression is added to the beginning of any other expression.  Here it is: (?<=^(([^\"]*(?<!\\\\)\"[^\"]*(?<!\\\\)\"[^\"]*)*|[^\"]*)) It means: "... and before it is an even number of double-quotes not preceeded by a \ or no quotes at all before it ..." If you've never used look-ahead or look-behind expressions, they're incredibly cool.  I understand...

UrlEncode vs. HtmlEncode

The difference between these two commands is obscure, and plagued me for some time.  It just gelled in my head, so I thought I'd share. HtmlEncode -- makes it safe to display user-entered text on a web page.  '<' and '>' are turned into &lt; and %gt; UrlEncode -- makes it safe to work as a url.  ' ' is turned into '+' and a bunch more. Well, which to use in an HTTP POST?  The magic bullet comes from http://aspnetresources.com/blog/encoding_forms.aspx "If you're wondering which one you should use in an HTTP POST, well just think of POST data as an extremely long query...

Object Oriented JavaScript

Desert Code Camp 2007 really rocked. I taught a class on Object Oriented JavaScript -- basically how JavaScript is different from the C# and ASP.NET we're used to, and how to think like JavaScript. The slides are available here. Thanks everyone for coming and making it a great show.

Curly Braces -- the holy war

I sense I'm spurring a holy war discussion when I raise the issue, but some have asked why I code with curly braces on the same line rather than a new line.  My answer is simple: readability. The goal with any code is not solely to accomplish the problem at hand, but to make the code legible for the casual reader who will come by it later on.  Maintainable code is written legibly. As an example, consider the Visual Basic type "variant".  It defines a variable as "something".  Um, gee, thanks. Dim myvar as Variant myvar = "1" myvar = 1 myvar = 1.0 myvar = Workbook.Worksheets[0] What is...

CSS Footer

This is a technique for creating a CSS footer that will snap to the bottom of the content or the bottom of the page, which ever is lower.  This all without any JavaScript. Download the code here. I learned the technique through "Google University" [read: I Googled it until I found an answer]. I wish I could give credit, but I've since misplaced the link I used. Thanks to whomever. The technique involves creating an "everything but the footer" div, and setting it's min-height to 100%. (For IE 6, set height to 100% -- compensate for IE 6's...

Code Signing: two worlds defined

I've always been a fan of code signing. There's "signing" to give the assembly a strong assembly name, and there's "signing" to verify the application hasn't been tampered with. (It irks me that they're named the same.) It finally gelled in my head the difference between the two. For my own reference, and for the benefit of others, here is a description of each: Strong Name Reason: This is necessary to install assemblies into the GAC and to include a library in a signed project. It is specific to managed code. Benefits: This insures the library doesn't conflict with...

PostScript is so elegant

Ok, most people don't code in PostScript. Granted, like SQL, it's one of those languages that's usually coded by a program written in another language. But it's so incredibly elegant! PostScript is like coding in reverse polish. Put as many parameters as are needed on the stack, then run the function. The answer is on the stack, ready to be the parameter in the next function. Need to move stuff out of the way? 'exch'-ange arguments. A google board post led me to the most elegant auto-paging and auto-line-wrap function. The auto-paging is easy: when...

Generics at Desert Code Camp

Desert Code Camp was October 28, 2006. It rocked! Among other things, I taught 'Generics'. In hind-sight, it would've been helpful to specify this was .NET Generics. I had quite a few questions on how this applied to Java. Sadly, I haven't a clue (yet). For those of you who missed it, it was a great adventure. The main thesis of the presentation included the following points: Generics give you type-safe collections & nullable value types. Using these types gives you compile-time type checking, and performance benefits. Incorporating Generics into new and existing projects is...

Intro to CSS at Desert Code Camp

Desert Code Camp was October 28, 2006. It rocked! Among other things, I taught 'Begining CSS'. In hind-sight, I wish I had taught the 'Advanced CSS' class too. The class went very well, and many said they learned a lot. For those of you who missed it, it was a great adventure. The main thesis of the presentation was you can get into CSS at any level you'd like, exploit it's strengths, and leave the rest. Hopefully, once you've got a feel for it, and it becomes a valuable tool, you'll see more value in...