您的位置:首页 > 移动开发 > Objective-C

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