13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

346 CHAPTER 12 LINQ beyond collectionsfrom anyth<strong>in</strong>g else, this means that the feature of RDBMS <strong>in</strong>dependence comes forfree. The same LINQ queries can be run aga<strong>in</strong>st SQL Server, Oracle, or Postgres, forexample: any system that NHibernate knows about, with SQL tailored for that particularimplementation.Before writ<strong>in</strong>g this book, I hadn’t used NHibernate (although I am reasonablyexperienced with its cous<strong>in</strong> <strong>in</strong> the Java world), and it’s a testament to the project thatwith<strong>in</strong> about an hour I was up and runn<strong>in</strong>g with the SkeetySoft defect database, us<strong>in</strong>gnoth<strong>in</strong>g but the onl<strong>in</strong>e tutorial. List<strong>in</strong>g 12.20 shows the same query we used aga<strong>in</strong>stLINQ to SQL <strong>in</strong> list<strong>in</strong>g 12.3 to list all of Tim’s open defects.List<strong>in</strong>g 12.20LINQ to NHibernate query to list defects assigned to Tim TrotterISessionFactory sessionFactory =new Configuration().Configure().BuildSessionFactory();us<strong>in</strong>g (ISession session = sessionFactory.OpenSession()){us<strong>in</strong>g (ITransaction tx = session.Beg<strong>in</strong>Transaction()){User tim = (from user <strong>in</strong> session.L<strong>in</strong>q()where user.Name == "Tim Trotter"select user).S<strong>in</strong>gle();}}var query = from defect <strong>in</strong> session.L<strong>in</strong>q()where defect.Status != Status.Closedwhere defect.AssignedTo == timselect defect.Summary;foreach (var summary <strong>in</strong> query){Console.WriteL<strong>in</strong>e(summary);}tx.Commit();As you can see, once the session and transaction have been set up, the code is similarto that used <strong>in</strong> LINQ to SQL. The generated SQL is different, although it executes thesame sort of queries. In other cases, identical query expressions can generate differentSQL, mostly due to decisions regard<strong>in</strong>g the lazy or eager load<strong>in</strong>g of entities. This is anexample of a leaky abstraction 7 —where <strong>in</strong> theory the abstraction layer of LINQ mightbe considered to isolate the developer from the implementation perform<strong>in</strong>g theactual query, but <strong>in</strong> practice the implementation details leak through. Don’t fall forthe abstraction: it takes noth<strong>in</strong>g away from the value of LINQ, but you do need to beaware of what you’re cod<strong>in</strong>g aga<strong>in</strong>st, and keep an eye on what queries are be<strong>in</strong>g executedfor you.So, we’ve seen LINQ work<strong>in</strong>g aga<strong>in</strong>st both web services and multiple databases.There’s another piece of <strong>in</strong>frastructure that is commonly queried, though: an enterprisedirectory.7www.joelonsoftware.com/articles/LeakyAbstractions.htmlLicensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!