Click here to Skip to main content
Click here to Skip to main content

Magic Wand: Object-relational mapping

By , 2 Jan 2013
Rate this:
Please Sign up or sign in to vote.

As a developer, we are always looking for reusable components which can ease our life and I can suggest ORM as the developer’s magic wand because it wipes off the overhead of dealing directly with the database and encourages OOP way of data  operations.

What is Object-relational mapping?

Wikipedia says “Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in object-oriented programming languages.This creates, in effect, a “virtual object database” that can be used from within the programming language.”

The concept is easy to say and difficult to imagine so we first of all try to draw it to have a better clarity.

To create a mapping, we require two ends so what are the two ends here?

One of the end is data and other is objects. Don’t you think, we should know both these terms first, before creating a mapping between the two ends.

Data is maintained in databases and these databases usually follows relational model like- we can have different tables in our database and these tables can be referenced by each other by following one of the relationships-

  • One-to-One
  • One-to-Many
  • Many-to-One
  • many-to-many

Objects are real-word entities having some state and behavior. State of an object is represented by the fields and behavior by methods. Like, if we want to know the name of a user, we just have to call

User.Name where User is an object and Name is the state of the object.

Now, if we look from the developer’s point of view, then it is clearly visible that how easily we can operate an object. Just imagine, in similar way,if we can operate on our data then our life will become much more comfortable.

To achieve that, we need to create a mapper between our data and objects like-

ORM

Here,Mapper does a transition between those database and objects. For example, it transits all related records from a table to the mapped object . Mapper is the layer of abstraction that hides all the implementation details, we don’t handle all the sql overhead but just work with the generated useful Objects,the OOP way.

What is the need of Object-relational mapping? or What are the benefits of Object-relational mapping?

  • Simple: We have already discussed, how easy it is, to work with objects than a relational database.
  • Less Code :  Less code as compared to manual queries, handwritten stored procedures, triggers,functions
  • Reduced development and maintenance costs: No big shots code required for regular database operations.
  • Caching: Object caching provides improved system performance.
  • Concurrency: Multiple users can do operations at the same time.
  • Loading of related data: All objects which are related to the object called, are loaded simultaneously.
  • Keys and Identifiers: Keys are important for the calls and they are properly managed.
  • Transactions: Transactions Management rules are properly followed and any operation occurs within a transaction scope.

What are the options available?

NHibernate:

NHibernate is a great ORM for the Microsoft .NET platform. NHibernate is free as open source software that is distributed under the GNU Lesser General Public License. Features:

  • Mapping from .NET classes to database tables.
  • Generates SQL commands.
  • Query execution and data retrieval.
  • Object oriented.
  • Portable with little overhead.

LINQ to SQL:

LINQ to SQL is Microsoft’s answer to all the existing ORM’s available in the market and it has some nice features plus it is quite easy to get started with. It basically works with SQLClient adapters to communicate with database.

Features:

  • Microsoft Support is the biggest plus point.
  • Cannot be termed as full fledged ORM but captures the basic principle of ORM.
  • LINQ is database independent and supports a very rich extensibility model which can be applied to any data source.
  • Light-Weight.
  • Easy Configuration.

Entity Framework:

Entity Framework is another entry from Microsoft which is very powerful.

The Entity Framework’s ORM implementation provides services like change tracking, identity resolution, lazy loading, and query translation so that developers can focus on their application-specific business logic rather than the data access fundamentals.

Features:

  • Works with a variety of database servers (including Microsoft SQL Server, Oracle, and DB2)
  • Includes a rich mapping engine that can handle real-world database schemas and works well with stored procedures
  • Provides integrated Visual Studio tools to visually create entity models and to auto-generate models from an existing database. New databases can be deployed from a model, which can also be hand-edited for full control
  • Provides a Code First experience to create entity models using code. Code First can map to an existing database or generate a database from the model.
  • Integrates well into all the .NET application programming models including ASP.NET, Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF), and WCF Data Services (formerly ADO.NET Data Services)

reference: http://msdn.microsoft.com/en-us/data/aa937709.aspx

OpenAccess:

OpenAccess ORM is developed by the .NET solutions provider Telerik. There is no major difference in the functionality and features delivered by ORMs and OpenAccess.

Features:

  • Visual Entity Designer with numerous wizards.
  • LINQ support
  • Disconnected Data support
  • Caching support

Example.

In our next article,we will showcase an example with one of the ORM.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Himanshu DS
Web Developer CodeSpread.com
India India
I am a regular developer working on c#,asp.net,sql,cms,digital marketing related sites. Through my blog, I am showing the non-technical part of our daily technical terms and processes which can be used to describe it to a layman.Sometimes I write basic technical posts also.
Follow on   Twitter

Comments and Discussions

 
QuestionIrwsoft Data Framework PinmemberMember 976235920-Jan-14 17:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 2 Jan 2013
Article Copyright 2013 by Himanshu DS
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid