Wednesday, 8 April 2009

Intercepting xaml DataTemplate of a given DataType

Since a few months I am working on a project where we use integrate WPF with and IOC, Castle Windsor (through the extension of the Composite WPF Contrib).

One of our xaml file would look like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/
xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Presentation="clr-namespace:XXX.Modules.MyForm.Presentation"
xmlns:WinForms="clr-namespace:XXX.Modules.Common.WinForms"
xmlns:Converters="clr-namespace:XXX.Modules.Common.Converters"

<converters:booleanenumconverter key="YYYConverter">
[snipped other converters]
<DataTemplate DataType="{x:Type
Presentation:MyFormPresentationModel}">
[snipped template xaml]
</datatemplate>
</resourcedictionary>
I wrote a couple of interceptors (inheriting from IInterceptor and implementing Intercept), but when I try to use them resolving the presentation model, the xaml DataTemplate resolves to a string (the name of the generated proxy, something as IMyFormPresentationModelProxy1de831fc74a341249b67...), not to the instanced object (which stepping in in debug I can see it is registered and then created and then also proxied correctly, as far as I can understand). This is exactly the same behaviour we get if we try to use the interface (i.e. setting the DataType to "{x:Type Presentation:IMyFormPresentationModel}" we get a string as well instead of the object).

Using the .Net reference source symbols (and/or deblector) I have been trying to step in on the exact bit of code which is setting that string instead of the correct proxied object or even the underlying instanced object, but with no luck so far.

I do think the issue may be with the xaml engine being unable to match the proxied type with the DataType of the DataTemplate, and I found out the .Net framework may perform that using the TypeExtension/MarkupExtension/DataTemplateSelector classes and possibly some implementation of the IXamlTypeResolver interface. From a quick search, I found out nowadays people is using those mostly to provide some support for generics in xaml (see i.e. "How To Get ParserContext" or "Limited generics support in Xaml").

From a purely theoretical point of view, I don't like much the idea to have to modify the xaml files to be able to intercept them, so I am not absolutely sure I have been missing something.

Before trying to spend some time on that (i.e. writing our own TypeExtension), I thought it could be better asking around if there is someone with previous experience or also some insight on this kind of scenario, so I have been posting a similar text to various newsgroups and mailing lists.

Last but not least, although I don't guess this is a Windsor bug (or at last I cannot yet see why it should be :)), we are using Castle project assemblies built from the revision 5380 of the 19th October 2008 in the trunk.

Intercettare/Ereditare il DataType per un DataTemplate in xaml

Da qualche mese lavoro ad un progetto dove usiamo WPF integrato con un IOC container (Castle Windsor, tramite l'estensione fornita dal progetto Composite WPF Contrib).

Abbiamo un file xaml simile a questo pseudo-codice:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/
xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Presentation="clr-namespace:XXX.Modules.MyForm.Presentation"
xmlns:WinForms="clr-namespace:XXX.Modules.Common.WinForms"
xmlns:Converters="clr-namespace:XXX.Modules.Common.Converters"

<converters:booleanenumconverter key="YYYConverter">
[snipped other converters]
<DataTemplate DataType="{x:Type
Presentation:MyFormPresentationModel}">
[snipped template xaml]
</datatemplate>
</resourcedictionary>
Ho scritto un paio di intercettori (in pratica usando un componente chiamato Castle Dynamic Proxy, questi generano delle classi che ereditano i componenti registrati con Windsor e che poi si possono usare per intercettare i metodi prima e dopo la loro esecuzione, ad esempio per scrivere dei log o per fare altre cose), ma quando tento di usarli risolvendo i vari componenti intercettati, i DataTemplate definiti nei file xaml dei componenti non risolvono l'oggetto generato (che e' stato istanziato correttamente da Windsor, come posso ad esempio vedere mentre sono in debug), ma ritornano una stringa, che poi altro non e' che il nome del proxy generato (una cosa tipo IMyFormPresentationModelProxy1de831fc74a341249b67...).

