520 likes | 609 Views
LINQ i alla dess smaker. Johan Lindfors blogs.msdn.com/johanl Johan.Lindfors@microsoft.com. Patrik Löwendahl www.lovendahl.net Patrik.Lowendahl@cornerstone.se. Bakgrund och utmaningar. Message Object. Domain Model. XML. Data Transfer Object. Active Record. DataTable. DataSet.
E N D
LINQ i alla dess smaker Johan Lindfors blogs.msdn.com/johanlJohan.Lindfors@microsoft.com Patrik Löwendahl www.lovendahl.netPatrik.Lowendahl@cornerstone.se
Message Object Domain Model XML Data Transfer Object Active Record DataTable DataSet Objekts modell Repositories DataReader
DataSet DataTable DataTable
SQL Server 2005 SQL Server 6.5 Categories Products Customers Orders SqlDataAdapter OleDbDataAdapter DataSet Categories Products Employees Customers Orders XmlDataDocument XML Fil Web service
Fokus på applikation inte lagring Frikopplar affärslogik från datastrukturen Större möjligheter till återanvändning och utökningar
LINQ-projektet <book> <title/> <author/> <year/> <price/> </book> Relationer Objekt XML .NET Language Integrated Query C# 3.0 Visual Basic 9.0 Andra… LINQ toObjects LINQ toDataSets LINQ toSQL LINQ toEntities LINQ toXML
Nyheter i C# 3.0 “Query expressions” var contacts = from c in customers where c.State == "WA" select new { c.Name, c.Phone }; “Local variable type inference” “Lambda expressions” var contacts = customers .Where(c => c.State == "WA") .Select(c => new { c.Name, c.Phone }); “Extension methods” “Anonymous types” “Object initializers”
Designmål Direkt mappning mot SQL RAD ”Deferred Execution” Mål: Microsoft SQL Server RTM: Visual Studio 2008
Anpassning av affärslogiken Partiella klasser och metoder Lagrade procedurer Optimering Kompilerade frågor
Koncept vi skall beröra Mappning Att ställa frågor Updatera data Stored procedures Arkitektur
Conceptual Model Map Storage/Logical Model Entity Data Model Schema Datastore Objects Schema *.CSDL *.MSL *.SSDL Datastorage OO Classes
ObjectQuery<T> ObjectContext EntityDataReader EntityCommand EntityConnection EntityDataReader EntityCommand EntityConnection Database
EF Stored Procs & TVFs Entity SQL Queries Normalised Application Objects Views Database LINQ to Entities Queries ‘Optimised’ Dynamic SQL
LazyLoad Källa: www.martinfowler.com
Identitymap Källa: www.martinfowler.com
ObjectContext UnitOfWork
Functionmapping SSDL: <Function Name="TenMostExpensiveProducts" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false” ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo" /> CSDL: <FunctionImport Name="TenMostExpensiveProducts" EntitySet="Products" ReturnType="Collection(NorthwindEFModel.Products)" /> MDL: <FunctionImportMapping FunctionImportName="TenMostExpensiveProducts” FunctionName="NorthwindEFModel.Store.TenMostExpensiveProducts"/> Context: var query = context.TenMostExpensiveProducts();
Insert / Update / Delete SSDL: <Function Name="CreateCategory" IsComposable="false”> <Parameter Name="CategoryName" Type="nvarchar" /> </Function> <Function Name="EditCategory" IsComposable="false"> <Parameter Name="CategoryID" Type="int" /> <Parameter Name="CategoryName" Type="nvarchar" /> </Function> <Function Name="RemoveCategory" IsComposable="false"> <Parameter Name="CategoryID" Type="int" /> </Function> MDL: <ModificationFunctionMapping> <InsertFunction FunctionName="dbo.CreateCategory”> <ScalarProperty Name="CategoryName” parameterName="CategoryName" /> <ResultBinding ColumnName="CategoryID" Name="CategoryID"/> </InsertFunction> <UpdateFunction FunctionName="dbo.EditCategory”> <ScalarProperty Name="CategoryID” ParameterName="CategoryID” Version="current"/> <ScalarProperty Name="CategoryName” ParameterName="CategoryName” Version="current"/> </UpdateFunction> <DeleteFunction FunctionName="dbo.RemoveCategory”> <ScalarProperty Name="CategoryID” ParameterName="CategoryID"/> </DeleteFunction> </ModificationFunctionMapping>
SQL Server Relational DBMS Applications Programming Layers Code Gen • XML • XLinq • XQuery • Objects • Linq … Browsing Binding Entity Client – EDM, Entity SQL Domain Modeling Tools Metadata Services Query and Update Pipelines Mapping Mapping Entity Framework Runtime Transactions Modeling Data Providers (ADO.NET patterns) SqlClient OtherClient Non-relational Web Service
Läs mer… http://forums.msdn.com http://msdn.microsoft.com/data http://www.lowendahl.net http://blogs.msdn.com/adonet http://forum.cornerstone.se http://blogs.msdn.com/angelsb
”Disconnected cache” Otypade var query = from row in myDataSet.Tables["Customers"].AsEnumerable() where row .Field<string>("City") == "London" select new { row.Field <string> ("CustomerID"), row.Field <string> ("ContactName") } ; Typade var query = from customer in northwind.Customers where customer.City == "London" select customer;