Quote for the Week

"Learn to enjoy every moment of your life"

Tuesday, May 15, 2018

ORM(Object Relational Mapping) in .NET

An Object Relation Mapping(ORM) library helps developers to create data access applications by programming against a conceptual model instead of programming against a relational database model.  ORMs bring lot of advantages and few of the prominent ones are -  RDBMS independent data access layer development, rapid application development, huge reduction in code, auto generation of boilerplate methods for CRUD operations and abstract access to data.

I have collected some of ORMs which can used for developing .Net Applications -


LLBLGen Pro Runtime Framework



The LLBLGen Pro Runtime Framework is an optional ORM that works in conjunction with the LLBLGen entity modeling tool. We say it’s optional because LLBLGen can also work with other ORMs such as Entity Framework.


Like Entity Framework, the LLBLGen Pro Runtime Framework is full OOP style ORM. But it differs in several ways, the first being an emphasis on performance. Though EF Core is significantly faster than classic Entity Framework, both are still considerably slower than other ORMs. Frans Bouma, author of LLBLGen Pro, hosts a performance shootout comparing materialization speed of various .NET data access / ORM implementations.


The LLBLGen Pro Runtime Framework also differs from EF/EF Core in that it is not context-bound. Each entity tracks its own changes, allowing you to manipulate an object graph in memory without holding a context open. (Which will make your DBA happy because not holding an open context also means you aren’t holding an open connection, which would otherwise interfere with database connection pooling.)



Like most ORMs for .NET Core, there are some limitations with the Core version of LLBLGen Pro Runtime Framework. However, this is mostly limited to missing features in .NET Core itself such as TransactionScope not being supported by SqlClient or fewer objects being binary serializable.

Microsoft Entity Framework



Entity Framework is one of the most advanced and feature rich ORM library from Microsoft.  Using the Entity Framework, developers can write data access code using the popular LINQ queries to retrieve and manipulate data as strongly typed objects. The Entity Framework’s ORM supports change tracking, identity resolution, lazy loading and query translation so that developers can focus on building their application specific business logic rather than wring thousands of lines of code for fundamental data access.

The Entity Framework is built on top of  ADO.NET provider model and it integrates very well with Visual Studio. It supports Domain model and Code First model and have providers for various databases like Sql Server, SQL Azure, Oracle, MySql and DB2.

NHibernate



NHibernate is the .NET sibling of popular Java ORM framework Hibernate. If you are looking for an ORM library outside of Microsoft provided libraries, NHibernate is the most probable candidate you would end up choosing. NHibernate  is a mature, open source ORM framework which is actively developed, fully featured, highly performant and successfully used in thousands of projects.


This framework is being developed for over a decade by community members and used in thousands of commercial and open source applications. NHibernate uses XML description of entities and relationships, automatically generates SQL for retrieving and manipulating data. Optionally you can describe mappings metadata with attributes in source code.

Microsoft LINQ to SQL



LINQ to SQL is another Microsoft ORM library which was released before Entity Framework. Before shipping Entity Framework, Microsoft heavily promoted Linq To Sql and it is used in many .NET applications. The LINQ to SQL provider allows LINQ to query SQL Server databases to retrieve and manipulate data as strongly typed objects. Basically it’s a semi-ORM which is meant for Sql Server and if you are looking for building generic data access code that works on any database, then Linq to Sql is not the choice for you.

ORMapster


ORMapster for Visual Studio 2013 is a simple ORM data mapper and code generator extension that does one thing: iI reads your data source and creates a data access layer with LINQ self-tracking entities. That's it. The resulting data entities can then be used in your projects, including WCF, ASP.NET MVC, Web Forms, Windows Forms and so on.


ORMapster integrates directly with Visual Studio, and versions are available for Visual Studio 2010 through 2013. To work with the LINQ entities created by ORMapster, you'll also need the ORMapster Framework NuGet package.


There's a handy Getting Started guide on the ORMapster Web site that walks you through building a data model based on the beloved Northwind sample database. Also, although there's pretty much no documentation explaining it, you might check out the ORMapster Web UI NuGet package, which appears to be a DataSourceControl optimized for data binding with ORMapster-generated entities.



There are few other less known libraries available and used in few projects – AutoMapper, EntitySpaces.NET, LightSpeed etc. For a comprehensive list of .NET ORM libraries.

Which one is the best?


Hmm... this is a tough question to answer as it highly depends on the projects to say which ORM is best for a .NET project. There are various factors we need to consider before nailing down on the ORM – availability of developer pool with a specific ORM knowledge, size, performance & scalability requirements of the target application, level of support required and  time to market factors.


In the list of available ORM libraries Microsoft Entity Framework & NHibernate stands out in the crowd. If you organization is a fan or open source community and looking for a well oiled and proven library then you should consider NHibernate.


On the other side, Microsoft is heavily investing in developing Entity Framework and in the past couple of years 5 iterations has been released.  It’s one of the most actively developed and well optimized ORM library that plays well any of the .NET technologies. The future for Entity Framework looks very promising as the big boss Microsoft is driving it. There is a good developer community available in the market with Entity Framework skills.


So, according to points above and based on your requirements choose between Entity Framework or NHibernate.

No comments: