Archives for January, 2010

31
Jan

Complex types and stored procedures

I like the idea of complex type. Sadly the designer didn’t supported complex types in EFv1/VS2008, although you could use it manually editing the model (it’s just a XML file). The designer in EFv2/VS2010 supports complex types (and you can use it also with EFv1, see this) hence it’s time to start really using these.

Unfortunately nothing is perfect. As far as you don’t wanna map some stored procedure result to collection of entities, then the designer will tell you, that it’s not supported. :( And Jeff Derstadt pointed in forum that this limitation will not be solved in RTM.

I’m using EFv4 in my current project and thanks to ability to map functions directly to LINQ functions via EdmFunction attribute I was able to to a lot of work via functions and queries created by EF itself. But I always feel a little bug in my head saying “What if you’ll need it later?”. Sure I can redo all back into direct properties, but if it’s close to the end of the project it’s pretty expensive to do it.

What a pity, I see complex types as a good concept, but I’m too careful to close myself the path with SPs.

23
Jan

Connecting from iPhone to Firebird

During the Friday I realized, that I did a long time nothing with my iPhone & MonoTouch development environment and I should try something more challenging. As I’m still fighting with some good idea for real world test application and my UIs are looking weird, I decided to turn my attention not to iPhone app directly, but to MonoTouch capabilities.

As a true geek I decided to try to connect to Firebird from iPhone. Although, thinking about it, I’m trying to connect to Firebird (or make it work with) with various technologies (Astoria offline, Silverlight, etc.). Because .NET provider for Firebird is pure C# and we have Mono compatible build, I deduced that it should work with iPhone too.

Sure, it’s a nice challenge to whole MonoTouch stack, because the .NET provider is more about the code than about the application itself. And we’re using there a lot of different things that can go wrong or may not be available or compilable to native code. And I have to say, the guys behind MonoTouch did a great work (I still can’t believe it).

With couple of minor tweaks I was able to create application that connects from iPhone (simulator) through internet to Firebird server. Pure C#, no hacking or major problems.


Application connecting to Firebird server and showing the server version.

First I’m impressed how mature the MonoTouch is. Second I’m still trying think thru all the possibilities you have with this. With some work on UI you can deliver the same database oriented application to Windows Mobile and iPhone using the same business layer (sure some webservice approach would be better, but …).

23
Jan

Interface Builder crash

I think the whole iPhone SDK knows I’m from the other side of the barricade. :D What else would explain the error I faced today after less than a minute when working in Interface Builder.

On the other hand I was brave enough to send the error report (as I was able to reproduce it quite easily), so maybe it’ll be fixed in next version. Good turn? Anyway sooner or later I’ll convince the machine that I’m not a bad guy.

21
Jan

Be careful with “not” conditions and nulls in LINQ when querying databases

Today I seen a code with good chance of hard to find mysterious bugs. Let’s start with database table structure we’re going to use for demonstration.

create table NullTest(id int primary key, b bit);

insert into NullTest values (0, 1);
insert into NullTest values (1, 0);
insert into NullTest values (2, null);

If you now try to query this table via LINQ (i.e. LINQ to Entities) you may get surprising results.

foreach (var item in ent.NullTests.Where(x => x.b != true))
{
	Console.WriteLine(string.Format("ID: {0} t B: {1}", item.id, item.b));
}
Console.WriteLine("===");
foreach (var item in ent.NullTests.AsEnumerable().Where(x => x.b != true))
{
	Console.WriteLine(string.Format("ID: {0} t B: {1}", item.id, item.b));
}

If you run this code, you’ll get different results.

ID: 1    B: False
===
ID: 1    B: False
ID: 2    B:

What happened? Is the database engine doing something wrong? Or is there a bug in LINQ? Neither of those. In fact both results are correct. The second one (evaluated in .NET on client) is obvious why it’s as is. But what happened in processing of first one (evaluated on database side)? The devil is in NULL logic. Every operation with NULL results in NULL or false if it’s a boolean operation. And this exactly explains the inconsistent result. In .NET null != true is true but in databases it’s false (because of the NULL rules described above).

Thus if you’re writing LINQ query for database, although the impedance mismatch should be hidden from you when using LINQ, you need to take into account different NULL handling in database engines and in .NET (or any common programming language).

Remember the DBNull.Value? That was explicit solution for this “problem”.

21
Jan

Knetlik – konference o .NET

13. února 2010 (sobota) se koná zajímavá konference okolo .NETích témat s podtitulem “Bringing Good Software Architecture”. Registrace je dostupná na známém místě. Program pak naleznete na knetlik.cz.

A proč je to konference zajímavá. Kromě přirozeně kvalitních témat ;) je to jejich délka. Pouze neobvyklých 10 minut (+5 minut pro otázky). Předpoklad je, že posluchači udrží takto lépe pozornost a vstřebají více informací. A ty budou ještě vlastně hodně kondenzované, aby člověk něco smysluplného řekl a přitom se vešel do časového limitu. Těším se, až uvidím, jak to funguje (nebo taky ne).

21
Jan

Školení Firebird (3.)

Společně s Databázovým světem tu máme další Firebird školení. Více informací, včetně možnosti přihlášení na: http://www.dbsvet.cz/view.php?cisloclanku=2010012101.

14
Jan

Extending IQueryable with superset method and StackOverflowException

Yesterday I was creating extension method for Skip for IQueryable. I needed to accept the nullable integer and if null just skip any processing. Something like this:

static IQueryable<T> Skip<T>(this IQueryable<T> resource, int? count)
{
	return (count.HasValue ? resource.Skip(count.Value) : resource);
}

