WILT: Query by Criteria with clause on component property with NHibernate


It took me a little bit to figure this one out but it makes sense and it’s pretty simple once we know:

Let’s say I have a class MyFirstClass with a component of type MySecondClass and I want to write a query to retrieve all MyFirstClass objects where the ID of MySecondClass is 1.
My first attempt went like this:

Session.CreateCriteria(typeof(MyFirstClass)
   .Add(NHibernate.Criterion.Expression
     .Eq("MySecondClass.ID", "1"));
That didn’t work because NHibernate doesn’t understand what I meant with MySecondClass.ID.
The solution is simply to use a sub criteria like so:
Session.CreateCriteria(typeof(MyFirstClass)
  .CreateCriteria("MySecondClass")
    .Add(NHibernate.Criterion.Expression
      .Eq("ID", "1"));

It is important to notice that the sub criteria is created using the Property name

WILT: NullPointerException when calling startActivity


That’s such a beginner error but I have no shame posting about it because I AM a beginner with Android  🙂

When trying to start a ListActivity from my main Activity, I kept on getting a NullPointerException. The code to launch my activity looked like this:

myActivityInstance.startActivity(new Intent(Intent.ACTION_VIEW));

Well, the problem was that I forgot to set the content view at the beginning of my launched activity. All I had to do was add this line at the beginning of my onCreate event handler:

setContentView(R.layout.tasks_list);

WILT: XML encode a string in .net


Always wondered why I couldn’t find a method that would XML encode a string, effectively escaping the 5 illegal characters for XML. There is such a method but its location in the API is not intuitive at all. It’s in the System.Security namespace:

   1: public static string Escape(

   2:     string str

   3: )

Its usage is:

   1: tagText = System.Security.SecurityElement.Escape(tagText);

This will escape the 5 characters <, >, &, “ and ‘

MSDN: http://msdn.microsoft.com/en-us/library/system.security.securityelement.escape.aspx

WILT: NHibernate – An association from the table MyTable refers to an unmapped class: MyClass


If you are sure that you already created the mapping for MyClass, the first thing to check is that you change the BuildAction Property for the mapping file MyClass.hbm.xml to Embedded Resource.

This can be done by right-clicking on the mapping file in Solution Explorer and then clicking on Properties. The BuildAction is the first property in the list.

WILT: Comparing 2 Lists in C#


I wanted to compare if 2 lists (List<T>) were equal meaning that they contained items that were equal and not that the 2 lists were actually the same object.

I made the mistake to use the Equals() method and of course got a false returned to me when I was expecting the 2 lists to be equal. The method to use is SequenceEquals() which is actually available for any class implementing IEnumerable.

SequenceEquals on MSDN

WILT: Inherit and Implement in C#


WILT: What I Learned Today

This is a new category of posts I decided to start to record by daily learnings. I am sure that a lot of those posts are going to be embarrassing because they will expose basic things I should have known for a long time but, oh well…
One of the things I enjoy the most with my job is that I learn everyday. Some days I learn about small details, other days I learn more complex techniques or theories.

Anyway, today’s post is about creating a class that both inherit from another class and implements an interface.

This is very straight forward and a frequent occurence but the little “trick” here is to list the inherited class(es) first.

   1: public class MyClassA

   2: {

   3:     ...

   4: }

   5:  

   6: public interface IMyInterface

   7: {

   8:     ...

   9: }

  10:  

  11: public class MyClassB : MyClassA, IMyInterface

  12: {

  13:     ...

  14: }