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

4,085 views

Published on

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

Published in: Technology
  • Be the first to comment

  • 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: http://www.orm.net/ </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 + system.data.dataextension ) </li></ul><ul><li>LINQ to SQL- DLinq </li></ul><ul><li>Direct sql server ( system.data.linq ) </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 {System.data.entity} </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>http://blogs.msdn.com/adonet/archive/2007/01/26/querying-datasets-introduction-to-linq-to-dataset.aspx </li></ul><ul><li>http://msdn.microsoft.com/en-us/vbasic/bb688086.aspx </li></ul><ul><li>http://msdn.microsoft.com/en-us/library/bb386977.aspx </li></ul><ul><li>http://www.danielmoth.com/Blog/2007/07/linq-to-dataset.html </li></ul><ul><li>http://channel9.msdn.com/posts/DanielMoth/LINQ-to-DataSet/ </li></ul><ul><li>[LINQ to entities] </li></ul><ul><li>http://msdn.microsoft.com/en-us/library/bb386964.aspx </li></ul><ul><li>http://dotnetaddict.dotnetdevelopersjournal.com/adoef_vs_linqsql.htm </li></ul><ul><li>http://blogs.msdn.com/kaevans/archive/2008/01/29/linq-to-sql-vs-linq-to-entities-smackdown.aspx </li></ul><ul><li>http://msdn.microsoft.com/en-us/library/cc161164.aspx </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>http://msdn.microsoft.com/en-us/library/cc161164.aspx </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>http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/05/10/9322.aspx </li></ul><ul><li>http://jonkruger.com/blog/2008/02/10/linq-to-sql-in-disconnectedn-tier-scenarios-saving-an-object/ </li></ul><ul><li>http://andrewtokeley.net/archive/2008/07/06/mocking-linq-to-sql-datacontext.aspx </li></ul><ul><li>http://devlicio.us/blogs/derik_whittaker/archive/2008/05/21/using-linq-s-datacontext-to-re-create-your-database-for-testing.aspx </li></ul><ul><li>http://www.west-wind.com/weblog/posts/246222.aspx </li></ul><ul><li>http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx </li></ul><ul><li>http://msdn.microsoft.com/en-us/library/bb882661.aspx </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>http://blogs.msdn.com/charlie/archive/2008/01/31/expression-tree-basics.aspx </li></ul><ul><li>http://blogs.msdn.com/charlie/archive/2008/02/13/linq-farm-seed-using-the-expression-tree-visualizer.aspx </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>http://code.msdn.microsoft.com/vlinq </li></ul><ul><li>[Demo] </li></ul><ul><li>LINQPad </li></ul><ul><ul><li>http://www.linqpad.net/ </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>http://blogs.msdn.com/ricom/archive/2007/06/22/dlinq-linq-to-sql-performance-part-1.aspx </li></ul><ul><li>http://blogs.msdn.com/ricom/archive/2007/06/22/dlinq-linq-to-sql-performance-part-1.aspx </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>http://alexpinsker.blogspot.com/2007/07/benchmarking-linq-vs.html </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>

×