Map SQL server 2008 Geography Type with Fluent-NHibernate

Recently I started a new project in a company involving Geo-Localization. I needed to integrate in my project GPS coordinate and to calculate the distance between them. In this project I decided to use SQL Server 2008 and Fluent-Nhibernate. Sql server 2008 has a great set of data types related to Geo-Localization known as Geography Data Type. With this fantaboulus data type you can represent point, line even polygon ad make a wide variety of manipulation with them, included calculate the distance between two point. All I needed was to store and retrieve my GPS coordinate into and from the db. I decided to use the SQL Server POINT structure. The structure of a data table was like this:

CREATE TABLE [Content](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Lat] [varchar](50) NOT NULL,
    [Lon] [varchar](50) NOT  NULL,   
    [Location] [geography] NULL,
    [Messgage] [varchar](400) NULL
)
go

In this project I decided to use NHibernate as O/RM mapper and in particular I decieded to use Fluent NHibernate. For every field of the above table , except for Location,   was quite easy do a mapping with Fluent because they're all basic type . But in order to map the Location field I needed to create an ad-hoc type class which I called SqlGeographyUserType. This class implemented the IUserType interface that allows you to create your own custom type.

class SqlGeographyUserType : IUserType

    {

        public bool Equals(object x, object y)

        {

            if (ReferenceEquals(x, y))

                return true;

            if (x == null || y == null)

                return false;

            return x.Equals(y);

        }

        public int GetHashCode(object x)

        {

            return x.GetHashCode();

        }

        public object NullSafeGet(IDataReader rs, string[] names, object owner)

        {

            object prop1 = NHibernateUtil.String.NullSafeGet(rs, names[0]);

            if (prop1 == null)

                return null;

            SqlGeography geo = SqlGeography.Parse(new SqlString(prop1.ToString()));

 

            return geo;

        }

        public void NullSafeSet(IDbCommand cmd, object value, int index)

        {

            if (value == null)

                ((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;

            else

                ((IDataParameter)cmd.Parameters[index]).Value = ((SqlGeography)value).STAsText().Value;

        }

        public object DeepCopy(object value)

        {

            if (value == null)

                return null;

            var sourceTarget = (SqlGeography)value;

            SqlGeography targetGeography = SqlGeography.Point(sourceTarget.Lat.Value, sourceTarget.Long.Value,

                                                              sourceTarget.STSrid.Value);

            return targetGeography;

        }

        public object Replace(object original, object target, object owner)

        {

            return DeepCopy(original);

        }

        public object Assemble(object cached, object owner)

        {

            return DeepCopy(cached);

        }

        public object Disassemble(object value)

        {

            return DeepCopy(value);

        }

        public SqlType[] SqlTypes

        {

            get { return new[] { NHibernateUtil.String.SqlType }; }

        }

        public Type ReturnedType

        {

            get { return typeof(SqlGeography); }

        }

        public bool IsMutable

        {

            get { return true; }

        }

    }

In this way I have been able to map SQL Server Geography data type with Fluent Nhibernate:

    public class ContentMap : BaseMap<Content>

    {

        public ContentMap()

        {

            Map(m => m.Location).CustomType(typeof (SqlGeographyUserType));

            Map(m => m.Lat);

            Map(m => m.Lon);

            Map(m => m.Message);

        }

    }

Next time I would like to show how to get the distance between two Geography point from SQL Server & Fluent-NHibernate . But who knows when will be? ;-)

posted @ Friday, March 26, 2010 3:43 AM

Print

Comments on this entry:

# re: Map SQL server 2008 Geography Type with Fluent-NHibernate

Left by Personalized Name Keychains at 3/6/2012 7:23 PM
Gravatar
long Map SQL server 2008 post

# re: Map SQL server 2008 Geography Type with Fluent-NHibernate

Left by tumblr at 3/6/2012 7:32 PM
Gravatar

# Web Hosting Providers

Left by Web Hosting Providers at 3/8/2012 1:22 AM
Gravatar
I have been searching for some information about it almost three hours. You helped me a lot indeed and reading this your article I have found many new and useful information about this subject.

# Magento theme Download

Left by Magento theme Download at 3/20/2012 7:43 PM
Gravatar
First of all let me tell you, you have got a great blog .I am interested in looking for more of such topics and would like to have further information. Hope to see the next blog soon.

# re: Map SQL server 2008 Geography Type with Fluent-NHibernate

Left by Guillerme at 4/16/2012 11:16 PM
Gravatar
Get the most benefits from free ddos protection that you can easily get in few minutes online. Don't need to pay until your website is fully 100% protected from ddos.

# Capara Veste 4 Avec

Left by Fomhi Blog at 10/5/2016 12:07 PM
Gravatar
[...] about it almost three hours. You helped me a lot indeed and reading this your ar [...]

# nice

Left by marki at 2/10/2018 8:26 AM
Gravatar
lul

# re: Map SQL server 2008 Geography Type with Fluent-NHibernate

Left by jorge at 4/27/2018 5:40 PM
Gravatar
Easy website backups for WordPress, cPanel or FTP websites. Easy to use and automatic cloud storage included. We know how to backup WordPress websites or any type of online website for you and the bot does simple website restores for you too. Every account uses secure online storage from the Amazon AWS cloud, to make sure your files are kept safe and protected.
https://www.websitebackupbot.com/


# re: Map SQL server 2008 Geography Type with Fluent-NHibernate

Left by jorge at 4/27/2018 5:54 PM
Gravatar

# Sony 4K LED TV

Left by Aliyah at 6/1/2018 5:11 AM
Gravatar
XBR X900F TVs are designed to handle various HDR formats including HLG, Dolby Vision, and HDR10. The sets similarly deliver high quality images which aren’t affected by light.Sony 4K LED TV
Comments have been closed on this topic.
«November»
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567