David's Blog

A technical blog about programming and technology.

Friday, December 21, 2007

Generating html with Xlinq?

As web developers we have, at some point in our careers generated html on the server.  It always gets messy especially when your are generating a table from data. Its always done with messy strings! What about setting attributes...   string row = "<td class=\"name\">" + value + "</td>" .. what a pain.
Xlinq is new in .NET 3.5 (The X stands for XML), you can read more about it here. I found(i think) a much better way to generate html on the server using asp.net. I needed to make an ajax call to the server that returned a filtered and paged table based on the parameters I passed.

Here is a screen shot of what the code looked like:

image

And generating the rows of the table:

image

The helper methods, Html.AsLink creates html markup("<a href...") as an XElement otherwise the special characters are escaped.

Pretty neat huh?

Thursday, November 15, 2007

Update, Silverlight 1.1 and Senior Design

So it's been a few months since I posted . I've have a very successful summer and I'll be joining Microsoft next year after I graduate. What's new? Well first things first my senior design project has changed from language design to a programming contest website... Pretty drastic, I know. I've been active with my school's ACM programming contest team for the last 3 years and we thought it would be cool to host an "Online Judge" at our school. So what exactly is an "Online Judge"? Sites like Topcoder, SPOJ, and ACM TJU that have automated judging software; users sign up and submit problems, compete in online contests, share knowledge of algorithms, design etc...

We aim to have a mixture of features from the sites mentioned above, but also have a stronger community. The site is currently live at http://fudge.fit.edu, but is password protected for now, we plan to have open beta testing in a few months. 
We currently accept submission in the following languages:

  • GNU C\C++
  • Java 1.5
  • C#
  • VB.NET
  • Python

With plans to add Haskell, Ruby, Perl and PHP in the near future.

We have, 3 computers that support the fudge framework and website, MILK, SUGAR and BUTTER, the compilation machine, web server and database respectively. The database is SQL Server 2005 running on Windows 2003 server.  Fudge is developed in Visual Studio 2008 Beta 2 using  ASP.NET and C# 3.0. For source control we are using TFS 2008 Beta 2.

The Fudge website uses the Fudge Framework. The framework acts as a service provider to pluggable modules. It handles uploading source code, remote compilation, and remote execution. The judging module, for instance, provides a bridge between the website and the framework. Source code are update the to the database, and a judge request is sent to the module. The module then uses the framework capabilities to compile the code, and runs test cases. The results are the saved to the database. In the near future, the framework will also provide a database interface, allowing modules not to establish specific connections and execute queries.

So where does the Silverlight come in? Topcoder has a java applet, which can be used to do practice problems and is used in actual contests. The applet allows users to code directly in the online arena, submit problems, run sample test cases, and chat.

In the last 2 weeks I've been working in Silverlight 1.1 alpha to try to make an online IDE for the fudge website. With the current bits in the alpha, this is a pretty tough to task to achieve,  i.e lack of textbox control, and many other useful controls.

The first version of the IDE control was an attempt to get it working initially, so the code was pretty disorganized:
you can view a demo here.

The second attempt was to create a base textbox control and derive other controls from it so I could find bugs when trying to use the textbox in other scenarios. Here is a demo of a command prompt control that derives from the TextBox control.

The control is still being refractored and optimized but you can take a look at its current state here.
Features missing are:

  • Page up
  • Page down
  • Selection
  • Copy and Paste in firefox

Saturday, August 4, 2007

Anonymous types and Page Methods

I've been playing with asp.net Ajax for the last couple of days in VS Orcas beta 1(haven't downloaded Beta 2 as yet), and I wondered if it was possible to return an anonymous type to JavaScript using the PageMethods feature. This is a very useful for web scenarios, for example you may want to return a list of tuples and bind a list of key and value pairs to a drop down list. Surprisingly it just works.

 

public class Person {
public string Name { get; set; }
public int Age { get; set; }
public int Id { get; set; }
}

