Tuesday, March 11, 2008

Long ago I realized that I was not to be a prolific blogger, but would be the antithesis thereof; yet I still feel guilt over not doing it. Not for the sake of others, mind you, but because spouting my opinion/knowledge (or illusion thereof) is something I innately enjoy doing. Spouting my opinion here is less rewarding than doing so face-to-face - I like the immediate gratification it provides. Not to mention the fact that my opinions/knowledge are much better presented in the context of an active analog conversation than a seemingly-passive digital one. Yes, I know, I know, dialog does exist in our digital version of face-to-face reality, but it really isn't the same and I don't know that I have determined, for myself, if it is as valuable.

Anyway, I do hope to blog more if for nothing more than the opportunity to write, which is something I enjoy and need to work on.


posted on Tuesday, March 11, 2008 11:16:03 AM (Mountain Standard Time, UTC-07:00)  #    Comments [0] Trackback
 Thursday, November 01, 2007

I sure hope so! I noticed this quite some time ago when I first saw the syntax for anonymous types in C#. Being a lover of JavaScript, I quite like the ability to do things in C# in a manner consistent with the syntax I am used to using in JavaScript (my current favorite programming language, especially thanks to MochiKit). The syntax I refer to is partly alluded to in this post by Eilon Lipton (first time I have ever heard of Eilon), where he talks about substituting an anonymous type for a Dictionary<string, string>, using said Dictionary for setting attributes on HTML tags he is creating in C#. Sound familiar? I mentioned something similar in my post yesterday.

To illustrate the syntax I keep referring to, here is what I am used to doing in JavaScript, particularly with MochiKit - let's say I want to create a div element programmatically (and let's spit it out as HTML so we can better get the gist of things):

 toHTML(DIV({id:"myDiv", class:"myCssClass"}, P({ style:"font-weight:bold" }, "Hello World!")));

The HTML rendered looks like:

 <div class="myCssClass" id="myDiv"> <p style="font-weight: bold;">Hello World!</p> </div>

So, rather than declaring a the JavaScript function "DIV" or "P" (which create div and paragraph DOM elements, respectively) with every possible option as a parameter to said function (e.g. id, style, class, etc.), you simply pass in an anonymous object which contains values for the properties you care about setting. This is a fairly typical way of doing things in JavaScript, and despite the protests from the academic portion of my brain as well as other developers, I would love to be able to do the same in C#.

 

Technorati Tags: , , ,

posted on Thursday, November 01, 2007 11:38:15 AM (Mountain Standard Time, UTC-07:00)  #    Comments [0] Trackback
 Wednesday, October 31, 2007

Since learning about the SubSonic project last week, I have spent a bit of time reading up on it, watching screencasts about it, playing with it, and reading the blog of its creator, Rob Conery. In his post "How MVC, jQuery, and SubSonic Will Make You Smile," Rob talks about the use of .NET 3.5's extension methods, in particular extending a Dictionary<string, string> to return HTML for a drop down list (HTML select); here is what his version of the extension method looks like:

 

 1: public static string ToHtmlSelect(this Dictionary<string, string> listItems, string name,
object selectedValue, object attributes)
 2: {
 3:  // input formats
 4:  string selectFormat = "<select name='{0}' id='{0}' {1}>\r\n{2}\r\n </select>";
 5:  string optionFormat = "\t<option value='{0}' {1}>{2}</option>\r\n";
 6:  
 7:  // output
 8:  StringBuilder sb = new StringBuilder();
 9:  
 10:  foreach (string s in listItems.Keys)
 11:  {
 12:  string selectedFlag = "";
 13:  string text = listItems[s];
 14:  string value = s;
 15:  if (value.ToLower().Equals(selectedValue.ToString().ToLower()))
 16:  selectedFlag = "selected=true";
 17:  sb.AppendFormat(optionFormat, s.ToString(), selectedFlag, text);
 18:  }
 19:  string result = string.Format(selectFormat, name, attributes.ToAttributeList(), sb.ToString());
 20:  return result;
 21: }



 

He also mentions that (on line 19) there is another extension method that "hangs off the object class" which basically enumerates all of the properties of an object, creating a name/value pair attribute list. He doesn't give the code of that extension method, which he states "is ScottGu magic at work and something he shows in his MVC demos." I have yet to watch those demos myself, but the code is probably something akin to this:

 

 1: public static string ToAttributeList(this object attributes) 
 2: {
 3:  StringBuilder sb = new StringBuilder();
 4:  foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(attributes)) 
 5:  {
 6:  sb.AppendFormat("{0}='{1}' ", property.Name, property.GetValue(attributes).ToString());
 7:  }
 8:  return sb.ToString();
 9: }

 

