Hopp til innhold Hopp til undermeny Hopp til kontaktinformasjon
 
 
Faglig

Blogger i 2011


Syndication - icon

The Indispensable IDisposable

av: Einar Høst
The using statement in C# is a peculiar spoonful of syntactic sugar. It is peculiar because it's tailor-made for a particular interface in the .NET framework. (i.e. IDisposable ). Hence in the C# standard , you'll find that the semantics of using is defined in terms of how it interacts with that interface, the existence of which is sort of assumed a priori. So the boundary between language and library gets really blurred. As you well know, the purpose of using is to make it 1) more convenient...
12.05.2011

Enumerating enumerables

av: Einar Høst
You know when you're iterating over some IEnumerable , and you need to associate the items in the IEnumerable with a sequence number? In Python, you could do this: In C#, however, you're forced to do something like this: var items = new [] { "zero", "one", "two" }; int no = 0; foreach (var it in items) {   Console.WriteLine("{0} => {1}", no, it);   ++no; } Yuck. I feel dirty each time. It's two measly lines of code, but it sure feels like I'm nailing something onto the loop that doesn't belon...
07.05.2011

Patching polymorphic pain at runtime

av: Einar Høst
In the last post, we saw that data binding in ASP.NET doesn't support polymorphism. We also saw that we could mitigate the problem by using simple wrapper types. Writing such wrappers by hand won't kill you, but it is fairly brain-dead. I mentioned that an alternative would be to generate the wrappers at runtime, using reflection. That actually sounds like a bit of fun, so let's see how it can be done. If nothing else, it's a nice introductory lesson in using Reflection.Emit . Comeback of the...
28.04.2011

Polymorphic pain in ASP.NET data binding

av: Einar Høst
I recently found out - the hard way, of course - that data binding in ASP.NET is broken with respect to polymorphism. It's not consistently broken, though - it depends on the particular control you're using. Makes life as a programmer that much more interesting, doesn't it? Let's consider a very simple example. We have a type hierarchy like the following: There's an ICanine interface, with implementing classes Wolf and Dog . Chihuahua is a subclass of the latter. The code looks like this:...
17.04.2011

A simple LRU cache

av: Einar Høst
Disclaimer: copy code from this site at your own peril! So yeah, I wanted to share this simple LRU cache I put together. LRU means "least-recently-used". The idea is that you want to keep the hottest, most useful elements in the cache, so you evict elements that haven't been used for a while. It's a useful approximation of Belady's algorithm (aka the clairvoyant algorithm) in many scenarios. Wikipedia has all the details .Of course, my LRU implementation (written in C#) is extremely simple,...
13.04.2011

UTC now!

av: Einar Høst
The other day, I stumbled across this post , describing how .NET developers are alledgedly misuing DateTime.Now . The gist of it is that we are using DateTime.Now in cases where DateTime.UtcNow would be more appropriate, for instance when measuring runtime performance. The difference between Now and UtcNow is that the former gives you local time, whereas the latter gives you "universal" or location-independent time. It turns out that Now is one or two orders of magnitude slower than UtcNow...
26.05.2011

Optimus Prime

av: Einar Høst
Iterators, man. They're so much fun. I've messed about with IEnumerable<T> a little bit before, even in the short history of this blog, but I don't feel like I can say I've done so in anger. Sort of the litmus test to see if you've grokked lazy evaluation is to implement an infinite sequence of some sort, wouldn't you agree? Until you do that it's all talk and no walk. So I thought I'd rectify that, to be a certified IEnumerable<T> -grokker. You in? We'll start gently. The simplest example of...
05.10.2011

Launch as admin

av: Einar Høst
Ignorance can hurt you for weeks and months, by forcing you to go through inefficient, mundane and - here's the main thing - unnecessary hoops to accomplish every-day, common-place things. Like launching Visual Studio with administrative rights. First off, I use Launchy to do pretty much anything (and you should too). Certainly I use it to launch Visual Studio. Every day. Thanks to Launchy, I don't have to take my spidery fingers off my lovely keyboard at all. No feeding that gluttenous RSI...
27.09.2011

Telepathic computer control with Launchy

av: Einar Høst
You'd think that, as a professional computer user, I'd have all kinds of fancy tricks up my sleeve to help me command the dang thing. But I don't. Maybe I should, but I don't. In fact, using Launchy is my one claim to power-user-dom. Launchy is a magical software artifact that allows you to translate thoughts to computer commands. The thoughts travel at light speed through my hands directly into the CPU. No kidding.A more mundane description would be that it allows you to launch applications...
25.09.2011

Dry data

av: Einar Høst
I’m a big proponent of the NoORM movement. Haven’t heard of it? That’s because it doesn’t exist. But it sort of does, under a different name. So-called “micro-ORMs” like Simple.Data , dapper and massive all belong to this category. That’s three really smart guys (unwittingly) supporting the same cause as I. Not bad. I’d say that’s the genesis of a movement right there. Unsurprisingly, NoORM means “not only ORM”. The implication is that there are scenarios where full-blown object-relational...
20.06.2011

Pix-It War!

av: Einar Høst
So-called custom HTTP handlers can be incredibly useful. It's almost laughably easy to write your own handler, and it enables some scenarios that might be difficult, cumbersome or inelegant to support otherwise. It's definitely something you'll want in your repertoire if you're an ASP.NET programmer. In essence, what a custom HTTP handler gives you is the ability to respond to an HTTP request by creating arbitrary content on the fly and have it pushed out to the client making the request. Thi...
04.11.2011

Bix-It: Pix-It in the Browser

av: Einar Høst
The previous blog post introduced PixItHandler , a custom HTTP handler for ASP.NET. The handler responds to HTTP POST requests containing a JSON description of a 8-bit style image with an actual PNG image. Provided you know the expected JSON format, it's pretty easy to use a tool like Fiddler (or cURL for that matter) to generate renderings of your favorite retro game characters. However, while you might (and should) find those tools on the machine of a web developer, they have somewhat limit...
06.11.2011

Pix-It Curling

av: Einar Høst
I mentioned cURL in passing in the last blog post . In case you haven't heard of it: it is a super-useful tool you can use to issue HTTP requests from the command line (and a whole slew of other stuff). Just thought I'd jot down a quick note on how to use it to play around with the pix-it HTTP handler. It's a breeze, really. So effortless! If you've installed cURL and vim (and added them to your PATH), you can do the whole command-line ninja thing and never take your fingers off the keyboard....
07.11.2011