public static List GetPersons() {
return new List { new Person { Name = "David", Age = 20, Id=1 }, new Person { Name = "John", Age = 10,Id=2 },
new Person { Name = "Harry", Age = 30,Id=3 }, new Person { Name = "Peter", Age = 40,Id=4 }, new Person { Name = "Jodi", Age = 15,Id=5 }};
}

[WebMethod]
public static object GetData() {
return from p in GetPersons()
where p.Age >= 20
select new { p.Id, p.Name };
}

On the server side the static method that is exposed to JavaScript is marked with the WebMethod attribute. The return type of the method is object since we can't name the return type. The result of the query expression is an IEnumerable< class { int Id; string Name }>, on the JavaScript side we get a collection of these nameless types that we can iterate over and populate a drop down list. Try it out, I thought it was very cool they way we could leverage new C# 3 features to enhance other programming domains.


 

<script type="text/javascript">
function populate() {
PageMethods.GetData(function(res) {
var list = $get('list');
var options = "";
for(var idx in res) {
options += "<option value='" + res[idx].Id + "'>" + res[idx].Name + "</option>";
}
list.innerHTML = "<select onchange='showValue(this)'>" + options + "</select>";
});
}

function showValue(list) {
alert("Id = " + list.options[list.selectedIndex].value);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager EnablePageMethods="true" runat="server">
</asp:ScriptManager>
<div>
<a href="javascript:populate()">Populate Drop Down</a> <br />
<div id="list" />
</div>
</form>
</body>
</html>



Technorati tags: , , ,

Wednesday, June 27, 2007

Experimental Language?

After exploring ideas about my senior design project, a friend and I have come up with an idea to produce a programming language. I know your probably like.. yea right do we really need another language? But its clear that there is always room for innovation in programming languages and language design in general. So far we've decided that it's going to be a .NET language and it's going to be C style. One very interesting idea I had was based on a statement I read in C# Team Wes Dyer's Blog, "One of the criticisms leveled at design patterns is that they are simply formalisms to address weaknesses in programming languages.  They require the human compiler to generate code whenever a specific recurring problem is encountered that cannot be solved directly with language support" you can read the post here. This makes me think, "how would a language be if it was based purely on syntactic sugar?" There will still be support for doing stuff "manually" for those die-hard programmers, but the majority of developers out there, want to be productive; this is a major strength of C# and Java (Not dissing C++ :)).  Managed code is more "manageable" in general. The LINQ project was a major effort to bridge the gap between data and the programming language. The convenience of not having to worry about memory allocations, and cleanup is nice. Most programmers want to avoid all the plumbing to achieve a task; they want good libraries and a good language that supports those. So our attempt will be to based the programming language on the developer, and make a new and familiar syntax for expressing algorithms.

Friday, June 8, 2007

Microsoft Surface

I haven't posted anything in a while cause I've been busy with work. Its fun up here in Redmond and I'm really enjoying my internship as a developer.

So the Microsoft surface is the next big thing. Its pretty amazing, reminds me of minority report. We see all these futuristic movies and ideas of what technology might be like in the future and it seems we are getting closer every day.. take a look Microsoft Surface

Tuesday, May 15, 2007

I've Arrived!!

Finally after a great weeks vacation home, I've arrived in Seattle. I'm staying in a studio apartment in downtown Bellevue. The Xbox is setup, I have wireless Internet, a bed, kitchen and bathroom, what more could you ask for. Tomorrow is my first day of work and I look forward to it.

Saturday, May 5, 2007

Finals are over!

Thank god finals are over, I'm packing my stuff getting ready for my one week vacation in paradise (Barbados).  Its been a grueling week and I'm just happy to spend this week relaxing, not thinking about having to study until work starts. I'll have some interesting things to post during the summer hopefully :D. Two of my friends were successful in the first round of Microsoft interviews and are getting a free trip to Seattle so I hope to see them when I am there and wish them the best of luck.