Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

LINQ 2 SQL Presentation To Palmchip And Trg, Technology Resource Group


Published on

Agenda Linq 2 Sql
To: Development Team
By: Shahzad Sarwar
Date: 3rd March 2009

Published in: Technology
  • Login to see the comments

  • Be the first to like this

LINQ 2 SQL Presentation To Palmchip And Trg, Technology Resource Group

  1. 1. LINQ 2 SQL By, Shahzad Sarwar
  2. 2. What you should know, before starting <ul><li>Anonymous function </li></ul><ul><li>Anonymous type </li></ul><ul><li>Anonymous Methods </li></ul><ul><li>Lamda expression </li></ul><ul><li>Collection Initializers </li></ul><ul><li>Object Initializers </li></ul><ul><li>Extension methods </li></ul><ul><li>Type Inference </li></ul><ul><li>Automatic properties </li></ul><ul><li>Partial Methods </li></ul><ul><li>Generic </li></ul><ul><li>Projection (Later) </li></ul><ul><li>Expression Tree (Later) </li></ul>
  3. 3. <ul><li>What is the problem? </li></ul><ul><li>Class!=Data </li></ul><ul><li>1. strongly-typed queries , what ? </li></ul><ul><li>2. strongly-typed results </li></ul><ul><li>Benefits </li></ul><ul><li>1. IntelliSense </li></ul><ul><li>2. Compile-time error checking. </li></ul>
  4. 4. <ul><li>What is Linq to SQL? </li></ul><ul><li>1.OR Mapper API </li></ul><ul><li>2.Syntax to query ( LINQ to objects ) </li></ul><ul><li>3.Tools </li></ul><ul><li>What r OR Mappers(OR/M)? </li></ul><ul><li>Not ORM? </li></ul><ul><li>What is Object Role Modeling (ORM)? </li></ul><ul><ul><li>Associated language FORML (Formal Object-Role Modeling Language) </li></ul></ul><ul><ul><li>Supported by Visio and Microsoft Architect </li></ul></ul><ul><ul><li>References: </li></ul></ul>
  5. 5. Current Solutions in market <ul><li>.NET Persistence </li></ul><ul><li>BBADataObjects </li></ul><ul><li>DataObjects.NET </li></ul><ul><li>Data Tier Modeler for .NET </li></ul><ul><li>DotNorm </li></ul><ul><li>Eldorado.NET </li></ul><ul><li>Enterprise Core Objects (ECO™) </li></ul><ul><li>Entity Broker </li></ul><ul><li>eXpress Persistent Objects for .NET </li></ul><ul><li>FastObjects.NET </li></ul><ul><li>JC Persistent Framework </li></ul><ul><li>LLBLGen Pro </li></ul><ul><li>ModelWorks </li></ul><ul><li>Nhibernate </li></ul><ul><li>Nolics.NET </li></ul>
  6. 6. Current Solutions in market <ul><li>Norm </li></ul><ul><li>Norpheme </li></ul><ul><li>ObjectBroker </li></ul><ul><li>ObjectSpaces </li></ul><ul><li>ObjectSpark </li></ul><ul><li>Objectz.NET </li></ul><ul><li>OJB.NET </li></ul><ul><li>OPF.Net (Object Persistent Framework) </li></ul><ul><li>ORM.NET </li></ul><ul><li>Pragmatier Data Tier Builder </li></ul><ul><li>RapTier </li></ul><ul><li>Sisyphus Persistence Framework </li></ul><ul><li>TierDeveloper </li></ul><ul><li>Bob.NET </li></ul><ul><li>ObjectPersistor.NET </li></ul><ul><li>Genome </li></ul>
  7. 7. A view of LINQ <ul><li>LINQ Consist of : </li></ul><ul><li>Total 6 LINQ implementations </li></ul><ul><li>3 general LINQ technologies </li></ul><ul><li>2 related to relational databases (OR/M) </li></ul><ul><li>1 Future Technologies </li></ul><ul><li>LINQ to Objects </li></ul><ul><li>Over objects , system.linq </li></ul><ul><li>LINQ to Dataset </li></ul><ul><li>over dataset ( LINQ to objects + ) </li></ul><ul><li>LINQ to SQL- DLinq </li></ul><ul><li>Direct sql server ( ) </li></ul>
  8. 8. <ul><li>LINQ to entities </li></ul><ul><li>entities framework ( a part of the ADO.NET Entity Framework which allows LINQ query capabilities) </li></ul><ul><li>The Entity Data Model (EDM) is a conceptual data {} </li></ul><ul><li>LINQ to XML = xlinq </li></ul><ul><li>The low-level XmlReader/XmlWriter API in .NET today. {System.Xml.Linq} </li></ul><ul><li>PLINQ </li></ul><ul><li>Query 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. </li></ul>
  9. 9. Conceptual View (LINQ)
  10. 10. Conceptual View (LINQ to Dataset)
  11. 11. Conceptual View (LINQ 2 entities)
  12. 12. Conceptual View ( PLINQ)
  13. 13. References <ul><li>[LINQ to Dataset] </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li>[LINQ to entities] </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul>
  14. 14. <ul><li>What is difference LINQ to SQ L and LINQ to Entities? </li></ul><ul><li>1:1 vs many to many , complex mapping </li></ul><ul><li>sql only , any database .... so run in database specfic pattern. </li></ul><ul><li> </li></ul><ul><li>Sample Application on the fly </li></ul><ul><li>Sample Application: </li></ul><ul><li>Sample Add , delete , edit </li></ul><ul><li>Example </li></ul><ul><li>What next ??????????????????????????? It is over… </li></ul>
  15. 15. DataContext <ul><li>Connection like class (Mixture of a lot of objects) </li></ul><ul><li>Caching n change tracking </li></ul><ul><li>Example </li></ul><ul><ul><li>Log </li></ul></ul><ul><ul><li>ExecuteCommand </li></ul></ul><ul><ul><li>GetTable </li></ul></ul><ul><ul><li>ExecuteQuery </li></ul></ul><ul><ul><li>Refresh </li></ul></ul><ul><ul><li>ObjectTrackingEnabled </li></ul></ul><ul><ul><li>Transaction </li></ul></ul><ul><li>Cases: </li></ul><ul><ul><li>Nested Query </li></ul></ul><ul><ul><li>Cache Query / Multiple context </li></ul></ul><ul><ul><li>ToList() </li></ul></ul><ul><li>What is Projection? </li></ul>
  16. 16. Context Management <ul><li>Four Approaches </li></ul><ul><ul><li>1. Create a new Context for each atomic operation (Application level management) </li></ul></ul><ul><ul><li>Problem: pass data objects or the data context itself around to other components in your application. </li></ul></ul><ul><ul><ul><li>E.g.: if you use business objects there's no real clean way to get a context passed between business objects. </li></ul></ul></ul><ul><ul><li>2.Create a global DataContext and handle all operations against this single DataContext object </li></ul></ul><ul><ul><li>Problem: </li></ul></ul><ul><ul><li>In other words if you have multiple simulataneous data operations that might not be directly linked/atomic </li></ul></ul><ul><ul><li>Not possible to do simple update.....? </li></ul></ul><ul><ul><li>and Options such ObjectTrackingEnabled or DeferredLoadingEnabled can't be set easily </li></ul></ul><ul><ul><li>Works for desktop , not for Web because of threading ........ </li></ul></ul>
  17. 17. Context Management <ul><li>3.Create a thread specific DataContext </li></ul><ul><li>Request specific DataContext that is tied to the active thread,Web request to the active ASP.NET HttpContext. </li></ul><ul><li>Example </li></ul><ul><li>Factory Pattern </li></ul><ul><li>4. Create a per business object DataContext </li></ul>
  18. 18. Architecture Guide Lines <ul><li>References: </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul>
  19. 19. Code Generation <ul><li>2 Options </li></ul><ul><ul><li>VS Studio OR Designer </li></ul></ul><ul><ul><li>SQLMetal </li></ul></ul><ul><li>Code Understanding </li></ul><ul><li>Explore </li></ul><ul><ul><li>XML Mapping file </li></ul></ul><ul><ul><li>Generated classes </li></ul></ul><ul><li>dataConext ,Database, Table, relationships </li></ul><ul><li>Example </li></ul>
  20. 20. Query pipeline <ul><li>LINQ 2 Objects = IEnumerable </li></ul><ul><li>LINQ 2 SQL= IQueryable </li></ul><ul><ul><li>public interface IQueryable : IEnumerable </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>Type ElementType { get; } </li></ul></ul><ul><ul><li>Expression Expression { get; } </li></ul></ul><ul><ul><li>IQueryProvider Provider { get; } </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>What is Expression Tree? </li></ul><ul><li>a method of translating executable code into data. </li></ul><ul><li>Add-ons: </li></ul><ul><ul><ul><li>Query Visualizer </li></ul></ul></ul><ul><ul><ul><li>Expression Tree Visualizer </li></ul></ul></ul><ul><ul><ul><li>Example </li></ul></ul></ul><ul><li>Out of processor , Grammar , sql processor </li></ul><ul><li>Various Stages ( Out of scope ) </li></ul><ul><li>1-Many mapping b/q LINQ grammer to db sql </li></ul><ul><li>References: </li></ul><ul><li> </li></ul><ul><li> </li></ul>
  21. 21. Joins n Lazy Loading <ul><li>Types of Joins </li></ul><ul><ul><li>Inner Join </li></ul></ul><ul><ul><li>Left Outer Join </li></ul></ul><ul><ul><li>Right Outer Join </li></ul></ul><ul><ul><li>Cross Join </li></ul></ul><ul><li>Example </li></ul><ul><li>LINQ implementation of Joins </li></ul><ul><li>Deferred Loading of objects </li></ul><ul><li>DataLoadOptions / DeferredLoadingEnabled property </li></ul><ul><li>AssociateWith Property </li></ul>
  22. 22. LINQDataSource <ul><li>Like sqlDataSource </li></ul><ul><li>Example 1 (Wizard View , declarative View ) </li></ul><ul><li>Overriding events </li></ul><ul><li>Selecting event </li></ul><ul><li>Example 2 </li></ul>
  23. 23. Using Store Procedure <ul><li>Add Store procedures to Model </li></ul><ul><li>Example </li></ul><ul><li>Add function to Models </li></ul><ul><li>Example </li></ul><ul><li>Difference b/w Store Procedure n function </li></ul><ul><ul><li>for computations , for Business logic </li></ul></ul><ul><ul><li>Functions can be called inside select/update/delete stmt but not the procedures. </li></ul></ul>
  24. 24. LINQ2SQL Customization <ul><li>By Store Procedure </li></ul><ul><li>Example </li></ul><ul><li>Object validation (Partial classes) </li></ul><ul><li>Example </li></ul><ul><li>Customize entity class ( 1 to m mapping ) </li></ul><ul><li>Example </li></ul>
  25. 25. Inheritance <ul><li>In OR Mapping, 3 Type </li></ul><ul><li>Example </li></ul>
  26. 26. Table per Class Hierarchy <ul><li>[Example] </li></ul>
  27. 27. Table per Subclass
  28. 28. Table per Concrete Class
  29. 29. LINQ Dev. Tools <ul><li>Visual LINQ Query Builder </li></ul><ul><li>through a friendly interface </li></ul><ul><li> </li></ul><ul><li>[Demo] </li></ul><ul><li>LINQPad </li></ul><ul><ul><li> </li></ul></ul><ul><ul><li>Development tool </li></ul></ul><ul><li>[Demo] </li></ul>
  30. 30. Performance <ul><li>Benchmark 1 </li></ul><ul><li>References </li></ul><ul><li> </li></ul><ul><li> </li></ul>
  31. 31. Performance <ul><li>Adventure Db , Store procedure </li></ul><ul><li>So, comparing to the raw ADO.NET - DAAB is 8% slower and LINQ is 28% slower. Comparing to DAAB - LINQ is 18% slower. </li></ul><ul><li>Reference: </li></ul><ul><li> </li></ul>
  32. 32. Performance <ul><li>But consider </li></ul><ul><ul><li>Productivity </li></ul></ul><ul><ul><li>SQL being first class language in VS </li></ul></ul><ul><ul><li>Caching of DB. </li></ul></ul><ul><ul><li>Change tracking optimization </li></ul></ul><ul><ul><li>So Over a Great Milestone in Development process </li></ul></ul>
  33. 33. What is Left……………….. <ul><li>Design Pattern for LINQ to SQL </li></ul><ul><li>XLINQ </li></ul><ul><li>LINQ to Dataset </li></ul><ul><li>LINQ to entities </li></ul><ul><li>PLINQ </li></ul><ul><li>n……………………. </li></ul><ul><li>… .. </li></ul><ul><li>………… .. </li></ul><ul><li>……………… .. </li></ul><ul><li>So Wait for Next presentation on LINQ ………………… </li></ul>