Symbiatch - maailma on rikki

EF Code First, LINQ-to-XML and Other Niceties for

04.10.2012 18.29 - ohjelmointi web 

I wanted to update my skills since I haven't done much web due to my studies. So I decided I'd update Telkussa to the newest technologies. It was made with ASP.NET MVC 2, used Microsoft's AJAX libraries for data handling and client side templating etc. It works very well and is very lightweight, compared to other popular TV sites.

So, the point was to update it to ASP.NET MVC 4. Since Microsoft has been going towards jQuery for a while, they don't really support their client side templating engine anymore. They recommended jQuery templating. Which is obsolete nowadays, it seems. The next big thing is JsRender.

JsRender looked nice, so I converted the templates for it. Went smoothly, the syntax is simple and there are some examples that tell you how to use it. No problem here!

I also wanted to try out the new WebApiController, which helps in creating REST services. Before I used Microsoft's AJAX thingies that created JS code to use web services. No more, now it's nice and RESTful. So I learned that one. Must do some more work with it still, though. One thing I miss is the simplicity: before I just had to include a server-generated script and I could just call Telkussa.SetFavorite(id) and not do all this $.ajax(...) stuff. Yes, I can make a function that does it for me, but it'd be nice to get it ready-made. Maybe I'll make a patch for ASP.NET MVC someday...

I used Entity Framework for the previous version too but now I wanted to try code first. It's a lot nicer to just create the classes for the database tables and let the system determine the links between them. So simple and clean! So now I don't need any designer stuff for this either.

In the RSS part I got to test LINQ-to-XML, which are these X-classes (XDocument, XElement etc). They're so cool in creating documents, compared to XmlDocument etc. Or what do you say about this:

var res = (from x in ctx.Programs where == chan && x.stop >= now && x.start > DateTime.Today
	orderby x.start select x).Take(8).ToArray();

doc = new XDocument(
	new XElement("rss", new XAttribute("version", "2.0"),
		new XElement("channel",
			new XElement("title", "Telkussa: " + channame),
			new XElement("description", "TV-ohjelmat nyt"),
			new XElement("link", ""),
			new XElement("ttl", "60"),
			new XElement("copyright", "Tokavuh Technologies oy"),
			new XElement("webMaster", ""),
			(from x in res select
				 new XElement("item",
					 new XElement("title", String.Format("{0:HH'.'mm} {1}", x.start,,
					 new XElement("description", x.description),
					 new XElement("pubDate", x.start.ToUniversalTime().ToString("r")),
					 new XElement("link", String.Format("{0:x}",

(Yes, I know there are some nasty string concatenations, I'll get to them later :)

The main point here is: the resulting XML structure can be shown in the code and you only need one statement for the whole RSS! First I get the programs from the database. Yes, I could've put it inline too, but it's a bit messy. And if you're wondering why I used ToArray(), it's because you can't create anything but classes with default constructors from LINQ in this case. So I had to take the data as an array and then I could create the XElements from the data.

I like this a lot. It's self-documenting and simple. No more adding children to this and that and keeping track of elements in variables.

All this took me a couple of hours and now I'm back on track with the newest things in ASP.NET field. Kinda. I'm sure there are many things still to (re)learn, but it's a good start. Nice to have a project that can be upgraded and used for learning purposes.

(Note: the public site is still the old one, so no point in checking out the JsRender things from there. I'll put the new one up when I'm sure everything works and I won't mess up the mobile clients etc. It probably already works, and even better than before, but still checking...)


Comments  |  Omat jutut  |  Muiden jutut  |  Kategoriat  |  kirjaudu