Bill Blogs in C#

Bill Wagner discusses C#, LINQ, and other items of interest

February 2007 - Posts

Where I discuss object identity and nullable queries

It’s been quite a while since I’ve tackled the DLINQ (sorry, LINQ to SQL) samples on this blog.  The combination of CoedeMash, writing for MSDN, and researching a book has meant that I haven’t done much here in the blog.  It’s time to change that.
The next LINQ to SQL sample group is quite simple. They show how LINQ to SQL uses entity references and link tables when resolving relationships between records in different tables.
The first sample shows this query:

    var q =
        from e in db.Employees
        where e.ReportsToEmployee == null
        select e;

The ReportsToEmployee property returns an EntityRef<Employee> in the employee table. EntityRef<T> is a reference type, so if there are no references, the ReportsToEmployee property returns null.  That’s all there is to it.
But, there is another way to handle these types of references.  The Employee class also contains a property that shows the employee ID for the reports to employee. The ReportsTo property contains a Nullable<int> that holds the value of the manager’s employee ID.
You can use ReportsTo to find all employees with no manager:

    var q =
        from e in db.Employees
        where !e.ReportsTo.HasValue
        select e;

Or, report on those entities that do exist, and retrieve the ReportsTo value to display that empolyee’s manager:

    var q =
        from e in db.Employees
        where e.ReportsTo.HasValue
        select new {e.FirstName, e.LastName, ReportsTo = e.ReportsTo.Value};

The next set of queries is the date time and string queries. I’m going to skip them until next time, and jump ahead to the Object Identity queries. The object identity samples show how LINQ to SQL actually does the work of mapping the object model to the relational model.   Throughout LINQ 2 SQL, the LINQ libraries ensure that anytime a query returns the same object, you receive a new reference to the object that has already been received. While these samples don’t show it, you must remember that this property is true even for records that contain links to other records, or are retrieved from other tables. Here’s the first sample that illustrate this very important concept.

 Customer cust1 = db.Customers.First(c => c.CustomerID == "BONAP");
 Customer cust2 = db.Customers.First(c => c.CustomerID == "BONAP");
Console.WriteLine("cust1 and cust2 refer to the same object in memory: {0}",
    Object.ReferenceEquals(cust1, cust2));

If you run this sample, you’ll see that cust1 and cust2 do indeed refer to the same object in memory.
The second sample shows how you do indeed get the same results using two different queries that do retrieve the same record from the database. Cust1 returns the first (and likely only) customer that has the BONAP customer id.  The second query looks through the orders table and finds the first order where the customer ID is BONAP. Then, that order is retrieved.  The comparison using ReferenceEquals to show you that both object variables do point to the same object in memory.

 Customer cust1 = db.Customers.First(c => c.CustomerID == "BONAP");
    Customer cust2 = (
        from o in db.Orders
        where o.Customer.CustomerID == "BONAP"
        select o )
        .First()
        .Customer;
    Console.WriteLine("cust1 and cust2 refer to the same object in memory: {0}",
                      Object.ReferenceEquals(cust1, cust2));

Now, to be fair, I’ve not gone out of my way to try to break this. I’ve also done very little with NHibernate or other ORM toolkits. Therefore, I’m going to be very guarded in my praise for this part of LINQ 2 SQL.  I like the design. I like the fact that it would seem that you don’t need to worry about the object identity. But, as I said, this is not an area where I’m an expert.



Posted by wwagner | with no comments
Filed under: ,
Yet another Fibonacci demo (but still worth reading)

Dustin Campbell (who is speaking at the Ann Arbor .NET Developers Group in March) has been writing a series of articles on C# 3.0. 

His series is well worth reading.  If you've looked at any of the talks I've given recently on C# 3.0 (at AADND, or CodeMash), you'll enjoy reading Dustin's blog. Dustin is covering much of the same ground. but from another angle. You'll get a different perspective, and that will help you get a greater understanding.



Fibonacci Numbers, Caching and Closures
Part 1: delegates and closures
What's in a Closure?
Part 2: explaining the underlying techniques covered in 1
Using Automatic Memoization
learning about storing function results for later use
A reference to a set of MIT lectures
A great set of 20 one hour lectures on Computer Science
Posted by wwagner | with no comments
Filed under:
Don't let the weather keep you away

Tomorrow (yes, I'm still behind in blogging) Jennifer Marsman and Drew Robbins will be speaking about Avalon. (You may know thid technologies as WPF but I still have trouble using the sterilized marketing names for anything .NET 3.0).  I'm looking forward to this, because Avalon brings powerful graphics capabilities to all developers, even those that aren't well versed in topics like meshes and tesselations and Phong shading. If you can visualize it, you can code it in Avalon.

Don't let the weather keep you away.  (People might think we're like those wimpy Seattle folks that cancel everything when it snows).

And, in March,  when it's thawed out a bit, we'll have Dustin Campbell giving a new talk entitled "Delegates and Events: the inside story". 

I always like Dustin's talks. He gets to the core of the technical issues It's about languages, low-level techniques, and best practices you'll use everyday. No matter what you're building, no matter what architecture or problem domain, Dustin will help you write better code.

