Getting Distinct Objects Using LINQ

If you try to get distinct objects using LINQ you have two choices. You can use the overload to Distinct, which takes an IComparer<> that you write, where you override Equals() and GetHashCode() or you can do a grouping and select the first object of each group. For example, at my work I work with a lot of Title objects. If I have some duplicates in my list I can’t just use Distinct(), because it will think every object is different, it doesn’t know how to determine what’s equal and what isn’t. Instead I would want to compare the Title Ids to check if they are different. You can do that like so

var distinctTitles = (from t in TitleList select t.Title).GroupBy(x => x.TitleId).Select(g => g.First());
|

LINQ and Collections

When we first upgraded to the .NET 3.5 framework, the one feature I wasn’t the biggest fan of was LINQ. My main reason for the dislike is the way the syntax stands out from everything else around it. It reminded me of when I was a COBOL developer, and the code would have inline DB2 or IMS logic, which stood out from everything else. It may be powerful, but it just doesn’t look or feel right.

Well, over time I got over it. We have used LINQ a lot in this Silverlight project I am working on. The main use is for getting a distinct list of items, or to quickly filter or sort. It works great for this. The other day I found an even greater use.

Let’s say you have an object that looks like this:

public class TitleRight
{
    public Guid ID { get; set; }

    public List<Territory> Territories { get; set; }

    public List<Language> Languages { get; set; }
}

Now, let’s say you have a list of TitleRight objects, and you want to find the ones in the list that have a certain TerritoryID and a certain LanguageID. In the past, one way you would accomplish this would be to iterate through the list of TitleRights, and for each one, you would then iterate through the list of Territory and Language, and try to match on the ID.

Now, in .NET 3.5, with the help of LINQ and Lambda expressions, you can do the following

var query = from tr in TitleRights
            where tr.Territories.Find(t => t.Id == territoryId) != null &&
                  tr.Languages.Find(l => l.Id == languageId) != null
            select tr;

So, what is this code doing? It uses the List<T>.Find() method to search the list for a match, returning NULL if nothing is found. It took a little trial and error for us to figure this out, but in the end I think it truly shows the power of LINQ. This accomplished in 4 lines of code, something that would normally take probably 2 - 3 times as many.
|

Tech Books I'm Currently Reading

The iPhone Developer’s Cookbook

33463358

I have a couple ideas for iPhone applications, nothing big, quick hit applications that would be available for free, so I picked up this book to point me in the right direction. I also have plans for a big iPhone application, that the people I’ve mentioned it to think is a great idea, but it’s one of those things that would take a team of developers, some venture capital, and some big company buy in, not something I think I can pull off at the moment, but maybe I can patent the idea Happy

Silverlight 2 Unleashed

31942746

The application I am currently working on at work is a Silverlight web application. Silverlight 2 was recently released out of beta, so there are finally books showing up that are no longer referencing the beta build. I purchased this book to bring into our war room as a reference. I like the Unleashed books, and this one seems to cover a wide variety of subjects related to Silverlight 2, so hopefully it’s of use to the team.
|

Using the Netflix API

The project I am currently working on makes use of the Netflix API to retrieve information about titles and displays the box art. It took me a while to get it working, mainly because I was trying to do too much. I figured I’d share this information, in case you also are going about using the API in the wrong way. There are two ways to use the API. First, you use it to make simple calls to their RESTful web services. Second, you are making a front-end of some sort that signs in as the user and lets them manipulate their queue, browse the library, or watch on demand streams. For our application I needed to go the first route, but was coding it the second way, which was overkill, and was causing me major problems. To perform a simple Title search, or to do a daily download of their entire index, you don’t need to sign in as a Netflix user, you just need to authenticate using Open Authentication with your consumer key and consumer secret (which you will need to apply for on their developer site).

Some kind developer was nice enough to write a simple class to help you with this. You can view the code to his implementation, OAuthBase, here. Add this class to your application, and then it’s as simple as the following code to perform a title search.


