Wednesday, January 26, 2011

Join on tables using Fluent NHibernate


I started using Fluent NHibernate about a year ago as I was experimenting with ORM's in general when a good friend introduced me to the framework.
First of all, I felt that learning Fluent NHibernate in the first place wasn't that easy. I eventually found everything I needed on the internet, but for some reason, it wasn't as easy for me as other frameworks i've dealt with in the past.

Enough said, there were some things that I couldn't find how to do on the internet, like how to join two different tables using Fluent NHibernate mappings. I even spotted some people thirsty for answers...
Check out this question on stackoverflow.com that I found as I was looking for the answer myself, and eventually ended up coming back to answer the question myself! :)

I'll show this using a simple example.

I have two entities :
public class FormStructure
{
    public virtual Int32 FormId { get; private set; }
    public virtual Int32 FormType { get; set; }
    public virtual FormField FieldId { get; set; }
}

public class FormField
{
    public virtual int FieldId { get; private set; }
    public virtual String FieldName { get; set; }
    public virtual int? FieldType { get; set; }
    public virtual int? DisplayOrder { get; set; }
}

Now, I want to be able to create a query on the FormStructure entity, and have the results ordered by the matching DisplayOrder field in the FormField entity. I also want the DisplayOrder field to be available to me as a property of the FormStructure entity.

In order to accomplish this, I will need to create a Join between these two tables.
The first step is to add the DisplayOrder field as a property in the FormStructure entity :
public virtual int? DisplayOrder { get; set; }

Then, all I needed to do was use the Join method on my mapping class like this :
public class FormStructureMap : ClassMap<formstructure>
{
    public FormStructureMap()
    {
        Table("FormStructure");

        Id(x => x.Id);
        Map(x => x.FormType);
        References(x => x.Schedule).Column("ScheduleId");
        References(x => x.Field).Column("FieldId");
        Map(x => x.IsMandatory).Nullable();

        Join("FormFields", m =>
        {
            m.Fetch.Join();
            m.KeyColumn("FieldId");
            m.Map(t => t.DisplayOrder).Nullable();
        });
    }
}

Now the query will automatically be created as a join behind the scenes, and I will have the DisplayOrder column available for me in every row.

This might remove some of the rows from the result, if they have NULL values in the added column from the join. To avoid this, add 'm.Optional()' inside the Join method.

In order to query the table with the join like i defined, all i need to do is something like this :
return session.CreateCriteria<formstructure>()
              .Add(Expression.Eq("FieldName", fieldName))
              .AddOrder(Order.Asc("DisplayOrder"))
              .List<formstructure>();

That's all for now.
I think i'll be blogging more about Fluent NHinernate in future posts as well, as I have started using it more heavily lately...

Monday, January 17, 2011

Generating email address images to avoid spam on your site


Now a days you can't publish an email address anywhere without receiving a bunch of spam immediately after. Those spam web crawlers just search everysite looking for everything and anything that resembles an email address.
I very often see people use different variations of writing their email like "myname at mydomain.com" or "myname at mydomain dot com".
I don't think this covers it anymore...
The best way, in my opinion, is to create an image of the email address instead of writing it. (That's what facebook does as well..)

I started working on a user based site, and the users' emails will be displayed throughout the site. So I created a class that will dynamically generate email address images for me so my precious users won't be so vulnerable to all the spam out there.

Here it is :


I will probably make some changes before using this on my site :
1. I don't think it's smart to give the file the name of the actual email address. I'll probably generate some unique ID but built from the users name.
2. I haven't decided yet if I want the image to be a link to contact the user. If so, then I must create a unique id for each user, and have the link lead to that page with the unique id so i can identify the user being contacted.
3. I'll probably add a simple caching mechanism to this, hence create a folder to store all the images, and then check if it exists before creating a new one each time.

Enjoy :)