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-
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-
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 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 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.
- 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.
- Easy Configuration.
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.
- 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)
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.
- Visual Entity Designer with numerous wizards.
- LINQ support
- Disconnected Data support
- Caching support