I thought it will pick the int version from framework, because I’m calling it with int and not int?. And I was wrong, what my colleague uncovered moments later. How to solve it? I was not happy with method rename and I felt there’s a way.

Actually the solution is pretty easy. The original Skip is just extension method, right? So it’s in static class as static method. So I can call it with class definition as non-extension method and this this there will be no ambiguity. Voilà.

static IQueryable<T> Skip<T>(this IQueryable<T> resource, int? count)
{
	return (count.HasValue ? Queryable.Skip(resource, count.Value) : resource);
}
14
Jan

Synology disk power cable

I was recently upgrading my Synology NAS adding new disks and rebuilding RAID volume. And because I somewhere lost (I’m storing all these pieces, but once a time I feel I have too much of small cables, screws etc. and I do the cleanup :) ) the precise length SATA power and data cables I was screwed. The data cable is easy to buy, although the shortest one I was able to find in reasonably long time was 50cm length. Never mind, it will fit there, with or without using force.

The problem was the power cable. After some searching I realized that the connector on motherboard is actually same as was on floppy drives (did anybody seen floppy drive in last 5 years?). With this information it was little bit easier to find the cables and/or adapters. At the end I bought SATA power < -> molex, molex (M) < -> molex (F) + floppy adapter. It works nicely, and also fits the box.

Just wondering what I’ll do, when floppies will be absolutely dead. Maybe I should buy couple of these cables in advance. :D

Hope this helps somebody when looking for this cable for Synology NAS (maybe the new products have also new more up-to-date connectors).

10
Jan

First touch at MonoTouch

Looks like I joined the prominent group of iPhone developers. Friend of mine, Meap, also experienced iPhone developer should be scared. This is what I did in almost no time.

OK, this application sucks, it’s doing nothing and looks … well looks how it looks. :D

But the interesting think is, that the application is “written” completely in C# using Mono and MonoTouch. On iPhone you can’t have JIT (because there’s no way to switch page from data to executable) at least now from my little knowledge. Thus you for IL you have to do AOT and that’s nice challenge. In fact that was the reason why I was trying the iPhone development with the MonoTouch.

The environment is completely different (I mean the “iPhone style of creating apps”) from i.e. Windows Mobile. I’m really looking forward to try new stuff not only with the iPhone environment but also with MonoTouch as it’s an interesting piece of code. God, tomorrow, there will be a punishment from friends in office as I’ve sworn to be absolutely not interested in iPhone (and still I am from users perspective), but the development turned this into a temptation (because of developer perspective).

10
Jan

That’s an installation…

Today I was installing iPhone SDK (in fact that’s first time I was installing something such big on Mac) and at the end of the installation the installer started optimizing system (yes, system) for installed software. :D Funny. Hopefully it was not creating new kernel. :) Just wondering, what would happen if this crashes…

OK, before all Mac-friendly people will kill me, I’ll make a simple statement. I’m not using Mac regularly and that’s why I sometimes found something funny and why I’m making fun of it (and because I’m Windows user ;) ).

9
Jan

Directory.GetFiles moral

Oh yes. I have here another moral. And it’s not good for me, because it’s (again) described in documentation (but who reads it when there’s nice IntelliSense?). In my program ID3 renamer I have couple of custom IO operations. One of fundamental methods is for enumerating directory(ies) for all MP3 files. It’s based on Directory.GetFiles and few functions inside ID3 renamer need exact order of files. One of these functions is FreeDB search. When I was renaming files on my network share, it wasn’t finding anything. But when moving the directory to local drive everything was fine.

After some debugging I found, that the ordering of files from network share is different than from local drive. If you look to documentation you see:

The order of the returned file names is not guaranteed; use the Sort() method if a specific sort order is required.

Ahh, there’s your problem. :) Sadly enough, the ordering from local drive looks stable and it’s based on filename as expected. Thus in a lot of cases you’ll not smell that something may go wrong. But on non local drives (for me my NAS) you’ll hit this immediately.

Never mind, could be worse (my first shot was some bug in my “MP3File” class and that will be really bad, as it could break users’ files). This can be fixed easily.

2
Jan

Čechy v Kindlu

Když jsem předevčírem psal o naustálém rozrůstání nabídky novin pro Kindle a přáním, aby se vydavatelství u nás také zapojila do “kindle kolotoče”, ani jsem netušil co dnes objevím.

The Prague Post je k dispozici pro Kindle. Stále jsou to sice anglicky psané noviny. Ne masové jako třeba lidovky, ale každý malý krok se počítá.

2
Jan

Switching from one CPU to more CPUs on XP

As I recently switched from Virtual PC to VMWare Workstation I wanted to upgrade my VMs to be able to use all cores I have and I granted. Machines I’m using most are Windows XPs and Windows 7. As the Windows 7 migration failed and I’m still about to install fresh W7 RTM, the challenge was XP.

Couple of hints I found on internet and as replies to my question in mailing list but nothing was working or it was screwing the system. But then I found a great document, with the manual, forced, way of switching. Although the document recommends doing the surgery in safe mode, I did it in live system (vivat snapshots) once, with just raw copy (+overwrite) and it worked.

copy C:WindowsServicePackFilesi386halmacpi.dll C:Windowssystem32HAL.DLL
copy C:WindowsServicePackFilesi386ntkrnlmp.exe C:Windowssystem32ntoskrnl.exe
copy C:WindowsServicePackFilesi386ntkrpamp.exe C:Windowssystem32ntkrnlpa.exe

Anyway, on real important system I would recommend safe mode too. After reboot the system detected new hardware, installed it and after another reboot I was ready to make the cores screaming.