Thursday, February 16, 2012

Primo evento Perugia GTUG


Venerdì 9 Marzo 2012 alle ore 18:00 si terrà a Bastia Umbra il primo evento organizzato dal Perugia GTUG (Google Technology User Group), durante il quale verrà presentato ufficialmente lo user group e saranno illustrate e discusse le prossime iniziative.
Durante l’evento ci sarà anche spazio per due brevi interventi tecnici introduttivi su Android e Google App Engine.
 

Mappa
http://bit.ly/bocciofila

Programma
Ore 18.00 - Presentazione del GTUG Perugia
Ore 18.20 - Google App Engine, una piattaforma su cloud (Massimiliano Pippi)
Ore 18.40 - Scopriamo Android (Luca Morettoni)
Ore 19.00 - Free drink & Open discussion

Modulo d'iscrizione:
Ci vediamo venerdì 9 =)

Wednesday, February 15, 2012

NHibermate 3.2: mapping by code complete guide

Adam Bar wrote a list of posts on his blog about the NHibernate mapping by code, also known as loquacious mapping.
So far, this is the only complete guide on the web about the new xml-less mapping.
A great job has been done with many comparisons between xml and FluentNHibernate mapping.

The summary is here:
http://notherdev.blogspot.com/2012/02/nhibernates-mapping-by-code-summary.html

Have a nice read :)

Monday, January 23, 2012

NHibernate proxy of incorrect type exception: "PropertyAccessException Invalid Cast (check your mapping for property type mismatches)"

Are you facing this exception?

NHibernate.PropertyAccessException : Invalid Cast (check your mapping for property type mismatches); setter of MyEntity ----> System.InvalidCastException : Unable to cast object of type 'MyOtherEntityProxy' to type 'AnotherEntity'.

Looking on google, I saw that other programmers have this type of problem.

This article can help you if you are using Ninject and uNhAddIns.NinjectAdapters for entity injection in NHibernate.

If you are using the "NinjectAdapter" assembly, you need to bind the IProxyFactory interface to a class, for example to the default provided with NHibernate, DefaultProxyFactory, if is good for you.
My problem was the definition of this bind in a Ninject kernel module, copied from somewhere on the net or from a book, I can't remember.
   Bind<NHibernate.Proxy.IProxyFactory>()
      .To<NHibernate.Proxy.DefaultProxyFactory>()
      .InSingletonScope(); // <= here the problem


Declaring this bind in singleton scope is not good because the instance will be shared between NHibernate entities metadata classes: so you will get always the first ProxyFactory instance created and you will receive an error like the one above at runtime when you need a proxy classes of a type different from the first one created.

Was really a pain and take a lot of time to find out the problem as you can image, and the solution is to declare the bind InTransientScope(), of course.

   Bind<NHibernate.Proxy.IProxyFactory>()
      .To<NHibernate.Proxy.DefaultProxyFactory>()
      .InTransientScope(); // <= problem fixed

Here the documentation about the object scopes in Ninject.

Doing a similar mistake, you could have the same problem with another dependency injection library like Sprint.Net or CastleWindsor, of course.

Hope it helps :)

Wednesday, January 18, 2012

Antivirus real time gratis per Windows: ClamWin + ClamSentinel

L'utilizzo dell'antivirus in questi ultimi anni è diventato uno strumento obbligatorio, sopratutto in ambiente Windows.
Perché spendere dei soldi per acquistare un antivirus quando sulla rete possiamo trovare dei software interamente gratis e di buona qualità!? La soluzione che andiamo a descrivere è l'unione di due programmi open source: ClamWin e ClamSentinel. Questi due programmi sono progetti di software libero ed entrambi fanno capo al progetto Clam AntiVirus.

- ClamWin fornisce un'interfaccia grafica per il motore antivirus ClamAV ma non supporta la scansione del sistema in tempo reale. Per colmare questa lacuna andremo ad utilizzare un altro software.
Disponibile al seguente link: http://sourceforge.net/projects/clamwin/

- ClamSentinel è un programma che monitora costantemente ogni modifica ai propri file e ne controlla il contenuto alla ricerca di virus usando ClamWin.
Disponibile al seguente link: http://sourceforge.net/projects/clamsentinel/

Qualcuno potrebbe obiettare che esistano già antivirus commerciali in versione gratuita.
Sì è vero, ma Clam Antivirus non è una seconda scelta né tanto meno una versione ridotta: è il miglior software disponibile in maniera totalmente libera, gratuita e senza pubblicità.

Wednesday, January 11, 2012

Efficient and scalable select of a random record in PostgreSQL


This is a code snippet for the select of a random record in a efficient and scalable way when using PostgreSQL:

   SELECT * FROM table_name
   LIMIT 1
   OFFSET (SELECT Random() * Count(*) FROM table_name);

According with the manual, the Random() function return a random value in the range 0.0 <= x < 1.0 .
So the effect of the expression (Random() * Count(*)) is to return a value between 0 and Count(*): we can use this value as offset to skip a random values of records.

This work in PostgreSQL 8.* and 9.*.

This solution required two queries, but if you have many records, is more efficient than this other one :

   SELECT * FROM table_name
   ORDER BY Random()
   LIMIT 1;

In this solution the Random() function is called for each row, so with many records it could be inefficient in term of execution time and memory usage.