Questo e' un comportamento molto simile a quello che incontro quando tento di usare un'interfaccia come DataType del DataTemplate (ad esempio impostando il DataType ad una cosa come "{x:Type Presentation:IMyFormPresentationModel}"), per cui immagino che il DataType non supporti molto bene l'ereditarieta' (o comunque sia piuttosto conservativo nella risoluzione dei tipi).

Dato che non penso che il problema sia nell'IOC o nell'estensione tramite la quale lo integriamo con WPF, immagino che il problema sia quindi che lo "xaml engine" non sia capace di far corrispondere il tipo descritto nel file xaml con quello generato dall'intercettore.

Ho fatto un po di ricerche, ed ho trovato che c'e' gente che per fare cose probabilmente simili (perlopiu' collegate al fatto che il DataType non supporta pienamente i tipi generici, vedi ad esempio "How To Get ParserContext" oppure "Limited generics support in Xaml") usa classi come TypeExtension o MarkupExtension e possibilmente qualche implementazione dell'interfaccia IXamlTypeResolver. Ho anche trovato qualche accenno al possibile uso di un DataTemplateSelector.

Da un punto di vista puramente teorico, l'idea di dover modificare i file xaml per poter intercettare i DataTemplate non mi attrae particolarmente, quindi mi sto chiedendo se per caso non mi sono perso qualcosa.

Prima di investire altro tempo per risolvere il problema (ad esempio scrivendo una mia TypeExtension), sto cercando di chiedere in vari gruppi o mailing list se c'e' qualcuno che abbia esperienza di questo tipo scenario (o qualcuno simile, magari qualcosa che abbia a che fare con l'ereditarieta' ed i tipi definiti in DataType per DataTemplate).

Anche soltanto qualche suggerimento o qualche referenza sarebbe molto gradita :)

Ho gia' scritto un messaggio molto simile sulla mailing list di Castle, ma immagino che questo non sia un bug di Windsor, ma il risultato di una limitazione (o di una scelta progettuale) del DataType.

Thursday, 2 April 2009

Etimologia di Türkiye

I Turchi chiamano il proprio paese "Türkiye".

Quello che non molti sanno, sia tra i Turchi che tra gli Italiani, e' che
"Türkiye" viene dall'Italiano "Turchia": quando Mustafa Kemal ed i suoi compagni cercarono di fondare uno stato Turco sulle ceneri dello stato Ottomano, lo chiamarono inizialmente "Türkiya", termine ottomano prestito linguistico dall'Italiano (vedi ad esempio [1], [2], [3], [4], [5], [6] o tante altre fonti facilmente reperibili).

La normalizzazione eufonica dell'ultima vocale avvenne poco prima della pubblicazione della prima costituzione qualche anno piu' tardi (dopo o durante il 1923).

Tra l'altro, nel suo significato originale (del XIX secolo), Turchia in Italiano indicava sia l'Anatolia che la fascia dei Balcani a sud e nord della via Egnatia (in pratica Albania, Grecia settentrionale, Macedonia, Rodopi e Tracia), tutti territori in cui fino al principio del XX secolo la maggioranza della popolazione era appunto di etnia Turca e/o religione musulmana.

Il neolatinismo "Turchia" usato in Italia nel XIX secolo, proveniva a sua volta dall'Italiano rinascimentale (probabilmente medievale), che pero' non era usato per definire quella che noi chiamiamo Turchia.

Infatti, veniva dall'Arabo "ad Dawlat at Turkiyyat" (molto affine al Turco odierno "Türkiye devleti", "Lo Stato Turco"), che altri non era che il nome ufficiale del Sultanato Mamelucco in Egitto.

In origine, quindi, la parola "Turchia" indicava l'Egitto!

Nell'Italiano contemporaneo e' rimasta qualche vestigia, come ad esempio il fatto che il mais si chiami "Grano Turco", esattamente come in Turco si chiama "Mısır" (che significa appunto "Egitto") perche' appunto dopo l'introduzione del mais nel bacino mediterraneo, l'Egitto era presto diventato il maggior esportatore.

Wednesday, 18 March 2009

ILI stabile da Ottobre 2008 a Febbraio 2009

