Top Banner
LINQ 2 SQL LINQ 2 SQL By, Shahzad Sarwar
34

LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

Mar 26, 2015

Download

Documents

Kylie Elliott
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

LINQ 2 SQLLINQ 2 SQL By, Shahzad Sarwar

Page 2: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

What you should know, before startingWhat you should know, before starting

• Anonymous function• Anonymous type• Anonymous Methods• Lamda expression• Collection Initializers• Object Initializers• Extension methods• Type Inference• Automatic properties• Partial Methods• Generic• Projection (Later)• Expression Tree (Later)

Page 3: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

• What is the problem?

Class!=Data• 1. strongly-typed queries , what ?• 2. strongly-typed results• Benefits

1. IntelliSense

2. Compile-time error checking.

Page 4: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

• What is Linq to SQL?1.OR Mapper API

2.Syntax to query ( LINQ to objects )

3.Tools

• What r OR Mappers(OR/M)? • Not ORM?• What is Object Role Modeling (ORM)?

• Associated language FORML (Formal Object-Role Modeling Language)

• Supported by Visio and Microsoft Architect

• References: http://www.orm.net/

Page 5: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

Current Solutions in marketCurrent Solutions in market• .NET Persistence • BBADataObjects • DataObjects.NET • Data Tier Modeler for .NET • DotNorm • Eldorado.NET • Enterprise Core Objects (ECO™) • Entity Broker • eXpress Persistent Objects for .NET • FastObjects.NET • JC Persistent Framework • LLBLGen Pro • ModelWorks • Nhibernate • Nolics.NET

Page 6: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

Current Solutions in marketCurrent Solutions in market• Norm • Norpheme • ObjectBroker • ObjectSpaces • ObjectSpark • Objectz.NET • OJB.NET • OPF.Net (Object Persistent Framework) • ORM.NET • Pragmatier Data Tier Builder • RapTier • Sisyphus Persistence Framework • TierDeveloper • Bob.NET • ObjectPersistor.NET • Genome

Page 7: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

A view of LINQA view of LINQLINQ Consist of :• Total 6 LINQ implementations • 3 general LINQ technologies • 2 related to relational databases (OR/M)• 1 Future Technologies

• LINQ to Objects Over objects , system.linq

• LINQ to Dataset over dataset ( LINQ to objects + system.data.dataextension )

• LINQ to SQL- DLinq Direct sql server ( system.data.linq )

Page 8: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

• LINQ to entities • entities framework ( a part of the ADO.NET Entity Framework which

allows LINQ query capabilities) • The Entity Data Model (EDM) is a conceptual data

{System.data.entity}

• LINQ to XML = xlinq • The low-level XmlReader/XmlWriter API in .NET today.

{System.Xml.Linq}

• PLINQQuery execution engine accepts any LINQ-to-Objects or LINQ-to-XML query and automatically utilizes multiple processors or cores for execution when they are available.

Page 9: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

Conceptual View Conceptual View (LINQ)(LINQ)

Page 10: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

Conceptual View Conceptual View (LINQ to Dataset)(LINQ to Dataset)

Page 11: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

Conceptual View Conceptual View (LINQ 2 entities)(LINQ 2 entities)

Page 12: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

Conceptual View Conceptual View ( PLINQ)( PLINQ)

Page 13: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

ReferencesReferences

[LINQ to Dataset]• http://blogs.msdn.com/adonet/archive/2007/01/26/querying-datasets-

introduction-to-linq-to-dataset.aspx• http://msdn.microsoft.com/en-us/vbasic/bb688086.aspx• http://msdn.microsoft.com/en-us/library/bb386977.aspx• http://www.danielmoth.com/Blog/2007/07/linq-to-dataset.html• http://channel9.msdn.com/posts/DanielMoth/LINQ-to-DataSet/

[LINQ to entities]• http://msdn.microsoft.com/en-us/library/bb386964.aspx• http://dotnetaddict.dotnetdevelopersjournal.com/adoef_vs_linqsql.htm• http://blogs.msdn.com/kaevans/archive/2008/01/29/linq-to-sql-vs-linq-to-

entities-smackdown.aspx• http://msdn.microsoft.com/en-us/library/cc161164.aspx

Page 14: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

What is difference LINQ to SQ L and LINQ to Entities?

• 1:1 vs many to many , complex mapping • sql only , any database .... so run in database specfic pattern.

http://msdn.microsoft.com/en-us/library/cc161164.aspx

• Sample Application on the fly• Sample Application:

Sample Add , delete , edit

Example

What next ??????????????????????????? It is over…

Page 15: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

DataContextDataContext• Connection like class (Mixture of a lot of objects)• Caching n change tracking• Example

• Log• ExecuteCommand• GetTable• ExecuteQuery• Refresh• ObjectTrackingEnabled• Transaction

• Cases:• Nested Query• Cache Query / Multiple context• ToList()

• What is Projection?

Page 16: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

Context ManagementContext Management

• Four Approaches1. Create a new Context for each atomic operation (Application level

management) Problem: pass data objects or the data context itself around to other components in your

