LINQ, I'm not ready for you just yet

Today I was between features on the current project I'm working on, so I had some free time start researching some technologies I've been meaning to learn and start using. The topics at the top of my learning list are LINQ and MVC. I gave LINQ a few months to mature, so I figured it was a good time to investigate.

Boy Crying

(there is no emoticon to express my anger!)

The picture above shows how I felt when I started writing my first LINQ expression. The biggest problem was the fact that the latest version of ReSharper doesn't support any .NET 3.0+ language features. Not only does it not support LINQ, it's IntelliSense severely interrupts you while writing it. So much so that it makes it unusable.

I went ahead and downloaded the latest development build (Build 783). On their download page, it's listed as "Works here". That wasn't encouraging. It does work a little better with LINQ, but it's still a steaming pile of you know what (dog poop for the not-so-smart). This further reinforces my love/hate relationship with ReSharper.

Anyway, I was eventually able to write some LINQ code. A great tool to get started is LinqPad, which is basically a query analyzer but with LINQ expressions. Writing LINQ is very difficult with a SQL background, because everything is backwards. You think you know what you're doing, but you don't.

Right now, we're using NHibernate in the main project that I've been working on for the past couple of months. It's amazing, but there are a couple of things that would be nice:

  • Better optimization of queries - It looks like LINQ does an amazing job with this.
  • Batched reads & writes - LINQ does batched writes, but lazy loading by default. Maybe not as big a deal as I think.
  • Cross session saving - I spent hours battling with some code that loaded a complex object with relationships in one session, and then saved them in another. It appears that LINQ solves this, but I'll have to run some tests to be sure.
  • Less work generating mapping files and relationships.

One thing that is nice about LINQ to objects is the fact that it will generate all of the model classes, plus the glue that connects the model to the database. You can either use Visual Studio and drop the tables into a mapping file, or you can use SqlMetal to script the class generation.

One of the biggest questions I'm trying to answer write now, is how unit testing fits in with LINQ. We're currently testing our data access layer by using an in memory SQLite database, which let's us perform close to real world saves and loads. We also use interfaces for our data access methods, which makes it easy to create testable classes that can simply be supplied a database interface.

I'm also not sure if it even makes sense to put my LINQ queries in a data access layer. The code would almost seem trivial, and would just create a lack of flexibility. Ironically, it almost feels like you should use LINQ to query against your data access layer.

For now, there are more questions than answers. For now, I don't plan on retrofitting my last project with LINQ, but I'm going to investigate if it will be a good foundation for the data access logic in my next project. Of course if I go that route, you'll be sure to hear about it!

Like this post? Please share it!

See a mistake? Edit this post!

.NET XML DataSet Generator from a SQL Query

As promised, I've now made the .NET XML DataSet Generator available on Google Code.

.NET XML DataSet Generator

If anyone can think of a better name for the project, please let me know! It's really hard to convey what it actually does. On the project page, I put these basic steps, which should help make it clear:

  1. Run this program to execute a SQL statement
  2. Copy the resulting XML into a file in your .NET unit test project
  3. Embed the XML file you created
  4. In your test code, load the embedded XML, deserialize it back into a DataSet (code coming soon)

As the project matures a little bit, I'll have better documentation. I may even whip up a video demonstration. It's not really a fancy tool, but it does come in handy.

For example, let's say you have a database method that returns a DataSet containing a table of blog post items. You have another method that turns that DataSet into an array of post objects. I know that you're thinking you should normally use an OR mapper, but this is just an example, so please don't interrupt me!. This tool lets you create a serialized DataSet that you can use in a unit test, which will reconstitute the DataSet into the exact same thing your database method would have returned. You can now pass in a snapshot of real data into the method your testing. If you ARE using an OR mapper like NHibernate, this probably won't happen much, but when it does, this tool will come in handy.

Like this post? Please share it!

See a mistake? Edit this post!

IIS 6 and Apache on the same server and port

This has been written about before, but I just ran into it, and hopefully I can save someone a headache.

Apache Feather 

I installed Apache on my dedicated server, which has 9 IP addresses. Since all my sites have a dedicated IP address, I figured I would just configure Apache to listen on some IP's, and IIS listen on the others. They both would listen on port 80. I configured it correctly, only to find out that Apache wouldn't start.

It turns out that IIS hogs port 80 on all IP addresses, whether IIS is configured to listen on them or not. Microsoft has a good knowledge base article on the subject, and of course it's by design so it's not going away.

The only thing to remember is that the instructions are for specifying the IP addresses that IIS should listen on. For some reason, I was thinking the opposite initially, which obviously caused some frustration.

Like this post? Please share it!

See a mistake? Edit this post!

Jason Young I'm Jason Young, software engineer. This blog contains my opinions, of which my employer - Microsoft - may not share.

@ytechieGitHubLinkedInStack OverflowPersonal VLOG