He's got his abstract posted on his blog: http://diditwith.net



Posted by wwagner | with no comments
It's not sales, it's not marketing. It's doing great, and letting your customers spread the word

Kathy Sierra had an interesting blog entry related to users evangelism of a product or service. She is (as usual) pretty spot on with the way she describes the issue. She points out that enthusiam for something goes so much farther than marketing dollars.

The perfect case study for this would be the Grateful Dead, and many of the bands that followed their example.

Ask any deadhead about the band, and you'll probably hear about their tape collection.  (Full disclosure: I've acquired more than 250 tapes over the years). My story is similar. I went to my first concert with a friend. I had a blast, and asked what albums he'd recommend getting. He smiled, and made me a few tapes from his collection. I was hooked. So I asked for more. He said to start looking for some other deadheads, and start trading. I found some other deadheads, added to the collection.

Years went by, and I'd started trading online. I also started making tapes for other friends and they started their own collections.

But, this blog entry isn't about trading tapes, it's about evangelism for a product or service. The trading tapes thing wasn't about money, access to the band, or anything much really. It was just about enjoying the music. (Yes, some folks made it about archiving every note the band played while on stage anywhere, but for most people, it was about the music).  The dead had a number of policies that drove the record companies crazy, but turned out to be great business decisions.

First, the dead allowed (and even encouraged) people to record their concerts. All you had to do was buy a ticket in the taping section. The taping section was right behind the sound board, so you got great quality tapes. In fact, the some of the tapers went to enough shows that they got to know the sound guys pretty well, and the sound guys would event run patch cords direct from the sound board to the tapers. (I've got some great soundboard tapes from the late 80's).  The only rule on the taper section: you could not sell the tapes. You could trade them, you could give them away, you could share with friends, but you couldn't sell them.

Second, the dead encouraged (or at least tolerated) the tape trading culture. As long as you stayed with the rules: no money, just trades. In fact, they even helped publish 'Deadbase', a complete listing of all the songs in all the concerts. (Want to know if your tape from June 14, 1984 has everything?  Check deadbase).

Third, dead.net continues to provide outlets for their music. Check out the taper section for music from this week in dead history.

This combination kept the dead successful during an 8 year drought between albums. They toured every year from 1980 to 1988 without releasing an album (studio or live).  The only way you heard their music was by hanging out with a deadhead that had a tape collection.

And yet, they were one of the biggest grossing concert bands through that period.

So, what are you doing to make it easier for your customers to recommend you to others? If they are enjoying your product or service, they'll want to. You just have to do great work, and make it possible.



Kathy Sierra on evangelism
Out spend or Out Inspire?

Posted by wwagner | with no comments
Filed under:
Announcing the inaugural meeting

Michigan's Interactive Designers and Software Development
Date: February 27, 2007
Time: 6:30PM
Location: Frenchie's
56 E. Cross St.
Ypsilanti,MI
Phone: (734) 483-5230

From their invite information:

The group's focus is Interactive Design for the Microsoft Interactive designers who are looking to design great user experiences:

Our first meeting will feature:

  • An overview of Expression Web (featuring a walkthru of it's features and creating a website). Expression Web is a new Microsoft product, centered around design using web standards.
  • Cool applications created with Expression Blend (Microsoft's WPF Interactive Design Program)
  • A look at designer's already using Expression and the applications they are creating.
  • Metalliq.com's Snowboard WPF Application (3D, Video, and GPS integration)
  • The Otto Online Shopping Experience (a WPF 2d/3D shopping app from the folks who own Crate and Barrel).
  • A look at the WPF Design Process
  • A review of the WPF/E cross-platform February CTP and discusssion about adding WPF/E video to your website and a discussion of the VC-1
    high definition video codec that can be used with WPF/E.
  • Open Question and Answer session for people wishing to enhance their design skills. We will with audience participation attempt to answer questions on popular topics like Photoshop, Expression Design, Macromedia Dreamweaver, Adobe Flex, Xaml, XML, and others.

Please note while the meeting is free to attend Frenchie's is donating space for the meeting and will be offering food and drink for the meeting for sale. We'd like to get an idea if you are attending or not so we can appropriately reserve enough space. please email us to let us know you are attending: midas.g@hotmail.com

We highly recommend that you come to the meeting and order something for yourself to eat or drink at the meeting so that we can keep the meeting space available to us for future meetings for free for the group. The food from SideTrack/Frenchies is great and they have a world famous burger that just can't be beat.



Posted by wwagner | with no comments
An engaging, online fictional blog world

OK, I've gone dark for a while. It's been busy around here with CodeMash, hosting the .NET hands - on lab with Richard Hale Shaw, the regular day job, and making some progress on the book.

But before I go back to some actual techincal content, let me draw your attention to Zombie Lawmen.  (www.zombielawmen.com) .

It's an engaging story told through the eyes of three different bloggers. New installments arrive roughly once a day, andcontinue to move the story forward. It's well worth the half hour a day to stay on top of it.



Zombie Lawmen
The zombie lawmen home page
Posted by wwagner | with no comments