Monthly Archives: January 2010

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.

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 …).

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.

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”.

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).