Saturday, 2 March 2013

Creating a DBGeometry Object using Entity Framework and MVC4 API



What is DBGeometry and how is it different from DBGeography?

The two data types sys.geography and sys.geometry are new types introduced in Sql Server 2008. They form a family of data types labeled “Spatial” types. These types represent information that is used for mapping applications. The difference between the two types is that sys.geometry represents a planar surface while sys.geography represents an elliptical surface, where the ellipsis is the surface of planet Earth. These types are commonly used for performing geospatial calculations. An example of a geospatial calculation is to compute the distance between two cities or calculating the area of a state.

Spatial data in T-Sql



This example updates the branch Address to a new geography value using the STGeomFromText static geography method.  The first argument is the GPS latitude and longitude coordinates while the second is SRID (Spatial Reference ID). The SRID is needed for geography objects because you have to refer to a specific curvature (in this case the SRID 4326 refers to the curvature of the earth).

Note that there is a method with the same name for geometry objects as follows:



In this case the SRID parameter is passed as 0 because this info is not needed for planar information.
You can find out more on how you can correctly instantiate variables of these two types in MS-Sql here.

Spatial data in .NET

When Spatial types were first introduced in Sql Server, there was no native support for them in the .NET types. The workaround for this was to use open source projects like SharpMap. Beginning from .NET 4.5, Microsoft introduced two new types to represent those sql type: DbGeometry and DbGeography.
The thing to notice about these types is that they provide no default constructor and that they can be instantiated only with static methods like FromText() and FromGml(). The following example instantiates a geography object based on two float values (submitted.Long and submitted.Lat) that are float properties of a custom class that holds map info like Longitude and Latitude.  


Since they have no default constructor and cannot be updated once they are instantiated, Spatial types are labeled as immutable. The only way to update the value of an immutable object is to override it with a new value. This is an example that demonstrates the idea of immutable objects:


Note that the string that is used for creating spatial data must follow the standard called  WKT (Well Known Text)

What is MVC 4 API?

MVC (Model-View-Controller) web API is a framework that simplifies building websites that provide RESTful services. The idea is to make the site as lightweight as possible by providing the core information in a request- response format on top of which you can build web apps, desktop apps, or mobile clients by only changing the interface to your core functions (or “Actions” as they are referred to in the MVC naming convention). To have an idea why ASP.NET MVC is being introduced as the replacement for the existing ASP.NET, you can check this article: it lists the following problems with ASP.NET: unit testing is not straightforward with code-behind and separation of code and UI is not really implemented in it.

ASP.NET MVC provides a project template named “Web API” where this behavior is required from the application (i.e. making its I/O happen through the HTTP verbs GET and POST).



What is Entity Framework?

Entity Framework is an API that facilitates accessing data objects from the database (or other data store) from inside your application. Entity Data Model is the standard used to describe data objects that is implemented by Entity Framework. The ADO website describes EF as the preferred Object Relational Mapping in any new project.

EF components

The Entity Framework consists of three main components:
Conceptual Model: defined in a CSDL (Conceptual Schema Definition Language) file
Storage Model:  defined in a SSDL (Storage Schema Definition Language) file
Mapping: this is the mapping between the Conceptual Model and the Storage Model, defined in a MSL (Mapping Specification Language) file.
These three files are used by Linq to Entities to query entity types that are defined in a conceptual model.
More low-level details about Entity Framework is available here and here, and trough this portal you can find tutorials and samples.

EF Development Approaches

The following development approaches have been defined by the community as the standard approaches to using EF in your project:
Database First: You can use EF to reverse-engineer an existing database and generate code that presents the database objects you have in the database in an easily accessible coding interface.
Model First: This approach is used when there is no database available when beginning the project. You can create a model using the EF designer and when the model is finalized you can use the EF designer to generate DDL statements to create the database in Sql Server
Code First (a.k.a. Code Only): In this approach you can use your own classes and properties without having to create an .edmx file.


This is a sample of a data model not generated by the designer (including a DbGeography object):


What is the link between MVC and Entity Framework?

MVC was designed with Entity Framework as its preferred ORM (Object Relational Mapping) method. As you can see in the Add Controller dialog shown below,  Entity Framework is the default ORM (Object-relational mapping) framework used with MVC.


How does the Entity Framework support spatial objects?

Beginning from Entity Framework 5, Entity Data Model introduced two new primitive types to represent spatial objects: Geometry and Geography, and associated them with the new CLR types DBGeometry and DBGeography mentioned above.
This means that in CSDL (Conceptual Model) and SSDL (Storage Model) we now have the following types:


Now since Entity Framework supports spatial types natively, it is very easy to write a method like this one:


Where Model1Container is an entity framework model for a database that has a table called Store with a spatial column called Location. The Distance() method returns meters, according to this discussion.
You can also find a very nice video introduction about EF support for Spatial types here.
Finally this tutorial will provide you a hands-on sample for creating a custom database context class that maps to a sql table containing a spatial data field and does the basic data access operations on it.



No comments:

Post a Comment