Saturday, September 19, 2009

LINQ and Entity Framework Posts for 9/7/2009+

Note: This post is updated weekly or more frequently, depending on the availability of new articles.

• Update 9/17/2009: Erik Meier’s .NET Reactive Framework (Rx) presentation
• Update 9/17/2009: OfTypeOnly<TEntity>() function, introduction to LINQ to Objects

Entity Framework and Entity Data Model (EF/EDM)

Jim Wooley LINQ to Entity Visualizer post of 9/10/2009, which I missed when posted, begins:

When demonstrating the LINQ tools, I typically start out showing the LINQ to SQL visualizer that’s available with the C# Samples. Today I saw that Raja Venkatesh has released a Visualizer for ObjectQuery<T> (aka. LINQ to Entities). As you do with the other visualizers, you enable this by simply saving the AnySourceEntityQueryVisualizer.dll to your Visualizers directory.)

Note: The download page specifies to copy it to your C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Packages\Debugger\Visualizers. However, Windows 7 blocks saving files there by default. Alternatively, you can copy it to your My Documents\Visual Studio 2008\Visualizers.

• Alex JamesTip 35 – How to write OfTypeOnly<TEntity>() post of 9/17/2009 shows you how to write an function for Entity Framework that returns only the specify type and not derived types, as well as a GetImmediateDescendants() function to obtain derived types.

Julie Lerman warns Take Caution with WinForms DataGridView RowPrePaint in this 10/9/2009 post:

The DataGridView’s RowPrePaint is a great way to deal with displaying information across graphs. I have been using it with Entity Framework data when I have a grid that shows an entity who relies on an entity reference for more details.

For example if you have an order line item with a product foreign key, you don’t want to display the FK, you want the product name.

With RowPrePaint, as the row is being created from its data, you can inject your own data, formatting etc.

Diego Vega decides whether To PI or not to PI (with EF4, that is) in this 9/7/2009 post:

A customer recently asked if there is still any advantage in using the entities that Entity Framework 4 generates by default instead of POCO classes.

Another way to look at this is: why are non-POCO classes that inherit from System.Data.Objects.DataClasses.EntityObject and use all sort of attributes to specify mapping of properties and relationship still the default in EF4?

This perspective makes the question more interesting for me, especially given a great portion of the investment we made in EF 4 went into adding support of Persistence Ignorance, and also given that using POCO is my personal preference, and I am aware of all the advantage this has for the evolvability and testability of my code.

Kazi Manzur Rashid’s three part series describes:

Creating a full blown url shrinking service was pocking around in my mind for quite some time(of course by using Twitter). Since I heard quite a few good things on Entity Framework 4.0, so I decided to start with it. The first thing I usually do when developing an application is creating the domain model. But to create the domain model, we first have to define the basic functionalities:

  • The system will only use Open ID for authentication.
  • User should be able to shrink url without logging in.
  • When shrinking url, user should be able to specify alias, if alias is not specified, the system will auto generate it.
  • Shrinked url will also have a associated webpage preview image.
  • The system will maintain statistic of shrinked url like number of visit, referrer domain, geographic data etc. (requires login)
  • The user should be able to reset shrinked url statistics. (requires login)
  • Should have a REST service for creating shrinked url which will work upon the daily limit that was previously set.
  • It should have nice web 2.0 style interface and should support adaptive rendering.

Shrinkr - Url Shrinking Service Developed with Entity Framework 4.0, Unity, ASP.NET MVC And jQuery (Part 1) 9/10/2009

Shrinkr - Url Shrinking Service Developed with Entity Framework 4.0, Unity, ASP.NET MVC And jQuery (Part 2) 9/13/2009

Shrinkr - Url Shrinking Service Developed with Entity Framework 4.0, Unity, ASP.NET MVC And jQuery (Part 3) 9/14/2009

LINQ to SQL

Matt Warren continues his homespun IQueryable series with Building a LINQ IQueryable Provider – Part XVI (IQToolkit 0.16) of 9/15/2009:

This is the sixteenth in a series of posts on how to build a LINQ IQueryable provider. If you have not read the previous posts you might try rolling into a ball and crying for momma. That’s right, reading on is as hazardous to your health as a skinned knee. Just be warned and have an anti-biotic on hand.

Complete list of posts in the Building an IQueryable Provider series

I would have had the update out sooner but I couldn’t think of what to write in this excuse slot. My excuse generator was down for scheduled maintenance but did not come back on line as planned and wouldn’t you know it the techie responsible gave me all sorts of lame excuses. Some people!

