Date post: | 10-May-2015 |
Category: |
Technology |
Upload: | |
View: | 7,135 times |
Download: | 0 times |
OR Impedance OR Impedance MismatchMismatch
2
OR Impedance OR Impedance MismatchMismatch•Conflicting type systemsConflicting type systems•Conflicting design goalsConflicting design goals
Database system focuses specifically on the storage and retrieval of data, whereas an object 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 system focuses specifically on the union of state and behavior for easier programmer manipulationmanipulation
•Conflicting architectural styleConflicting architectural styleMost database products are built to assume a fundamentally client/server style of interaction, 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 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 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.precise opposite, and in fact, perform significantly worse when distributed.
•Differing structural relationshipsDiffering structural relationshipsRelational data stores track entities in terms of relations between tuples and tuplesets; object-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 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 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 connections. Where databases use foreign-key relationships to indicate relations, objects use references or pointersreferences or pointers
3
OR Impedance OR Impedance MismatchMismatch•Differing identity constructsDiffering identity constructs
Object systems use an implicit sense of identity to distinguish between objects of similar state 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 (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 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 object system cannot be built without a sense of object identity, whereas relational tables can have no primary key whatsoever, if desired.have no primary key whatsoever, if desired.
•Transactional boundariesTransactional boundariesObject systems do not have any sense of "transactional demarcation" when working with the 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 objects, whereas database instances must in order to deal with the multi-user requirements of
a modern client/server-based systema modern client/server-based system..•Query/access capabilitiesQuery/access capabilities
Retrieving data stored in a relational database makes use of SQL, a declarative language 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, 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 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 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 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 principal access is intended to be distributed, as relational database are, this can be a crippling problem.crippling problem.
4
Object Relational Object Relational MappingMappingWikipedia defines an ORM as: “a programming defines an ORM as: “a programming
technique for converting data between incompatible technique for converting data between incompatible type systems in relational databases and object-type systems in relational databases and object-oriented programming languages. This creates, in oriented programming languages. This creates, in effect, a "virtual object database," which can be used effect, a "virtual object database," which can be used from within the programming language.” An ORM from within the programming language.” An ORM has to provide a facility to map database tables to has to provide a facility to map database tables to domain objects, using a design surface or wizard. domain objects, using a design surface or wizard. This mapping is in-between your database and This mapping is in-between your database and domain model, independent from the source code domain model, independent from the source code and the database. The ORM runtime then converts and the database. The ORM runtime then converts the commands issued by the domain model against the commands issued by the domain model against the mapping into back end database retrieval and the mapping into back end database retrieval and SQL statements. Mapping allows an application to SQL statements. Mapping allows an application to deal seamlessly with several different database deal seamlessly with several different database models, or even databases.models, or even databases.
The LINQ ProjectThe LINQ Project
StandardStandardQueryQuery
OperatorsOperators
ObjectsObjects
DLinqDLinq(ADO.NET)(ADO.NET)
XLinqXLinq(System.Xml)(System.Xml)
<book><book> <title/><title/> <author/><author/> <year/><year/> <price/><price/></book></book>
XMLXML
.NET Language Integrated Query.NET Language Integrated Query
C#C# VBVB Others…Others…
SQLSQL WinFWinFSS
SqlConnection c = new SqlConnection c = new SqlConnection(…);SqlConnection(…); c.Open(); c.Open(); SqlCommand cmd = new SqlCommand(SqlCommand cmd = new SqlCommand( @"SELECT c.Name, c.Phone@"SELECT c.Name, c.Phone
FROM Customers cFROM Customers c WHERE c.City = @p0"WHERE c.City = @p0"
);); cmd.Parameters.AddWithValue("@po", cmd.Parameters.AddWithValue("@po", "London"); "London"); DataReader dr = c.Execute(cmd); DataReader dr = c.Execute(cmd); while (dr.Read()) {while (dr.Read()) { string name = dr.GetString(0);string name = dr.GetString(0); string phone = dr.GetString(1);string phone = dr.GetString(1); DateTime date = DateTime date = dr.GetDateTime(2);dr.GetDateTime(2); }} dr.Close();dr.Close();
Data Access In APIs TodayData Access In APIs Today
Queries in Queries in quotesquotes
Arguments Arguments loosely loosely boundbound
Results Results loosely loosely typedtypedCompiler Compiler cannot cannot
help catch help catch mistakesmistakes
Data Access with DLINQData Access with DLINQ
public class Customerpublic class Customer{{ public int Id;public int Id; public string Name;public string Name; public string Phone;public string Phone; … …}}
Table<Customer> customers = Table<Customer> customers = db.Customers;db.Customers;
var contacts =var contacts = from c in customersfrom c in customers where c.City == "London"where c.City == "London" select new { c.Name, c.Phone };select new { c.Name, c.Phone };
Classes Classes describe describe
datadataTables are Tables are collectionscollections
Query is Query is natural natural
part of the part of the languagelanguageThe The compiler compiler helps you helps you
outout
public class Customer { public class Customer { …… } }
public class Northwind: public class Northwind: DataContextDataContext
{{ public Table<Customer> public Table<Customer>
Customers;Customers; ……
}}
Northwind db = new Northwind(Northwind db = new Northwind(……););var contacts =var contacts =
from c in db.Customersfrom c in db.Customers where c.City == "London"where c.City == "London"
select new { c.Name, c.Phone };select new { c.Name, c.Phone };
DLinq For Relational DataDLinq For Relational Data
AccessingAccessing data with DLinqdata with DLinqClasses Classes
describe datadescribe data
Strongly typed Strongly typed connectionconnection
Integrated Integrated query syntaxquery syntax
Strongly typed Strongly typed resultsresults
Tables are Tables are like collectionslike collections
ArchitectureArchitecture
LINQ QueryLINQ Query ObjectsObjects SubmitChangeSubmitChanges()s()
SQL QuerySQL Query RowsRows SQL or SQL or Stored Stored ProcsProcs
DLinqDLinq(ADO.NET)(ADO.NET)
SQLSerSQLServerver
from c in db.Customersfrom c in db.Customerswhere c.City == "London"where c.City == "London"selectselect new { c.Name, c.Phone } new { c.Name, c.Phone }
select Name, Phoneselect Name, Phonefrom customersfrom customerswhere city = 'London'where city = 'London'
ApplicationApplication
Services:Services:- Change tracking- Change tracking- Concurrency control- Concurrency control- Object identity- Object identity
Key TakeawaysKey Takeaways
Language integrated data accessLanguage integrated data access
Maps tables and rows to classes and objectsMaps tables and rows to classes and objects
Builds on ADO.NET and .NET TransactionsBuilds on ADO.NET and .NET Transactions
MappingMapping
Encoded in attributesEncoded in attributes
Relationships map to propertiesRelationships map to properties
Manually authored or tool generatedManually authored or tool generated
PersistencePersistence
Automatic change trackingAutomatic change tracking
Updates through SQL or stored proceduresUpdates through SQL or stored procedures
DataContextDataContext
Strongly typed databaseStrongly typed database
Querying For ObjectsQuerying For Objects
12
Key TakeawaysKey Takeaways
Language Integrated QueryLanguage Integrated QueryCompile-time type checking, IntelliSenseCompile-time type checking, IntelliSense
SQL-like query syntaxSQL-like query syntaxWith support for hierarchy and With support for hierarchy and relationshipsrelationships
Intelligent object loadingIntelligent object loadingDeferred or immediateDeferred or immediate
Updating ObjectsUpdating Objects
14
Key TakeawaysKey Takeaways
Auto-generated updatesAuto-generated updatesUsing optimistic concurrencyUsing optimistic concurrency
TransactionsTransactionsIntegrates with System.TransactionsIntegrates with System.Transactions
SQL pass-throughSQL pass-throughReturning objects from SQL queriesReturning objects from SQL queries
15
DLinq SummaryDLinq Summary
Allows access to relational data as Allows access to relational data as objectsobjects
Supports Language Integrated QuerySupports Language Integrated Query
Works with existing infrastructureWorks with existing infrastructure
Unifies programming model for Unifies programming model for objects, relational and XMLobjects, relational and XML
16
When to Use LINQ to When to Use LINQ to SQL?SQL?The primary scenario for using LINQ to SQL is when The primary scenario for using LINQ to SQL is when
building applications with a rapid development cycle building applications with a rapid development cycle and a simple one-to-one object to relational mapping and a simple one-to-one object to relational mapping against the Microsoft SQL Server family of databases. against the Microsoft SQL Server family of databases. In other words, when building an application whose In other words, when building an application whose object model is structured very similarly to the object model is structured very similarly to the existing database structure, or when a database for existing database structure, or when a database for the application does not yet exist and there is no the application does not yet exist and there is no predisposition against creating a database schema predisposition against creating a database schema that mirrors the object modelthat mirrors the object model
17
When to Use LINQ to When to Use LINQ to SQL?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