4. 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.
5. The LINQ Project Standard Query Operators DLinq (ADO.NET) XLinq (System.Xml) .NET Language Integrated Query C# VB Others… Objects <book> <title/> <author/> <year/> <price/> </book> XML SQL WinFS
6. Data Access In APIs Today SqlConnection c = new SqlConnection(…); c.Open(); SqlCommand cmd = new SqlCommand( @"SELECT c.Name, c.Phone FROM Customers c WHERE c.City = @p0" ); cmd.Parameters.AddWithValue("@po", "London"); DataReader dr = c.Execute(cmd); while (dr.Read()) { string name = dr.GetString(0); string phone = dr.GetString(1); DateTime date = dr.GetDateTime(2); } dr.Close(); Queries in quotes Arguments loosely bound Results loosely typed Compiler cannot help catch mistakes
7. Data Access with DLINQ public class Customer { public int Id; public string Name; public string Phone; … } Table<Customer> customers = db.Customers; var contacts = from c in customers where c.City == "London" select new { c.Name, c.Phone }; Classes describe data Tables are collections Query is natural part of the language The compiler helps you out
8. DLinq For Relational Data public class Customer { … } public class Northwind: DataContext { public Table<Customer> Customers; … } Northwind db = new Northwind( … ); var contacts = from c in db.Customers where c.City == "London" select new { c.Name, c.Phone }; Accessing data with DLinq Classes describe data Strongly typed connection Integrated query syntax Strongly typed results Tables are like collections
9. Architecture LINQ Query Objects SubmitChanges() SQL Query from c in db.Customers where c.City == "London" select new { c.Name, c.Phone } select Name, Phone from customers where city = 'London' Rows SQL or Stored Procs DLinq (ADO.NET) SQLServer Application Services: - Change tracking - Concurrency control - Object identity
16. 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
17. 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