What's inside:

    • Performance – I’ve actually tried to improve things.
    • Caching – Re-use queries without the burden of defining delegates.
    • Evaluation – Execute expression trees without Reflection.Emit.
    • Bug Fixes – Most of the bugs reported on CodePlex are fixed.

The full source code and redistributable DLL's can be found here.

Jim Wooley describes Testing to see if a record Exists in LINQ to SQL in his 9/13/2009 post:

There are a number of options you can consider when testing to see if a record exists using LINQ to SQL. Which one should you use? It depends… In general, check the generated SQL for various options in SQL Management Studio to see the how the various execution plans compare.

Chris Swain explains his Linq to Sql WCF Service Generator 1.7, “The New and Improved Linq to Sql WCF Service Generator!,” in this 9/12/2009 post:

After working with the original Linq to Sql WCF Service Generator template (see original post) in a real world application I’ve come up with some changes that I think are for the better. So I figured I’d post this new version on the blog so others can make use of it.

I realized when using the generator template in a real world application is that most real world services don’t want to directly expose their database structure to prospective clients. Most developers want the freedom to decide which properties to expose to the client as DataMembers on their DataContracts.

The original version of the template directly exposed the results of the Linq to Sql designer entities to any client. This meant that in order for the template to work, the serialization for the Linq to Sql designer had to be set to Unidirectional. This also meant that any client using the service was easily aware of the structure of the database as exposed through the Linq to Sql designer.

As a solution, I changed the template so that it now generates a set of DataContracts for each of the methods exposed by the service. By default, each DataContract generated does not have any DataMembers exposed. This gives the developer the freedom to expose whatever DataMembers are necessary for the service via a partial implementation of the DataContract classes.

All this explanation is nice, but nothing helps you understand something better than actually using the code. So, I’ve reworked the original walk-through to show the usage of the new 1.7 version of the template.

Jeffrey SchwartzLINQ to SQL Lives On post that appeared on 9/10/2009 in the Visual Studio Magazine site’s Data Drive column includes:

… Tony Sneed, a consultant and trainer, believes those looking to use an ORM for SQL Server will be better off using LINQ to SQL rather than the Entity Framework. In a blog posting last week, he pointed to a client, Credit Solutions, who successfully used LINQ to SQL for a line-of-business application  "You're going to get up and running much more quickly with LINQ to SQL than you would with Entity Framework," Sneed said in an interview.

But the question he pointed to in the blog posting: "who is adding new features to LINQ to SQL?" The first viable alternative he has come across is PlinqO, developed by code generation tool supplier, CodeSmith.

LINQ to Objects, LINQ to XML, et al.

Keith Dahlby’s Hacking LINQ Expressions: Join With Comparer post of 9/19/2009 solves the following problem:

Many of the Standard Query Operators require comparing sequence elements and the default query providers are kind enough to give us overloads that accept a suitable comparer. Among these operators, Join and GroupJoin have perhaps the most useful query syntax:

 var res = from s in States
           join a in AreaCodes
           on s.Abbr equals a.StateAbbr
           select new { s.Name, a.AreaCode };

While a bit more verbose, I find the intent much easier to read then the method equivalent:

var res = States.Join(AreaCodes,
                      s => s.Abbr, a => a.StateAbbr,
                     (s, a) => new { s.Name, a.AreaCode });

Or maybe I’ve just spent too much time in SQL. Either way, I thought it would be useful to support joins by a comparer.

Erik Meijer presented a session on the .NET Reactive Framework (.NET Rx) to Sun Microsystems’ Java Virtual Machine Language Summit on 9/17/2009. Erik’s abstract says:

The .NET Reactive Framework (Rx) is an abstraction of the well-known Subject/Observer pattern derived as the mathematical dual of the iterator pattern.

We show how Rx can be used to program against event streams and asynchronous computations using LINQ syntax as well as using good old imperative code.

• Jason Young provides an introduction to LINQ to Objects in his Understanding LINQ and LINQ to SQL (and EF) post of 9/17/2009:

Back to basics for this post. Developers often throw around the word LINQ when talking about a number of different technologies. Now that I have been comfortably using a wide variety of LINQ technologies for a fair amount of time, I’m now able to convey some of the key differences that are critical to using LINQ technologies efficiently. I’m also using this as a foundation and reference for some exciting upcoming posts. …