This allows you to pass anonymous types, specifying what might normally be optional parameters, a la something I have been doing in JavaScript for a while using MochiKit (other JavaScript libs exploit the same literal JavaScript object notation in their function signatures). The idea is that you don't need to have a structured method signature that accepts all possible options - you can pass in what you want to specify as a part of an anonymous object. This will make more sense if you have spent any time doing this in JavaScript or want to wait a moment and read on.

Anyway, the whole reason I was even posting on this subject is because of a comment made on Rob's post by one Joe Chung, wherein he states:

"Seeing StringBuilder-generated HTML makes me sad for the future of ASP.NET. Object-oriented ASP spaghetti code is still spaghetti code."

He is referring to the string hacking done in the ToHtmlSelect extension method. I too don't care for it, and simply wanted to show a nicer way of doing this, which leverages objects we already have access to in ASP.NET which do that grunt work for us.

Here is my version of Rob's method (which I still have issues with, but for now we will let those go for the sake of staying on topic) - note, there are no literal strings for those that despise them:

 1: public static string ToHtmlSelect(this Dictionary<string, string> listItems, object selectedValue,
object attributes)
 2: {
 3:  DropDownList htmlSelect = new DropDownList();
 4:  htmlSelect.Attributes.AddAttributes(attributes);
 5:  foreach (string key in listItems.Keys)
 6:  {
 7:  ListItem item = new ListItem(listItems[key], key);
 8:  item.Selected = (key.ToLower().Equals(selectedValue.ToString().ToLower()));
 9:  htmlSelect.Items.Add(item);
 10:  }
 11:  return htmlSelect.ToHtml();
 12: }

 

Now, my version does not use the ToAttributeList() extension method hanging off of  object - mine uses something a bit different, but an extension method nonetheless (although a bit naive, it will suffice for our purposes):

 

 1: public static void AddAttributes(this System.Web.UI.AttributeCollection attributeCollection,
object attributes)
 2: {
 3:  foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(attributes))
 4:  {
 5:  attributeCollection.Add(property.Name, property.GetValue(attributes).ToString());
 6:  }
 7: }

 

My extension method hangs instead off of System.Web.UI.AttributeCollection because my version of the ToHtmlSelect() method works with the System.Web.UI.DropDownList object, which uses that as the place to store its attributes.

The other key to mine is the following extension method which wraps a technique I have been using for over 2 years to create HTML (I didn't figure it out on my own, but can't remember where I saw this exactly - I think in the Google Group for the AjaxPro.NET component back when it was simply Ajax.NET):

 

 1: public static string ToHtml(this Control control)
 2: {
 3:  StringWriter sw = new StringWriter();
 4:  HtmlTextWriter htw = new HtmlTextWriter(sw);
 5:  control.RenderControl(htw);
 6:  return sw.ToString();
 7: }

 

Note - this can be used for all sorts of Control-derived ASP.NET HTML wrapper objects to get you some HTML without string hacking in your layers of code:

 

 1: Calendar c = new Calendar();
 2: string calendarHtml = c.ToHtml();

 

Notice Joe - no literal strings! That should make you happy (I know it makes me happy!).

Now, I love hacking JavaScript as much as the next guy (been doing it hardcore for the last 2.5 years even with ASP.NET because it is too much fun!), but hacking strings to create HTML is still not that fun. Even in JavaScript I don't do it if I don't have to, preferring to use functions that handle that for me (MochiKit has some simple DOM functions for handling HTML creation).

Anyway, hope this is of some use to someone out there!

 

Technorati Tags: , , , ,

posted on Wednesday, October 31, 2007 12:38:37 PM (Mountain Standard Time, UTC-07:00)  #    Comments [11] Trackback
 Tuesday, October 30, 2007

This looks like a future cult classic:

 

 

 

Free the Pirates of the Great Salt Lake


posted on Tuesday, October 30, 2007 8:09:47 PM (Mountain Standard Time, UTC-07:00)  #    Comments [0] Trackback
 Friday, October 26, 2007

It's true - the Mormons are members of a cult! Most don't even realize it, and those that do want to deny it; it remains utterly undeniable regardless!

See for yourself - this website has the incontrovertible evidence.


posted on Friday, October 26, 2007 8:59:26 PM (Mountain Standard Time, UTC-07:00)  #    Comments [2] Trackback