private void Search()
{
    var url = new Uri(“http://api.netflix.com/catalog/titles?term=” + titleName);

    var consumerKey = ConfigurationSettings.AppSettings[“ConsumerKey”];
    var consumerSecret = ConfigurationSettings.AppSettings[“ConsumerSecret”];

    string normalizedUrl;
    string normalizedRequestParameters;

    var oauth = new OauthBase();

    var signature = oauth.GenerateSignature(
        url, consumerKey, consumerSecret,
        null, null, “GET”, oauth.GenerateTimeStamp(), oauth.GenerateNonce(),
        out normalizedUrl, out normalizedRequestParameters);

    var finalUri = normalizedUrl + “?” + normalizedRequestParameters + “&oauth_signature=” +
        oauth.UrlEncode(signature);

    // Call the service to perform the title search
    var xmlDocument = new XmlDocument();
    xmlDocument.Load(finalUri);
}


Believe me, this is so much simpler than what I started out doing. I was following their examples and I was connecting to their site through my application, bringing up a Netflix login screen, having to add my application to a trusted application list, etc. I wasted a couple days development before I realized that was only needed if I wanted to let users interact with Netflix, not just do a title search.
|

VSTS 2008 Power Tools

If there is one category of software developed by Microsoft that I have never been a fan of, it’s the source control applications. Visual Source Safe was just awful, and their newer one, Team System, isn’t all that much better.

When I first started at Turner, we were using Source Safe on my team. From there we moved over to Star Team, which actually made Source Safe look good. We eventually left Star Team for CVS, which rocked. I like CVS because it is simple to use, and it just works. My favorite feature is using an application like Tortoise CVS, which allows you to do all of your source control from within Windows Explorer. We moved to Subversion from CVS, which is a better version of CVS, so I liked it even more. Once I loaded Tortoise SVN I could use it from Windows Explorer too.

When I started on this Agile project, we moved over to Team System. It’s pretty much like Source Safe, but has some extra features. The main reason we moved over was for its SCRUM and shelving capabilities, two features we never use. It integrates with Visual Studio, but it isn’t the greatest integration. If you have multiple machines you work on, it screws up its whole ‘workspace’ concept. Worst of all, to edit a file you had to fire up Visual Studio and go into the Team Explorer interface and check it out for edit. This sucks when you want to make a simple configuration change. This is where CVS and SVN rocked, you just opened the file in whatever editor you like and edit it. Since it monitors the directory, it picks up the change. Right-click on the file and choose Check-In. It’s that simple.

Well, you can finally do this with Team System, if you go and download their VSTS 2008 Power Tools. The shell integration isn’t installed by default, you have to do a custom install to enable it. After a log-out-log-in you now have VSTS capabilities from within Explorer.
|

Extension Methods

Since we are using Silverlight on this new project I am working on, we are doing our development in .NET 3.5. One of the new features in .NET 3.5 is Extension Methods. When I first saw this I figured it was a nice feature, but I didn’t know when I would use it. Today I wrote a couple. I figure there’s probably a couple reasons you would create an Extension Method: (a) You see you are performing the same coding routine in multiple places, and it would be nice if you could replace it with less code, and (b) there’s a method you always wished was on one of the built in .NET types -- something like String.ToUpperCase() or something along those lines. The Extension Methods I wrote today fell into the first category. We have a lot of places in our code where we check if an object is null before progressing.

if (contract == null)
    return;

I wrote a simple IsNull Extension Method that can be applied to any object as long as a reference to my Extension Method namespace is in the using directives.

public static bool IsNull(this object source)
{
    return source == null;
}

It’s very simple, and only saves a little typing, but it looks nicer to have the following

if (contract.IsNull())
    return;

We also tend to have a lot of code that checks if a List is not null and also isn’t empty

if (titleIds == null || titleIds.Count == 0)
    return;

For this I created an IsNullOrEmpty Extension Method

public static bool IsNullOrEmpty<T>(this List<T> source)
{
    return ((source == null) || (source.Count == 0));
}

This way you can write the code as

if (titleIds.IsNullOrEmpty())
    return;

These are the only Extension Methods I’ve written so far, but I can think of another one that would be useful and would fall into the second category of why you would write Extension Methods. Let’s say the Generic List of Integer TitleIds I had in the previous example had a couple duplicates, it would be nice to be able to say

titleIds.RemoveDuplicates();

|

Visual Studio Error: "Some of the properties associated with the solution could not be read."

Something happened when I created the Visual Studio .NET 2008 Solution for my Executive View project and checked it into Team System. After it was checked in, I, and the other developer, would receive the following error when opening the solution: “Some of the properties associated with the solution could not be read.”

It took a little digging, but once I looked at the solution file in Notepad++, I saw that there was a duplicate GlobalSection(TeamFoundationVersionControl) = preSolution section. It’s the section that contains all of the parameters prefixed with Scc*. If you delete the duplicate selection it will fix the error.
|

Starting New Project At Work

A few months back I was involved in developing a prototype of an Executive View application, using WPF technology. I wasn’t the biggest fan of the project, mainly due to us using Tangerine, a pre-developed application from Infragistics. While it did help us get the prototype out the door in a timely fashion, I didn’t want us to go the route of just building on top of that application. Luckily we didn’t go that route. It was decided we could use Tangerine as a reference application, which is what I was pulling for from the beginning.

This week will mark the first iteration of The Executive View development. We are doing it in .NET 3.5, with a Silverlight front end. It will be a nice diversion from the project I was working on, which will be nice. I’m not the biggest fan of Silverlight development, mainly because here I am trying to get out of Microsoft development all together, and I’m going to be learning a totally new development environment. At the same time I guess it is pretty cool to be developing using the newest, cutting-edge technology. We’ll see how it goes.
|

Microsoft Validation Application Block

I first used one of the Microsoft Application Blocks a few years ago, and needless to say, I wasn’t all that impressed. We tried to use the caching application block. It was more a pain than it was worth. Because of this I was a little reluctant to use any of the application blocks again, but after seeing a quick presentation on the Validation Application Block from a co-worker, I decided to give it a shot. The application blocks have come a long way. The code is a lot cleaner now, and it feels like the overall design is better. I guess it’s like a lot of Microsoft applications; version 1 sucks, but it slowly gets better with each subsequent version.
|

WPF - Thoughts on first week of use

I've been doing development on our prototype application since the beginning of this week and thought I would give my thoughts I what I think of WPF.

Pros:

It's pretty easy to do things using WPF compared to doing the similar thing in traditional .NET WinForms development. Now, I don't know if this is a case of WPF being a great framework, or WinForms being a bad one; but I'll still consider this a Pro.

Cons:

The tools. Even though WPF has been out a while, the tools used for development still aren't up to snuff. I perform a majority of my XAML development using Expression Blend March 2008 CTP, and all of the C# coding using Visual Studio .NET 2008. In a way this sucks, because, even though Expression Blend is made for doing WPF development, there's no intellisense to be found. Also, it doesn't integrate with a source control system, like Team System, which we use. So I will modify a XAML file and go to save it, then find it's read-only and needs to be checked out. VSTS doesn't have a stand-alone client, I have to fire up Visual Studio to check out the file.

The prototype we are developing is modeled on Infragistics Tangerine example application. This sucks because we're shoe-horning our code into all this existing code. I actually scrapped all the code on the window I am modifying, and also scrapped using their overly complicated XamDataGrid for the built in and easily customizable WPF ListView. Hopefully once we get through with this prototype next week and they show it to the steering committee, and the buy off on it, we can scrap the whole Tangerine application and begin fresh. It's hard to learn a new technology when all you're doing is adding code to an existing application.

Overall I like WPF. I'm not exactly enjoying doing the current development we're doing with it, but it is a nice change of pace.
|

WPF

One of the upcoming features of the application we are working on is called the online experience. We want to provide the higher ups, the directors and managers, with an easy, intuitive look into what's going on at the moment and in the future, in our company. One thing my managers are fans of, which I'm happy about, is eye candy. I don't mean having lots of bling for no reason. But, from working on a Mac, you come to like the eye candy that makes the operating system and it's applications a pleasure to use. I suggested we look into using WPF (Windows Presentation Foundation). I've been pretty impressed with some of the demo applications I've seen, and hopefully we have waited long enough since it's introduction that a lot of the bugs have been ironed out. One thing I really like is this gives us a chance to have a business application that doesn't necessarily have to follow the usual boring business application look-and-feel. It does get boring sometimes working on an application that's just filled with grids displaying information, will be a welcome change to introduce something new.
|

WCF: Underlying connection closed

If you use WCF you may encounter an error with the following text: 'The underlying connection was closed: The connection was closed unexpectedly.' This has to rank up there as one of the most useless error messages you may receive as a developer. It's sort of a catch-all error message. We encountered it twice today, and the only thing in common between our two issues is it was looking like a serialization issue.

I spent about 4 hours or so trying to figure out why one of my classes was having trouble serializing. It finally came down to one of the fields being of type System.Type. Apparently you can't serialize a type of System.Type, because it's an abstract class that inherits from an internal System.RuntimeType class. Now, wouldn't that be a good error message, 'Unable to serialize type: System.Type', instead of the message above that gave no indication as to what was wrong?

The second instance of a team member receiving this error was their serialized object containing more items than is specified in the maxItemsInObjectGraph. Again, wouldn't it be nice to receive an error message, 'Unable to serialize: number of items exceed maxItemsInObjectGraph.'?

If you receive this catch-all error, you should look into anything which can cause a serialization issue.
|

WCF Message Size Limit

My first technology post of 2008!

We ran into an interesting problem at work that I thought I would detail, in case others run into this problem. A little background on our project. Our application is used for the entry and management of contracts. These are entertainment contracts, which can pertain to 1 or more titles. These titles can have multiple rights and restrictions. A lot of contracts are pretty simple, but some can get complex, containing a large amount of data. In our testing we ran into one of these kinds of contracts. It was for a long running show, so it contained all the episodes ever aired, which was around 460 episodes. Now, add to that a couple rights that pertain to each episode and a couple restrictions, plus we attached a copy of the real contract in PDF form, which was around 860k in size. When we tried to save the contract it bombed quickly not giving any indication of what occurred. When we looked at the Request Message in Fiddler we saw that it was a little over 4MB in size and it looked fine, so no red flags there. We checked the trace logs and it didn't provide us with any answers, just an error message saying the server connection closed unexpectedly. We also looked at the app.config and the web.config settings to check if all the maxMessageLengths and maxBuffers were set to their maximum, along with high timeouts. All of this looked fine. Searching the web provided us with no answers, so we did some testing. We created a new contract that contained no rights or restrictions and we attached one instance of the PDF. This saved fine. We then attached 2 PDFs, then 3PDFs, and it finally failed once we hit 4 PDFs. This caught our attention because the size of the message we saw in Fiddler before was around 4MB and now this contract with 4 PDFs was around that size too. Still, 4MB seemed small, since all of the maxLengths in the config files are Int32, meaning their maximum value is 2GB. Now that we had this info I did a search and found a couple interesting things. MSMQ has a maximum message size of 4MB. This was interesting, but we weren't using MSMQ, so I looked elsewhere. That's when I ran into ASP.NET having a limit of 4MB for files being transmitted. This is apparently a safety feature to prevent DOS attacks. While we aren't using ASP.NET we are using WCF services with wsHTTP, so I figured it was worth a shot. I went into the web.config and added the following line in the System.Web section

<httpRuntime maxRequestLength="32678"/>

This set the max length of a Request to 32MB.

Once this was added we ran the same test we had before and the contract saved with no issues. We researched this issue over the course of a couple days and were fearing the worst. We thought we may have to re-architect the entire way we passed around data. In the end we were glad to see it was 1 line that fixed everything.
|