Ecco un grafico che mostra il numero di articolo postati su it.lavoro.informatica dalla sua nascita, in serie per mese e per anno (12 mesi precedenti al mese incluso) diviso per il mese e per l'anno in cui si e' avuto un massimo di articoli:



La fonte dei dati e' Google Groups.

Dal grafico si deduce chiaramente che a partire da Ottobre 2008 abbiamo assistito ad una sostanziale stabilizzazione del numero di articoli pubblicati.

Wednesday, 4 February 2009

The "one too many installed .Net framework" time bomb

Today, after installing "Microsoft .NET Framework 3.5 Service Pack 1 and .NET Framework 3.5 Family Update (KB951847) x86", the Microsoft Internet Explorer 7 in my machine was broken.

I wasn't able to start various web applications anymore.

Also, when trying to check the about screen, I was getting an "Not enough storage is available to complete this operation" error.

Thanks to this, I found out why, and I was able to work that around.

It seems that for each installed .Net framework an empty registry value is added on the following key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\5.0\User Agent\Post Platform.
Now, it seems that some Microsoft function is getting all the names of those values, it is concatenating them on a big string, adding 2 characters per name, and if the length of this string exceed 209, IE will stop to work properly.

The work around was as suggested to remove as much names as to shorten that string to a length lesser than 209 characters (counting the 2 already spent for each name!).

In my case, I was having the following names:

.NET CLR 1.1.4322
InfoPath.1
.NET CLR 2.0.50727
FDM
.NET CLR 3.0.04506.30
.NET CLR 3.0.04506.648
.NET CLR 3.5.21022
.NET CLR 3.0.4506.2152
.NET CLR 3.5.30729
MS-RTC LM 8
OfficeLiveConnector.1.3
OfficeLivePatch.0.0

They would result on 226 characters (202 + 2*12), which is 17 characters too much. I removed the last value, and everything started to work again I could have removed also just every character after the first 2 ("Of"), and everything would have worked as well, but if we add anything more, IE breaks.

In general, if we find in that key a single value with a name of 207 characters, as such (obviously unbroken):

12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
123456

With this IE7 will work, add a single character more, and IE7 will break.

Now, until Microsoft doesn't properly fix it, the issue at stake here is if this happen, which names(s) should we remove?
On my workstation, for now I removed OfficeLivePatch.0.0, but it may be needed by some other application, so I think we should exercise good sense, and trying to figure out case by case which version of .Net is less probable to be used on a given workstation/server.

Wednesday, 17 December 2008

Alitalia è morta

Oltre 6 mesi fa mi ero comprato il biglietto per venire in Italia a Natale con Alitalia.
Dato l'andazzo della compagnia negli ultimi mesi, ho pensato che fossero soldi persi, e mi sono ricomprato (3 mesi fa) altri biglietti con altri vettori per gli stessi giorni (Ryanair e Turkish Airlines).
Nel frattempo ho fatto presente la situazione a diverse persone che conoscevo (5) che erano nella mia stessa situazione (tutti voli Alitalia durante il periodo di Natale in giorni/orari differenti).
Beh, oggi Alitalia mi ha scritto che mi hanno cancellato il volo :))
Ho scritto ai 5, 1 aveva seguito il mio esempio, e ha gia' comprato il biglietto con Ryanair da mesi, altri 2 hanno avuto il loro volo cancellato, e stanno aspettando che Alitalia li contatti per fargli sapere come procedere, degli altri 2 ancora non ho notizia.
Sto parlando di biglietti comunque comprati da mesi, con soldi incassati da Alitalia (e a questo punto oramai scialacquati da mesi), e questi geni del trasporto hanno avuto mesi e mesi per considerare che quel biglietto (parte del quale e' stato cancellato innumerevoli volte negli ultimi mesi) probabilmente non avrebbero potuto garantirlo, avrebbero fatto meglio ad avvertirci mesi fa ..
Tra l'altro, nella email nemmeno parlano di rimborso, giusto chiedono che noi gli si telefoni in Italia ..

Thursday, 16 October 2008

What if you really cannot press F8?

You may then edit the boot.ini file.
Microsoft has got a quite terse KB about that, titled Safe-Mode Boot Switches for Windows Boot.ini File.
Good safe-mode computing ..