Leverage the power of ORM tools to design data access layers that are independent of the underlying database
Object Relational Mappers (ORMs) are tools that have evolved with the view to increase development productivity, provide database independence, and also to abstract calls to the underlying database. Object Relational Mapper (ORM) tools help you to write your data access code without having to bother about the database underneath. There are plenty of Object Relational Mapping tools available now. You have nHibernate, LINQ to SQL, Entity Spaces, and what not.
Pre-Requisites
To work Telerik Open Access ORM tool and the code examples illustrated in this article, you should have the following installed in your system:
- Visual Studio 2010/2012
- Telerik Open Access ORM
You can download a copy of Telerik OpenAccess ORM from this link: http://www.telerik.com/community/forums/orm/orm-express/openaccess-orm-free-edition-where-to-download.aspx
Before we delve deep into Telerik OpenAccess ORM and start using it, let’s take a quick tour of the other available ORMs.
LINQ to SQL
Language Integrated Query (LINQ), an extension of the C# language, provides a framework for performing CRUD operations with relational data in a strongly typed way. It is a query translation and execution pipeline that has been introduced as part of the C# 3.0 library. LINQ comprises of a set of query operators – one for each data source, i.e., you have different LINQ flavours for LDAP, Objects, CSV, XML, Entities, SQL, etc. LINQ to SQL is an ORM tool that can be used to abstract data access code – you are however constrained to use SQL Server database underneath.
NHibernate and Fluent NHibernate
NHibernate is a lightweight Object Relational Mapping (ORM) tool for .NET. It also has a statically compiled counterpart called Fluent NHibernate – an XML-less; compile safe, automated NHibernate mapper that also provides support for LINQ. In essence, Fluent NHibernate lets you map .NET classes with NHibernate without the need for those cumbersome XML files.
The ADO.NET Entity Framework
The ADO.NET Entity Framework is an extended Object Relational Mapping Tool from Microsoft that has over the past few years become increasingly popular and is being widely used these days. It is essentially used to abstract the object model of an application from its relational or logical model. You can use it to objectify your application’s data and isolate the logical or relational model of your application from the object model.
The ADO.NET Entity Framework 4.0 provides the following features:
- Persistence Ignorance
- Lazy Loading
- Better N-Tier Support
- Model-First and Code Only Development
- Built-In Functions, UDFs and Model Defined Functions
Telerik Open Access ORM
The Telerik OpenAccess ORM tool attempts to solve the object-relational impedance mismatch that often exists amongst the application and relational database management systems.
Getting Started
This section assumes that you have already downloaded and installed Telerik OpenAccess ORM in your system. To get started using Telerik OpenAccess ORM tool, follow these simple steps:
- Click on File -> New -> Project in Visual Studio
- From the list of the installed templates displayed, select “Web”
- Next select “TelerikOpenAccess Web Application” from the list of the templates displayed in the right pane
- Name the project and click OK
- In the “Select OpenAccess Domain Model Type” window that comes up next, select the option “Populate from database”, specify the entities name and click on Next
- In the “Setup Database Connection” window that is shown next, specify the connection parameters and click on Next
- In the “Choose Database Items” window that comes next, select the database items you would like to be in your data context and then click on Next
- You may define your naming rules if you want to in the “Define Naming Rules” window that is shown next, else, you may just want to skip by clicking on Next
- In the next window, i.e., “Code Generation Settings”, you may want to specify the code generation templates and project output folder, else, you may just click on “Finish” to complete generation of Telerik OpenAccess ORM data context.
And you are done! You’ll see a .rlinq file created in your solution explorer. You can now make use of the data context you just created to perform CRUD operations against your database.
You can use the data context much the same way you use your LINQ or Entity Framework data contexts. Here is an example:
using (EntitiesModel dbContext = new EntitiesModel()) { }
To query data you can use this code:
using ( EntitiesModel dbContext = new EntitiesModel() ) { IEnumerable<Employee> employee = dbContext.Employees.Where( e => e.FirstName == "Joydip"); }
The following snippet of code illustrates how you can insert data using the Telerik OpenAccess ORM data context:
Employee newEmployee = new Employee(); newEmployee.FirstName = "Joydip"; newEmployee.LastName = "Kanjilal"; dbContext.Add(newEmployee); dbContext.SaveChanges();
To update an employee record using the Telerik OpenAccess ORM data context, you can use this code:
Employee employeeToUpdate = dbContext.Employees.First(); employeeToUpdate.Phone = "+918978001180"; dbContext.SaveChanges();
To delete a record you can write the following code:
Employee employeeToDelete = dbContext.Employees.Last(); dbContext.Delete(employeeToDelete); dbContext.SaveChanges();
References
http://www.telerik.com/support/documentation-and-tutorials/step-by-step-tutorial-for-openaccess.aspx
http://www.telerik.com/help/openaccess-orm/openaccess-orm-introduction.html
http://www.telerik.com/help/openaccess-orm/developer-guide-crud-overview.html
Summary
In this article we discussed popular ORM tools and then how we can get started with the Telerik OpenAccess ORM tool. Happy reading!