Understanding the internal workings of these technologies will let us fully take advantage of all the wonderful features it has to offer. In upcoming posts, I’ll be warning you of some potential pitfalls related to how your queries are interpreted and translated. I’ll also be showing you how to get significant performance gains by using LINQ to SQL or Entity Framework efficiently (over traditional SQL based solutions). I’ll also be showing you how I write LINQ queries to query an AutoCAD document!

Keith Dahlby is up to part four of his LazyLinq series with Simplifying LazyLinq of 9/13/2009:

This is the fourth in a series of posts on LazyLinq, a wrapper to support lazy initialization and deferred disposal of a LINQ query context:

  1. Introducing LazyLinq: Overview
  2. Introducing LazyLinq: Internals
  3. Introducing LazyLinq: Queryability
  4. Simplifying LazyLinq
  5. Introducing LazyLinq: Lazy DataContext

As I was iterating on the proof of concept for LazyLinq, I always wanted to get rid of the TQuery type parameter. I thought I needed it to distinguish between ordered and unordered wrapped queries, but it just felt messy. The underlying provider mechanism didn’t need it, so why should I?

Well after taking a closer look at the SQL query provider, I figured out how to eliminate it.

Gunnar Peipman explains Using LINQ to query object hierarchies in this 9/15/2009 post:

I used LINQ to solve the following problem: find all titles of objects at hierarchy level X when you know object ID in hierarchy level Y. I cannot imagine if there is some other solution that is same short and clear as one that LINQ provides. Take a look and decide by yourself.

linq-levels-example Here is simple diagram with my entities. Here are my simple rules. Level1 has no parent level and my contain one ore more Level2 entities. Level2 entities have one Level1 parent and one or more Level3 entities. Level3 entities have one Level2 parent entity and collection of one or more Items. So there is many-to-many relationship between Level3 and Items.

We cannot use composite pattern here because these classes will be very different and there will be no point where we need one common interface for them. That’s why we have one class per level. Also the number of levels is fixed and there is no plan to expand this hierarchy.

By the way, you can model arbitrary class hierarchies on this model and still use this example (as long as it doesn’t hurt performance and you are really sure what you are doing).

Bart De Smet’s lengthy Taming Your Sequence’s Side-Effects Through IEnumerable.Let post of 9/10/2009 begins:

Side-effects don’t fit together very well with functional-style delayed computation, as observed in LINQ. Having such constructs embedded in an otherwise eagerly evaluated imperative language like C# can cause quite some confusion from time to time.

Bart then continues with many code examples for and further explanations of these topics:

  • Multiplication of side-effects
  • What’s let anyway?
  • IEnumerable.Let – Signature and use
  • Implementing IEnumerable.Let
  • A few samples
  • Conclusion

ADO.NET Data Services (Astoria)

Vincent Home explains Building RESTful application using SQL Azure, WCF, ADO.NET Data Service & Telerik OpenAccess WCF Wizard in his 9/10/2009 post which has links to these videos:

And these posts:

Phani Raju’s Using the ADO.NET Data Services Silverlight client library in x-domain and out of browser scenarios – II (Forms Authentication) post of 9/10/2009 continues his Astoria for Silverlight series:

In this blog post, we will talk about using the Silverlight Client Library against a Data Service that is secured with Asp.Net Forms Authentication.

In short, the whole process of authenticating against a Forms Authentication protected Data Service looks like this:

image

Click here to download sample application

ASP.NET Dynamic Data (DD)

Henry CordesSearch functionality in ASP.NET Dynamic Data post of 9/6/2009, which I missed when posted, begins:

Using the ASP.NET Dynamic Data features introduced by the .NET Framework SP1 on my new project( a short intensive XP'ish, prototyping),  I was impressed by the fact that in two days I had all entities up and running in a data-entry application.

So I had something to show my client and the future users. They told me what they liked and how they felt the application should be working.

The first thing was that they wanted to search. Not only Filter (what is made possible out-of-the-box), but search, to my surprise this was not possible!

I went to www.asp.net/dynamicdata and opened the forum. I asked for the functionality. Scott Hunter, the PM on Dynamic Data answered my question an gave me the following url: http://www.codeplex.com/DynamicDataFiltering.

Josh Heyse from Catalyst Software Solutions is responible for this release. It is a dll that holds a DynamicLinqDataSource, DynamicFilterControl and the DynamicFilterForm. You just put it on the page by dragging and dropping.

blog comments powered by Disqus