1. Trang chủ
  2. » Tất cả

Chapter 7 - Beyond LINQ

28 368 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 28
Dung lượng 907,45 KB

Nội dung

LINQ via C# 3.0 Chapter – Beyond LINQ © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel What’s Beyond? • Very near: • http://tinyurl.com/4xupp8 – ADO.NET Entity Framework (VS2008 SP1 Beta1) • http://tinyurl.com/5e959q – ADO.NET Data Services (VS2008 SP1 Beta1) • http://tinyurl.com/6kb45k, http://astoria.mslivelabs.com • Quite near: – Parallel LINQ (June08 CTP) • http://tinyurl.com/6ztng6, http://tinyurl.com/5r8c77 ã LINQ to Your Own â Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Parallel LINQ • Part of the Parallel Extensions for NET – System.Threading.dll assembly – Adds AsParallel() extension method to IEnumerable – Currently supports LINQ to Objects only from n in Enumerable.Range( ).AsParallel() where IsPrime(n) select n © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Parallel LINQ Notes • Current CTP is stable but there are still performance issues • Parallelizing tiny inputs harms performance • Can specify degree of parallelism to AsParallel() • Can specify order preservation (pipeline processing) © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel ADO.NET Entity Framework • Advanced Object-Relational Mapper • Supports three-tiered mapping: – Business entities – Conceptual model – Relational data • More flexible than LINQ to SQL: – – – – Single class  multiple tables Many-to-many relationships Arbitrary queries (Entity-SQL) Will support providers other than SQL Serverđ â Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Designer Support • Modeling very similar to LINQ to SQL – Can work objects-first or schema-first © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Generated Classes public partial class SalesEntities : ObjectContext { public ObjectQuery Sales } [EdmEntityType( , Name=“SaleInfo”)] [DataContract(IsReference = true), Serializable] public partial class SaleInfo : EntityObject { [EdmScalarProperty(EntityKeyProperty=true, )] [DataMember] public int Id } © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Using LINQ to Entities using (SalesEntities context = new SalesEntities()) { var everyone = context.CreateQuery( "SELECT VALUE sp FROM SalesPeople AS sp"); foreach (var p in everyone) Console.WriteLine(p.Name + " " + p.Seniority); var query = Entity-SQL from sale in context.Sales where sale.Amount > group sale by sale.SalesPerson.Name into g select new { Name = g.Key, Amount = g.Sum(s => s.Amount) }; foreach (var info in query.OrderByDescending(i => i.Amount)) Console.WriteLine(info.Name + ": " + info.Amount); } © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel More Entity-SQL • Navigating relationships: var highSales = context.CreateQuery( @"SELECT VALUE sp FROM SalesPeople AS sp WHERE EXISTS ( SELECT VALUE s FROM NAVIGATE(sp, SM.SalesPeople_Sales) AS s WHERE s.Amount > 50 )"); foreach (var p in highSales) Console.WriteLine(p.Name + " " + p.Sales.Sum(s => s.Amount)); © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Entity Framework vs LINQ to SQL • LINQ to SQL is a full ORM – It’s not just a RAD drag-and-drop tool – Moderate learning curve • Entity Framework is fuller – Steep learning curve • Most projects can use LINQ to SQL – Discussion: http://tinyurl.com/5wa5qk, http://tinyurl.com/573jo8, http://tinyurl.com/6dxeej © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Out Of The Box © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Out Of The Box © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Out Of The Box © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Service Operations [WebGet] public IQueryable TopSales(int n) { return (from sale in this.CurrentDataSource.Sales orderby sale.Amount descending select sale).Take(n); } • Operations • Change interceptors (entities change) • Query interceptors (customizing query information) © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Service Operations [WebGet] public IQueryable TopSales(int n) { return (from sale in this.CurrentDataSource.Sales orderby sale.Amount descending select sale).Take(n); } © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Client Code • Non-browser clients should use System.Data.Services.Client.dll – And the companion DataSvcUtil.exe tool – (Works with a conceptual model directly as well!) • It’s WCF! – Can work with shared data contracts – Can generate only the contracts we need – WCF SP1 features support for POCO (don’t need [DataContract] or [DataMember] on anything) – Can work with DataSvcUtil-generated classes © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Client Code – Manual class SaleInfo { //POCO public int Id { get; set; } //Plain Old CLR Object public int Amount { get; set; } } class DataServicesClient { static void Main(string[] args) { DataServiceContext context = new DataServiceContext(new Uri(“ /SalesData.svc")); var sales = context.Execute( new Uri(“ /SalesDataService.svc/Sales")); foreach (SaleInfo sale in sales) Console.WriteLine(sale.Id + " " + sale.Amount); } } © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Client Code – Generated public partial class SalesEntities : DataServiceContext { public DataServiceQuery SalesPeople } [DataServiceKey(“Name”)] public partial class SalesPerson { public Collection Sales } [DataServiceKey(“Id”)] public partial class SaleInfo { } © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Client Code – Generated SalesEntities context2 = new SalesEntities( new Uri("http://localhost:1034/SalesDataService.svc")); var query = from sp in context2.SalesPeople orderby sp.Seniority descending select sp; foreach (var d in query) Console.WriteLine(d.Name + " " + d.Commission); • The query is effectively “serialized” • A small subset of query operators is supported • Most work has to be done on the client © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Data Services Over LINQ to SQL • Any DataContext-assignable class can be used in a DataService • Table does not implement IUpdatable – This means that insert, delete and update operations over the client context will fail • You can implement IUpdatable yourself! – http://tinyurl.com/5ranan © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Data Services – Quirks with LINQ to SQL • Many-to-many association tables (with two foreign keys) don’t work by default – A primary key is required – Resolve with [DataServiceKey(“FK1”, “FK2”)] on the generated class (partial) • Primary keys not called Id or TableNameId don’t work by default – The [DataServiceKey] attribute is required © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Custom LINQ Providers • Simple LINQ providers can implement IEnumerable or the query pattern • Advanced providers require IQueryable – Parsing the query expression tree – Translating it to SQL (e.g LINQ to Oracle), web service calls (e.g LINQ to Amazon), other system APIs (e.g LINQ to WMI) – List of providers: http://tinyurl.com/yt8ala © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Some Custom LINQ Providers LINQ to Amazon LINQ to Active Directory® LINQ to Google LINQ to Flickr LINQ to NHibernate LINQ to Microsoft SharePointđ ã LINQ to Microsoft Office Excelđ • LINQ to… Your Own! • • • • • • © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Chapter Summary • ADO.NET Entity Framework • ADO.NET Data Services • Parallel LINQ (June08 CTP) ã Custom LINQ Providers â Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel Resources • LINQ in Action – http://linqinaction.net • C# 3.0 Language Specification – http://tinyurl.com/baoh8 © Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel ... Some Custom LINQ Providers LINQ to Amazon LINQ to Active Directory® LINQ to Google LINQ to Flickr LINQ to NHibernate LINQ to Microsoft SharePointđ ã LINQ to Microsoft Office Excelđ • LINQ to… Your... 1 4-1 8 Baruch Hirsch St Bnei Brak 51202 Israel Designer Support • Modeling very similar to LINQ to SQL – Can work objects-first or schema-first © Copyright SELA Software & Education Labs Ltd 1 4-1 8... Software & Education Labs Ltd 1 4-1 8 Baruch Hirsch St Bnei Brak 51202 Israel Entity Framework vs LINQ to SQL • LINQ to SQL is a full ORM – It’s not just a RAD drag-and-drop tool – Moderate learning

Ngày đăng: 12/01/2013, 16:18

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w