Introduction:
In this article,i am going to explain about how to perform the object-relational mapping in asp.net applications.
Main:
Relational databases treat data in terms of rows and columns, and objects treat things in terms of arrays and properties. Object-relational mapping (OR mapping) bridges these two worlds so your code can look like object code and still store itself in a relational database.
you’ll create a simple object data model for a video store application. You’ll learn how to create mapped objects as well as workarounds for common pitfalls and quirks found in ADO.NET that can adversely affect your data model. Let’s start by discussing why you may need to worry about OR mapping.
OR mapping provides three main benefits:
Encapsulation: This is one of the primary pillars of object-oriented design, and OR mapping extends this concept to the database.
Identity: A mapped object has an identity. In concrete terms it has a unique identifier. Most objects that are not mapped to a database lack a real identity independent of the data presented to the end user. Although it’s possible to create an identity for the objects using hashcodes or a similar methodology, mapped objects take this to the next logical level.
Behavior: Data in a database does not have behavior. By fusing the world of databases to software code, you have the unique opportunity to define how your data should interact with your system in a clean and reusable way.
Why Object Relational Mapping?
OR mapping keeps the code in your application encapsulated and clean, and it makes it easy to manage changes in the database. By building an OR mapping component, your database logic will be encapsulated and hidden away from the rest of your system. Because the logic of loading and saving your object rests entirely in the OR mapping subsystem, the code in the rest of your program uses only clean object-oriented code. It also makes it far easier to make changes to the database later without having to alter every place you reference the data.
The core issue is short-term vs. long-term laziness. Using short-term laziness would probably put SQL queries directly in your Web pages and just use SqlDataReader objects to work with your data. Past the initial implementation, this method makes your code hard to maintain. If you add a column to the database, you’ll have to find every reference to that table in your application and update it. Also, as the need to use the same table on multiple pages arises, you’ll find yourself copying and pasting large sections of code to get the same functionality on these new pages as well.
Long-term laziness means taking the upfront time costs associated with building out the object relational mapping for your table. It will definitely take you longer to initially produce these objects, but in the long term it will mean much less work.
Conclusion:
Hope this helps,
Happy coding.