Pages

Wednesday 17 April 2013

Object Relational Mapping: Frameworks and Advantages

Object Relational Mapping: Frameworks and Advantages

ORM (Object Relational Mapping) wraps your tables or stored procedures in classes in your programming language, so that instead of writing SQL statements to interact with your database, you use methods and properties of objects.

There are a lot of frameworks available for implementing ORM (Object Relational Mapping) in different programming languages.

Based on abstraction, ORM (Object Relational Mapping) manages the mapping details between a set of objects and underlying relational databases, XML repositories or other data sources and sinks, while simultaneously hiding the often changing details of related interfaces from developers and the code they create.

ORM (Object Relational Mapping) hides and encapsulates change in the data source itself, so that when data sources or their APIs change, only ORM needs to change to keep up—not the applications that use ORM to insulate themselves from this kind of effort. This capacity lets developers take advantage of new classes as they become available and also makes it easy to extend ORM-based applications. In many cases, ORM changes can incorporate new technology and capability without requiring changes to the code for related applications.

Example of ORM (Object Relational Mapping)

Lets say you fire following SQL query using traditional way:

String sql = "SELECT ... FROM persons WHERE id = 10";
DbCommand cmd = new DbCommand(connection, sql);
Result res = cmd.Execute();
String name = res[0]["FIRST_NAME"];

ORM enables you to write same logic on the objects and classes like this:

Person p = repository.GetPerson(10);
String name = p.FirstName;

Some frameworks also put a lot of the code in as static methods on the classes themselves, which means you could do something like this instead:

Person p = Person.Get(10);

Some also implement complex query systems, so you could do this:

Person p = Person.Get(Person.Properties.Id == 10);

Advantages of ORM (Object Relational Mapping)

1. You can hide the SQL away from your logic code.

2. ORM has the benefit of allowing you to more easily support more database engines. For instance, MS SQL Server and Oracle has different names on typical functions, and different ways to do calculations with dates, so a query to "get me all persons edited the last 24 hours" might entail different SQL syntax just for those two database engines. This difference can be put away from your logic code.

3. You can focus on writing the logic, instead of getting all the SQL right. The code will typically be more readable as well, since it doesn't contain all the "plumbing" necessary to talk to the database.

ORM (Object Relational Mapping) Frameworks:

In .NET, ADO.NET Entity Framework is the widely used ORM Framework. There is a long list of ORM frameworks supported in different programming languages. You can read this list from wikipedia.

No comments:

Post a Comment

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.