Você está na página 1de 17

OR Impedance Mismatch

OR Impedance Mismatch

•Conflicting type systems


•Conflicting design goals
Database system focuses specifically on the storage and retrieval of data, whereas an object system focuses specifically
on the union of state and behavior for easier programmer manipulation
•Conflicting architectural style
Most database products are built to assume a fundamentally client/server style of interaction, assuming the database is
located elsewhere on the network, and programs accessing the database will be doing so via some sort of remote
access protocol. Object systems assume the precise opposite, and in fact, perform significantly worse when distributed.
•Differing structural relationships
Relational data stores track entities in terms of relations between tuples and tuplesets; object-oriented systems instead
prefer to track entities in terms of classes, compilation of state and behavior that relates to one another through IS-A
and/or HAS-A style unidirectional connections. Where databases use foreign-key relationships to indicate relations,
objects use references or pointers

2
OR Impedance Mismatch

•Differing identity constructs


Object systems use an implicit sense of identity to distinguish between objects of similar state (the ubiquitous this
pointer or reference), yet databases require that sense of identity to be explicit via primary key column or columns. In
fact, in modern object-oriented languages an object system cannot be built without a sense of object identity, whereas
relational tables can have no primary key whatsoever, if desired.
•Transactional boundaries
Object systems do not have any sense of "transactional demarcation" when working with the objects, whereas database
instances must in order to deal with the multi-user requirements of a modern client/server-based system .
•Query/access capabilities
Retrieving data stored in a relational database makes use of SQL, a declarative language predicated on the
mathematical theory of relational algebra and predicate. In object systems, the entire object is required in order to
navigate from one object to the next, meaning that the entire graph of objects is necessary in order to find two
disparate parts of data—for a system intended to remain entirely in working memory, this is of no concern, but for a
system whose principal access is intended to be distributed, as relational database are, this can be a crippling problem.

3
Object Relational Mapping
Wikipedia defines an ORM as: “a programming technique for
converting data between incompatible type systems in relational
databases and object-oriented programming languages. This
creates, in effect, a "virtual object database," which can be used
from within the programming language.” An ORM has to provide
a facility to map database tables to domain objects, using a design
surface or wizard. This mapping is in-between your database and
domain model, independent from the source code and the
database. The ORM runtime then converts the commands issued
by the domain model against the mapping into back end database
retrieval and SQL statements. Mapping allows an application to
deal seamlessly with several different database models, or even
databases.

4
The LINQ Project
C# VB Others…

.NET Language Integrated Query

Standard
DLinq XLinq
Query
(ADO.NET) (System.Xml)
Operators

<book>
<title/>
<author/>
<year/>
<price/>
</book>

Objects SQL WinFS XML


Data Access In APIs Today

Queries in
SqlConnection c = new SqlConnection(…);
c.Open(); quotes
SqlCommand cmd = new SqlCommand(
@"SELECT c.Name, c.Phone Arguments
FROM Customers c loosely bound
WHERE c.City = @p0"
);
Results
cmd.Parameters.AddWithValue("@po",
loosely typed
"London");
DataReader dr = c.Execute(cmd); Compiler
while (dr.Read()) { cannot help
string name = dr.GetString(0); catch
string phone = dr.GetString(1); mistakes
Data Access with DLINQ

public class Customer Classes


{ describe data
public int Id;
Tables are
public string Name;
collections
public string Phone;

}
Query is
natural part
Table<Customer> customers = db.Customers;
of the
var contacts = language
The compiler
from c in customers helps you out
where c.City == "London"
DLinq For Relational Data
Accessing data with DLinq
public class Customer { … Classes
}
describe data

public class Northwind: DataContext Tables are


{ like collections
public Table<Customer> Customers;
… Strongly typed
connection
}
Northwind db = new Northwind(…);
var contacts = Integrated
from c in db.Customers query syntax
where c.City == "London"
Strongly
select new { c.Name, c.Phone }; typed
results
Architecture
from c in db.Customers
where c.City == "London" Application
select
new { c.Name, c.Phone }

LINQ Query Objects SubmitChanges()

Services:
DLinq - Change tracking
- Concurrency control
(ADO.NET) - Object identity

SQL Query Rows SQL or


Stored
Procs
select Name, Phone
from customers
where city = 'London'
SQLServ
Key Takeaways
Language integrated data access
Maps tables and rows to classes and objects
Builds on ADO.NET and .NET Transactions
Mapping
Encoded in attributes
Relationships map to properties
Manually authored or tool generated
Persistence
Automatic change tracking
Updates through SQL or stored procedures
DataContext
Strongly typed database
Querying For Objects
Key Takeaways
Language Integrated Query
Compile-time type checking, IntelliSense

SQL-like query syntax


With support for hierarchy and relationships

Intelligent object loading


Deferred or immediate

12
Updating Objects
Key Takeaways
Auto-generated updates
Using optimistic concurrency

Transactions
Integrates with System.Transactions

SQL pass-through
Returning objects from SQL queries

14
DLinq Summary
Allows access to relational data as objects

Supports Language Integrated Query

Works with existing infrastructure

Unifies programming model for objects,


relational and XML

15
When to Use LINQ to SQL?
The primary scenario for using LINQ to SQL is when building
applications with a rapid development cycle and a simple one-to-
one object to relational mapping against the Microsoft SQL Server
family of databases. In other words, when building an application
whose object model is structured very similarly to the existing
database structure, or when a database for the application does not
yet exist and there is no predisposition against creating a database
schema that mirrors the object model

16
When to Use LINQ to SQL?
I want to… LINQ to SQL is
applicable

Use an ORM solution and my database is 1:1 with my object


model

Use an ORM solution with inheritance hierarchies that are


stored in a single table

Use my own plain CLR classes instead of using generated


classes or deriving from a base class or implementing an
interface

Leverage LINQ as the way I write queries

Use an ORM but I want something that is very performant and


where I can optimize performance through stored procedures
and compiled queries

17

Você também pode gostar