Magic Wand: Object-relational mapping

ORM
ORM

Magic Wand: Object-relational mapping

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.

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

More from CodeSpread:

  • Try one ORM: Entity FrameworkTry one ORM: Entity FrameworkAs promised in the last article, We are going to share our experience of Entity Framework Ver 4.1. First of all, we have to add and configure Entity Framework in visual studio. We are going to u...
  • Agile Encounters UnpredictabilityAgile Encounters UnpredictabilityWhat is Agile? Wikipedia says “Agile software development is a group of software development methods based on iterative and incremental development, where requirements and solutions evolve through...
  • Few lines about Static code analysisFew lines about Static code analysisStatic code analysis We divide the term into two parts: Static+code analysis. Static in programming can be referred to as non-running/non-dynamic and code analysis is a process where the code is...
  • 23 Dec: Must Read Codes [11-15]23 Dec: Must Read Codes [11-15]We are sharing few must read codes/concepts which are required now and then. Each of these codereads doesn’t require a full length descriptive article so combining them into groups of five. CodeRe...
  • POCO vs Custom Classes/DTOPOCO vs Custom Classes/DTOWhen I started working on Entity Framework, I didn't catch the idea of POCO initially. I started with custom classes and later came to know the advantages of POCO over custom classes. What is POCO...
  • How to make a Connection between c# and mysql?How to make a Connection between c# and mysql?This is my first article on codespread.com, titled ‘Connection between c# and mysql’.Before going into details, we should know that mysql is a open source software for data base management system. ...
  • Best Practices: Does Professional Database exists?Best Practices: Does Professional Database exists?What is Database? Database is a structured set of data, which facilitates storage,search and retrieval. Databases revolves around tables and relationships between them. Imagine you have a truck lo...
  • Short Note on Performance TestingShort Note on Performance TestingWikipedia says "performance testing is in general testing performed to determine how a system performs in terms of responsiveness and stability under a particular workload. It can also serve to inv...