application.E.g.: if you use business objects there's no real clean way to get a context passed between business objects.

2.Create a global DataContext and handle all operations against this single DataContext object

Problem:In other words if you have multiple simulataneous data operations that might not be

directly linked/atomicNot possible to do simple update.....?and Options such ObjectTrackingEnabled or DeferredLoadingEnabled can't be set

easily Works for desktop , not for Web because of threading ........

Page 17: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

Context ManagementContext Management

3.Create a thread specific DataContext

Request specific DataContext that is tied to the active thread,Web request to the active ASP.NET HttpContext.

Example

Factory Pattern

4. Create a per business object DataContext

Page 18: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

Architecture Guide LinesArchitecture Guide Lines

References:• http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/

archive/2007/05/10/9322.aspx• http://jonkruger.com/blog/2008/02/10/linq-to-sql-in-

disconnectedn-tier-scenarios-saving-an-object/• http://andrewtokeley.net/archive/2008/07/06/mocking-linq-to-sql-

datacontext.aspx• http://devlicio.us/blogs/derik_whittaker/archive/2008/05/21/using-

linq-s-datacontext-to-re-create-your-database-for-testing.aspx• http://www.west-wind.com/weblog/posts/246222.aspx• http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-

framework-design-guidelines.aspx• http://msdn.microsoft.com/en-us/library/bb882661.aspx

Page 19: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

Code GenerationCode Generation

• 2 Options • VS Studio OR Designer• SQLMetal

• Code Understanding • Explore

• XML Mapping file• Generated classes

dataConext ,Database, Table, relationshipsExample

Page 20: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

Query pipelineQuery pipeline• LINQ 2 Objects = IEnumerable • LINQ 2 SQL= IQueryable

public interface IQueryable : IEnumerable { Type ElementType { get; } Expression Expression { get; } IQueryProvider Provider { get; } }

• What is Expression Tree?a method of translating executable code into data.

• Add-ons:• Query Visualizer• Expression Tree Visualizer• Example

• Out of processor , Grammar , sql processor• Various Stages ( Out of scope )• 1-Many mapping b/q LINQ grammer to db sql References:• http://blogs.msdn.com/charlie/archive/2008/01/31/expression-tree-basics.aspx• http://blogs.msdn.com/charlie/archive/2008/02/13/linq-farm-seed-using-the-

expression-tree-visualizer.aspx

Page 21: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

Joins n Lazy Loading Joins n Lazy Loading

• Types of Joins• Inner Join

• Left Outer Join

• Right Outer Join

• Cross Join

• Example• LINQ implementation of Joins• Deferred Loading of objects• DataLoadOptions/DeferredLoadingEnabled property• AssociateWith Property

Page 22: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

LINQDataSourceLINQDataSource

• Like sqlDataSource• Example 1 (Wizard View , declarative View )

• Overriding events• Selecting event• Example 2

Page 23: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

Using Store ProcedureUsing Store Procedure

• Add Store procedures to Model• Example • Add function to Models• Example• Difference b/w Store Procedure n function

• for computations , for Business logic

• Functions can be called inside select/update/delete stmt but not the procedures.

Page 24: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

LINQ2SQL CustomizationLINQ2SQL Customization

• By Store Procedure• Example• Object validation (Partial classes)• Example• Customize entity class ( 1 to m mapping )• Example

Page 25: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

InheritanceInheritance• In OR Mapping, 3 Type• Example

Page 26: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

Table per Class HierarchyTable per Class Hierarchy

[Example]

Page 27: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

Table per SubclassTable per Subclass

Page 28: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

Table per Concrete ClassTable per Concrete Class

Page 29: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

LINQ Dev. ToolsLINQ Dev. Tools

• Visual LINQ Query Builder through a friendly interface

http://code.msdn.microsoft.com/vlinq

[Demo]

• LINQPad http://www.linqpad.net/

Development tool

[Demo]

Page 30: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

PerformancePerformance• Benchmark 1

References• http://blogs.msdn.com/ricom/archive/2007/06/22/dlinq-linq-to-sql-

performance-part-1.aspx• http://blogs.msdn.com/ricom/archive/2007/06/22/dlinq-linq-to-sql-

performance-part-1.aspx

Page 31: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

PerformancePerformance

• Adventure Db , Store procedure

• So, comparing to the raw ADO.NET - DAAB is 8% slower and LINQ is 28% slower.Comparing to DAAB - LINQ is 18% slower.

Reference:• http://alexpinsker.blogspot.com/2007/07/benchmarking-linq-

vs.html

Page 32: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

PerformancePerformance

• But consider• Productivity• SQL being first class language in VS• Caching of DB.• Change tracking optimization

So Over a Great Milestone in Development process

Page 33: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.

What is Left………………..What is Left………………..

• Design Pattern for LINQ to SQL• XLINQ• LINQ to Dataset• LINQ to entities• PLINQ• n…………………….• …..• …………..• ………………..• So Wait for Next presentation on LINQ …………………

Page 34: LINQ 2 SQL By, Shahzad Sarwar. What you should know, before starting Anonymous function Anonymous type Anonymous Methods Lamda expression Collection Initializers.