How to do a one-to-one mapping in Fluent nHibernate

In most circumstances, you will use a many-to-one mapping and that’s easily created with fluent nHibernate using the “References” relationship. However if you need to perform a 1 to 1 mapping, then things get tricky. Say for example, you have a user record and wanted to have the user preferences in another table. Each user will have a record in the preferences table so it’s a direct one to one mapping. You will then have to use the HasOne relationship in fluent nhibernate.

public class User
{
    public virtual int Id { get; set; }
    public virtual string Username { get; set; }
    public virtual UserPreference Preference { get; set; }
}

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.Id);
        Map(x => x.Username);

        HasOne(x => x.Preference);
    }
}

public class UserPreference
{
    public virtual int UserId { get; set; }
    public virtual bool Newsletter { get; set; }
}

public class UserPreferenceMap : ClassMap<UserPreference>
{
    public UserPreferenceMap()
    {
        Id(x => x.UserId).GeneratedBy.Assigned();
        Map(x => x.Newsletter);
    }
}

So we have a User table with Id set as the primary key and we have a second table UserPreference with UserId as the primary key. The Id on the user table is autogenerated by the database but when a user entity has already been created, we pass this object to the UserPreference entity so that it can get the UserId. That’s why we need to specify in the mapping class for UserPreferenceMap that the Id is Assigned.

Google changing page titles in SERPs

While searching where one website was ranked for in Google for a search term, I noticed something really strange in the SERPs. Google has changed the title of the webpage by appending the site’s name to the end of the title. To give you an idea of what’s happening, consider the following:

Webpage title : Basic SEO Skills
Website name : My Website

You would think that “Basic seo skills” would be listed as the title in google search result pages but no, the title was displayed as “Basic SEO Skills – My Website”, so the suffix “My website” was appended to the real title. I checked the said webpage and the website name was not in the title which means Google was deliberately adding this to the title. Funny thing is that for a different search term (variation of the main keywords), the right title is shown.

So I did a little research and it turns out that Google reserves the right to alter your page title tag to show a more relevant result to the searcher (this depends on what search terms are being used). I knew they showed a more relevant snippet (text from your webpage where the search terms are found) instead of the description tag but this title thing was new to me.

Where does Google pull the additional words to append to your title tag?
Well it turns out that the suffix can come from anchor text pointing to your page, your website name or from text in your content.

When does Google alter your page title?
Seems like whenever your webpage has been deemed a good result to display for a particular search term but your title does not contain an essential keyword to make the search result more appealing, Google will spice up your title by appending the search term from, say, the inbound link’s anchor text.

If your page title is short or not descriptive as well (eg Untitled or no title at all), Google will most likely change it to better reflect your content.

Enum conversion in C#

Just for reference, here are some examples on how enums can be converted in C#:

public class MyAnimalClass
{
    public enum Animal
    {
        Cat = 1,
        Dog = 2,
        Bird = 3
    }

    // enum to string
    public void Hey()
    {
        Animal animal = Animal.Bird;
        string animalText = animal.ToString(); // Bird

        HttpContext.Current.Response.Write("<br />enum to string : " + animalText);
    }

    // string to enum
    public void Hey2()
    {
        string animalText = "cat";
        Animal animal;
        // Enum.TryParse will return false if the string value cannot be converted to the enum type
        if (!Enum.TryParse<Animal>(animalText, out animal))
            HttpContext.Current.Response.Write("<br />string to enum is null for : " + animalText);
        else
            HttpContext.Current.Response.Write("<br />string to enum: " + animal.ToString()); // Animal.Cat

        Animal animal2 = (Animal)Enum.Parse(typeof(Animal), animalText, true); // Animal.Cat (case insensitive comparison)

        HttpContext.Current.Response.Write("<br />string to enum insensitive: " + animal2.ToString());
    }

    // int to enum
    public void Hey3()
    {
        int animalInt = 2;
        Animal animal = (Animal)animalInt;

        HttpContext.Current.Response.Write("<br />int to enum : " + animal.ToString());
    }

    // enum to int
    public void Hey4()
    {
        Animal animal = Animal.Bird;
        int animalInt = (int)animal;

        HttpContext.Current.Response.Write("<br />enum to int : " + animalInt.ToString());
    }
}

Using enums when coding makes life easier as you can easily tell what sort of object you’re dealing with rather than having to guess what the numbers (1,2,3, etc) mean really.

Useful MySql statements

Grant privileges to a certain user
GRANT ALL PRIVILEGES ON *.* TO someone@localhost IDENTIFIED BY ‘theirpassword’ WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO someone@”%” IDENTIFIED BY ‘theirpassword’ WITH GRANT OPTION;

The second statement is for granting privileges to the account when not on the local server (not localhost). Both statements will give all privileges on all tables and databases.

Grant only trigger privilege
GRANT Trigger ON *.* TO someone@localhost IDENTIFIED BY ‘theirpassword’ WITH GRANT OPTION;

Show all triggers
SHOW TRIGGERS;

Create a trigger
CREATE TRIGGER MyTrigger BEFORE INSERT ON employee
FOR EACH ROW
BEGIN
IF NEW.JobTitle = ‘Manager’ THEN
SET NEW.Wage = 30000;
END IF;
END;

The NEW keyword above references the row which is going to be inserted. Therefore just before the insert is done, if the JobTitle was ‘Manager’, the Wage column would be updated to 30000 before being committed to the database.