Object-Relational Mapping - Taking the Horror Out of Data Access
2008-05-01 06:11
197 查看
of.com" target=_blank>Object-Relational
of.com" target=_blank>Mapping – of.com" target=_blank>Taking of.com" target=_blank>the of.com" target=_blank>Horror of.com" target=_blank>Out of Data Access
1.
of.com" target=_blank>Horror stories and of.com" target=_blank>the of.com" target=_blank>Object Data divide
Let’s face it, of.com" target=_blank>the world is getting more and more OO, but we are still
pretty happy with our relational Databases. In fact of.com" target=_blank>the RDBMS is of.com" target=_blank>the foundation
upon which basically all of our Data driven applications reside. Having said
this we are pretty aware of of.com" target=_blank>the fact that of.com" target=_blank>the relational Data models we deploy
are limited. Sometimes of.com" target=_blank>they aren’t much more than a bunch of tables that
are increasingly cursed due to limitations and design flaws.
To develop great applications, we
need a solid foundation based on a great Data model, a Data model that should
matches real life as closely as possible, of.com" target=_blank>the so called domain model. How many
Database schemas actually resemble of.com" target=_blank>the real-world problem? Data Access has become
a time-consuming of.com" target=_blank>Horror story that can take of.com" target=_blank>the fun of.com" target=_blank>Out of any development project.
But it doesn’t have to be this way.
2.
Benefits of OO in stateless business methods
Even though relational of.com" target=_blank>theory is great for storing and retrieving large
amounts of structured Data, it hardly makes for readable business methods. of.com" target=_blank>the
reason being that of.com" target=_blank>the relational Data model contains very little information
(despite what of.com" target=_blank>the name implies) abof.com" target=_blank>Out of.com" target=_blank>the relationships and intended use of
of.com" target=_blank>the Data. It has a low level of abstraction in of.com" target=_blank>the same way as C compared to
VisualBasic. A properly implemented of.com" target=_blank>Object-oriented Data model on of.com" target=_blank>the oof.com" target=_blank>ther
hand gives you of.com" target=_blank>the full picture and allows business methods to contain more
straightforward, readable, code. Comparing two code snippets proves our point.
of.com" target=_blank>the first code snippet interfaces of.com" target=_blank>the relational Data model through SQL, of.com" target=_blank>the
second interfaces of.com" target=_blank>the of.com" target=_blank>Object-oriented Data model through OO-notation (in this
example we use of.com" target=_blank>the API of an O/R DAL generated by Pragmatier
Data Tier Builder, se reference at of.com" target=_blank>the end of this article):
Public Sub GiveRaise(ByVal ConnStr As String, _
?????????????????????ByVal EmployeeID As Long, _
?????????????????????ByVal RaisePercent As Double)
Dim strSQLFetchSalary As String
Dim strSQLUpdateSalary As String
Dim Salary As Long
Dim rs As ADODB.Recordset
Dim conn As New ADODB.Connection
conn.Open ConnStr
strSQLFetchSalary = "Select Salary From " & _
????????????????????"Employees Where emp_id = " & _
????????????????????EmployeeID
rs.Open strSQLFetchSalary, conn
If Not (rs.Bof Or rs.Eof) of.com" target=_blank>then
???Salary = rs("Salary")
End If
rs.Close
Salary = Salary + (Salary * RaisePercent)
strSQLUpdateSalary = "Update Employees Set (Salary) = " & _
??????????????????? ??Salary & " Where emp_id = " & _
??????????????????? ??EmployeeID
conn.Execute strSQLUpdateSalary
conn.Close
End Sub
fig 1.
Business methods using SQL.
Public Sub GiveRaise(ByVal EmployeeID As Long, _
?????????????????????ByVal RaisePercent As Double)
Dim of.com" target=_blank>ObjectFactory As New EmployeeServer.CComponent
Dim Employee As Employee
Set Employee = of.com" target=_blank>ObjectFactory.GetEmployee(EmployeeID)
Employee.Salary = Employee.Salary + (Employee.Salary * RaisePercent)
End Sub
fig 2. of.com" target=_blank>the
same business methods calling persistent of.com" target=_blank>Objects in an O/R DAL.
3.
Introducing an O/R DAL
Obviously of.com" target=_blank>there is a problem if we want to use OO in our business methods yet
store of.com" target=_blank>the Data in an RDBMS. We have to map of.com" target=_blank>the of.com" target=_blank>Object-oriented Data model to
a relational Data model, basically of.com" target=_blank>Mapping of.com" target=_blank>Objects to tables. To do this properly
we need to introduce an of.com" target=_blank>Object-to-relational of.com" target=_blank>Mapping Data Access layer, a so-called
O/R DAL. All Data Access should be done by calling of.com" target=_blank>the persistent Data of.com" target=_blank>Objects
in this layer, narrowing any maintenance efforts such as Database refactoring
or upgrades to a single part of of.com" target=_blank>the application.
of.com" target=_blank>the answer to why we choose to implement
an of.com" target=_blank>Object-to-relational of.com" target=_blank>Mapping Data Access layer (O/R DAL) is obvious when
we present of.com" target=_blank>the exciting features it will bring us. What it basically spell is
faster, simpler and more maintainable development:
–of.com" target=_blank>the persistent Data of.com" target=_blank>Objects
that we have created are strongly typed, saving us from costly and embarrassing
type mismatches and improving of.com" target=_blank>the accuracy of our code.
–of.com" target=_blank>the components expose of.com" target=_blank>themselves beautifully through Intellisense, eliminating
typos and problems with remembering property names.
–And of.com" target=_blank>then for of.com" target=_blank>the greatest feature of all… you can navigate of.com" target=_blank>the
of.com" target=_blank>Object-oriented Data model by using of.com" target=_blank>the OO-dot syntax, such as OrderItem.Order.Customer.BillingAddress.
Beautiful and sensational! Even your client can be convinced that this refers
to of.com" target=_blank>the billing address of of.com" target=_blank>the customer whose order item is being processed.
fig 3. Navigate through of.com" target=_blank>the Data structure using
of.com" target=_blank>the OO-dot and Intellisense. No more worries abof.com" target=_blank>Out spelling, vocabulary or dialect.
Even though we Access of.com" target=_blank>the Data at
a higher level by navigating through beautiful of.com" target=_blank>Objects, of.com" target=_blank>the Data is still available
in a relational form allowing you to plug straight into of.com" target=_blank>the db to perform number
crunching or Data mining. Not to mention of.com" target=_blank>the fact that you can keep SQL-Server
and your existing DBA withof.com" target=_blank>Out retraining.
of.com" target=_blank>Mapping – of.com" target=_blank>Taking of.com" target=_blank>the of.com" target=_blank>Horror of.com" target=_blank>Out of Data Access
1.
of.com" target=_blank>Horror stories and of.com" target=_blank>the of.com" target=_blank>Object Data divide
Let’s face it, of.com" target=_blank>the world is getting more and more OO, but we are still
pretty happy with our relational Databases. In fact of.com" target=_blank>the RDBMS is of.com" target=_blank>the foundation
upon which basically all of our Data driven applications reside. Having said
this we are pretty aware of of.com" target=_blank>the fact that of.com" target=_blank>the relational Data models we deploy
are limited. Sometimes of.com" target=_blank>they aren’t much more than a bunch of tables that
are increasingly cursed due to limitations and design flaws.
To develop great applications, we
need a solid foundation based on a great Data model, a Data model that should
matches real life as closely as possible, of.com" target=_blank>the so called domain model. How many
Database schemas actually resemble of.com" target=_blank>the real-world problem? Data Access has become
a time-consuming of.com" target=_blank>Horror story that can take of.com" target=_blank>the fun of.com" target=_blank>Out of any development project.
But it doesn’t have to be this way.
2.
Benefits of OO in stateless business methods
Even though relational of.com" target=_blank>theory is great for storing and retrieving large
amounts of structured Data, it hardly makes for readable business methods. of.com" target=_blank>the
reason being that of.com" target=_blank>the relational Data model contains very little information
(despite what of.com" target=_blank>the name implies) abof.com" target=_blank>Out of.com" target=_blank>the relationships and intended use of
of.com" target=_blank>the Data. It has a low level of abstraction in of.com" target=_blank>the same way as C compared to
VisualBasic. A properly implemented of.com" target=_blank>Object-oriented Data model on of.com" target=_blank>the oof.com" target=_blank>ther
hand gives you of.com" target=_blank>the full picture and allows business methods to contain more
straightforward, readable, code. Comparing two code snippets proves our point.
of.com" target=_blank>the first code snippet interfaces of.com" target=_blank>the relational Data model through SQL, of.com" target=_blank>the
second interfaces of.com" target=_blank>the of.com" target=_blank>Object-oriented Data model through OO-notation (in this
example we use of.com" target=_blank>the API of an O/R DAL generated by Pragmatier
Data Tier Builder, se reference at of.com" target=_blank>the end of this article):
Public Sub GiveRaise(ByVal ConnStr As String, _
?????????????????????ByVal EmployeeID As Long, _
?????????????????????ByVal RaisePercent As Double)
Dim strSQLFetchSalary As String
Dim strSQLUpdateSalary As String
Dim Salary As Long
Dim rs As ADODB.Recordset
Dim conn As New ADODB.Connection
conn.Open ConnStr
strSQLFetchSalary = "Select Salary From " & _
????????????????????"Employees Where emp_id = " & _
????????????????????EmployeeID
rs.Open strSQLFetchSalary, conn
If Not (rs.Bof Or rs.Eof) of.com" target=_blank>then
???Salary = rs("Salary")
End If
rs.Close
Salary = Salary + (Salary * RaisePercent)
strSQLUpdateSalary = "Update Employees Set (Salary) = " & _
??????????????????? ??Salary & " Where emp_id = " & _
??????????????????? ??EmployeeID
conn.Execute strSQLUpdateSalary
conn.Close
End Sub
fig 1.
Business methods using SQL.
Public Sub GiveRaise(ByVal EmployeeID As Long, _
?????????????????????ByVal RaisePercent As Double)
Dim of.com" target=_blank>ObjectFactory As New EmployeeServer.CComponent
Dim Employee As Employee
Set Employee = of.com" target=_blank>ObjectFactory.GetEmployee(EmployeeID)
Employee.Salary = Employee.Salary + (Employee.Salary * RaisePercent)
End Sub
fig 2. of.com" target=_blank>the
same business methods calling persistent of.com" target=_blank>Objects in an O/R DAL.
3.
Introducing an O/R DAL
Obviously of.com" target=_blank>there is a problem if we want to use OO in our business methods yet
store of.com" target=_blank>the Data in an RDBMS. We have to map of.com" target=_blank>the of.com" target=_blank>Object-oriented Data model to
a relational Data model, basically of.com" target=_blank>Mapping of.com" target=_blank>Objects to tables. To do this properly
we need to introduce an of.com" target=_blank>Object-to-relational of.com" target=_blank>Mapping Data Access layer, a so-called
O/R DAL. All Data Access should be done by calling of.com" target=_blank>the persistent Data of.com" target=_blank>Objects
in this layer, narrowing any maintenance efforts such as Database refactoring
or upgrades to a single part of of.com" target=_blank>the application.
of.com" target=_blank>the answer to why we choose to implement
an of.com" target=_blank>Object-to-relational of.com" target=_blank>Mapping Data Access layer (O/R DAL) is obvious when
we present of.com" target=_blank>the exciting features it will bring us. What it basically spell is
faster, simpler and more maintainable development:
–of.com" target=_blank>the persistent Data of.com" target=_blank>Objects
that we have created are strongly typed, saving us from costly and embarrassing
type mismatches and improving of.com" target=_blank>the accuracy of our code.
–of.com" target=_blank>the components expose of.com" target=_blank>themselves beautifully through Intellisense, eliminating
typos and problems with remembering property names.
–And of.com" target=_blank>then for of.com" target=_blank>the greatest feature of all… you can navigate of.com" target=_blank>the
of.com" target=_blank>Object-oriented Data model by using of.com" target=_blank>the OO-dot syntax, such as OrderItem.Order.Customer.BillingAddress.
Beautiful and sensational! Even your client can be convinced that this refers
to of.com" target=_blank>the billing address of of.com" target=_blank>the customer whose order item is being processed.
fig 3. Navigate through of.com" target=_blank>the Data structure using
of.com" target=_blank>the OO-dot and Intellisense. No more worries abof.com" target=_blank>Out spelling, vocabulary or dialect.
Even though we Access of.com" target=_blank>the Data at
a higher level by navigating through beautiful of.com" target=_blank>Objects, of.com" target=_blank>the Data is still available
in a relational form allowing you to plug straight into of.com" target=_blank>the db to perform number
crunching or Data mining. Not to mention of.com" target=_blank>the fact that you can keep SQL-Server
and your existing DBA withof.com" target=_blank>Out retraining.
相关文章推荐
- The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[BMS.DataAccess.Models.ServiceTyp
- Error creating object Please verify that the Microsoft Data Access Components 2.1 (or later) have been properly installe
- The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. 错误的原因及解决方案
- URL-encoded form data is not valid ,Operation is not valid due to the current state of the object
- The object of type 'RectTransform' has been destroyed but you are still trying to access it
- BUG: The GetSchemaTable method of the SqlDataReader object returns the wrong column name
- LINQ: The Future of Data Access in C# 3.0
- Inside The C++ Object Model ---- The Semantics of Data
- [Javascript] A function works like 'print_r()' in PHP to print out the details of an object for JS debugging
- DataStage job monitor fails with out of memory error in the javacore file
- BUG: The GetSchemaTable method of the SqlDataReader object returns the wrong column name
- [JavaScript] Access the properties of an object
- The size of the object heap + VM data exceeds the maximum representable size问题解决办法
- Error creating object Please verify that the Microsoft Data Access Components 2.1 (or later) have be
- EF 6.0 The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. c#中的时间
- Inside The C++ Object Model ---- The Semantics of Data
- Decoupling models from the database: Data Access Object pattern in PHP
- custom format Property of business object for the data binding
- org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback.....Parameter index out of range (1 > number of parameters, which is 0).;
- The Solution of "The calling thread cannot access this object beacuse a different thread owns it."