Thursday, March 13, 2008

Type inference in .NET 3.0

One of the things I am having a hard time wrapping my head around is why one of the recommendations of Resharper 4.0 is to implicitly type all local variables. It goes against my anal retentive nature for clean, easy to read code.

The following article tries to explain the benefits of this practice, though I’m not sure they have me convinced though I have, at times, found myself annoyed with the redundant declarations of a new variable

i.e.


Dictionary<SearchCriteriaType, SearchCriteriaDisplay> criteriaDisplays = new Dictionary<SearchCriteriaType, SearchCriteriaDisplay>();


Can be shortened to


var criteriaDisplays = new Dictionary<SearchCriteriaType, SearchCriteriaDisplay>();


I guess that makes the code less cluttered. But is life really that much bad with the previous example that the Resharper folks found the need to suggest that it is now best practice?

http://www.programmersheaven.com/2/CSharp3-1

Wednesday, March 12, 2008

Visual Studio 2008 Error after upgrade

I needed some capabilities from VS2008 DB Edition to go with my VS2008 TA (Team Architect, not whatever other acronym starts with T&A), so I slapped a copy of that onto my machine this morning.

However, when I fired up VS2008, it started briefly, and then crashed. It did this a few times in a row, and I checked the event log.


.NET Runtime version 2.0.50727.1434 - Fatal Execution Engine Error (79FEC5F0) (80131506)


Ruh-Roh Shaggy. Time to reboot.

Try it again. Same problem.

We're in trouble now, Scoob.

After searching the web for a bit, I came across starting the app in SafeMode. with the/safemode switch. That worked. I let the app load up, then I closed it back down, removed the /safemode switch, and presto, back in business.

I think it has to do with my recent installation of Resharper 4.0 EAP, since the app was dying trying to load the toolbox items. We'll see if it happens again the next time I do an upgrade.

Tuesday, March 11, 2008

SQL Thief - Last Modified Stored Procs

I will admit that what I am about to put here, I stole from Pinal Dave's site. Nonetheless, I use these scripts quite frequently, but have a hard time finding them on line (you have to remember just the right search word, and I always forget to bookmark them). So for my quick reference

Here's how to get a list of all stored procedures modified in the last x days


SELECT name
FROM sys.objects
WHERE type = 'P'
AND DATEDIFF(D,modify_date, GETDATE()) < 7 --Change 7 to any other day value.



The following script will provide name of all the stored procedure which were created in last 7 days, they may or may not be modified after that.


SELECT name
FROM sys.objects
WHERE type = 'P'
AND DATEDIFF(D,create_date, GETDATE()) < 7 --Change 7 to any other day value

Monday, March 10, 2008

Dude, you're such a Tool

Yes, half the fun of blogging is coming up with good titles. This, post, as it so clear states, is about tools.

As a .NET developer, I take no shame in using as many tools as I can to make my day easier. Here are as many as I can think of. Most of them you will know, some you may have heard of, and some you'll think to yourself, why isn't he using xxxxx. Well, I want to know about xxxx, so if you don't see it listed here (and it pertains to something I would need at WORK, let me know.


  1. Visual Studio 2008 (of course, and 2005, and ack, sometimes 2003). LINQ in 2008 is quickly becoming my most important coding skill. Frankly, it had me at Hello, and I've been it's query biatch ever since

  2. SQL Server Management Studio - if you aren't working in SQL server these days, what, are you stuck in 1995?

  3. NUnit - Test driven development is getting a hold of me, and I like it!

  4. CruiseControl.NET - To know it is to be the only one in the office who cares, but it makes my life a lot easier in the long run

  5. NAnt - Part of my suite of build tools

  6. FireFTP - Mozilla add on for FTP. Great little tool

  7. FireBug - Mozilla add on for debugging web pages and css files

  8. Fiddler - Helps for debugging http traffic. Ethereal is another good tool to. I just haven't used it lately

  9. .NETReflector - Lost the source code for a .NET dll? Run it through Reflector to get it back. Want to compare two versions of a .NET dll to see what changed? Run it through Reflector

  10. ummm Visual Source Safe - better than nothing I guess. Slightly better.

  11. asp.net AJAX Toolkit 3.5. I've been really getting into AJAX this week, and it's pretty cool, though it does not seem very forgiving.



One of the tools I am going to try soon is called Resharper. I've heard really good things about it, and I'm pretty sure it will save me time, but it is quite expensive, and I just want to make sure I'm going to use it. Time to get the trial licence, and give it a whirl.

Anyway, that's pretty much what I sue on a day to day basis (that, plus google to help me find blogs that fix my bugs).

What am I missing?

Friday, March 7, 2008

Can I By a Val please Vanna

File this under gosh darn whacky wastes of time

If you have a web service written in VB.NET which has functions which are called by ASP.NET AJAX scripts, don't ever declare the values in the function declaration as ByRef. Use ByVal instead.

ASP.NET AJAX will show a Method 500 error and you'll spend much time trying to find out why that one call doesn't work.

Again, the side effects of relearning VB.NET

Oh joy.

You insensitive bastard

Here's a really simple function that uses LINQ and will throw a bizarre exception when written in VB.NET. The exact same function, written in C# will work perfectly.

First, create a simple class


Public Class SearchCriteria
Public GroupName As String
Public Keyword As String
End Class


Then, add the following function to an NUnit test harness using VB.NET


Public Sub BadList()
Dim searchCriteria As New List(Of SearchCriteria)

Dim distinctSearchCriteria As New List(Of SearchCriteria)

distinctSearchCriteria = searchCriteria.Distinct()

End Sub



Running that code, results in the following exception


System.InvalidCastException : Unable to cast object of type 'd__80`1[SearchCriteria]' to type 'System.Collections.Generic.List`1[SearchCriteria]'.




Do you see why? Since it's been a long time since I've done VB.NET, it took me a while to figure this out. Of course my code wasn't that simple, but here's the solution:




Public Sub BadList()
Dim searchCriteriaList As New List(Of SearchCriteria)

Dim distinctSearchCriteria As New List(Of SearchCriteria)

distinctSearchCriteria = searchCriteriaList.Distinct()

End Sub




It goes back to VB.NET not being case sensitive. Declaring a variable with the same name as the class (albeit a different case), confused the hell out of the runtime engine.

This is just one of the many reasons I prefer C# to VB.NET. But the client wants the development done in VB.NET, so I get to (re)learn another language.

Thursday, March 6, 2008

Book on Pre-Order

I don't think I have ever put a technical book on Pre-Order before, but I did just that this morning for LINQ Object Relational Mapping

I can honestly say that I spend hours every working day thinking about ORM. In fact, a large percentage of my time over the past 4 years has been trying to map database tables to usable objects and to try to find a good architecture for applications that really works in the real world.

I have really high hopes for this book, and deep fears as well. I'm hoping I read it and think "I can just tweak a little bit of my design here and there and I'll be right on track."

I'm fearing that I'll read it and either a) not know what the hell they are talking about and realize I am still in way over my head, it's time to start flipping burgers, b) OMFG, this is better than what I have by a huge factor. I have to rebuild everything, or c) WTF, do none of these guys who write these books ever spend time in the real world? With most books, it's a little of all of these at one point or another.

I guess I'll just have to wait and see. The book is scheduled to be released April 28, 2008.

Wednesday, March 5, 2008

NAnt and .Net Framework 3.5

When I went to build my first LINQ application on our build server running CruiseControl.NET, the first message I got was:


Solution file error MSB5014: File format version is not recognized. MSBuild can only read solution files between versions 7.0 and 9.0, inclusive


I figured all I needed to do was to force NAnt to use the .NET 3.5 Framework, so I added a line to the NAnt Script as follows:


<property name="nant.settings.currentframework" value="net-3.5"/>



However, then I got the following message

Target framework could not be changed. "net-3.5" is not a valid framework identifier. Valid values are: net-2.0, net-1.1.


So after a little digging on the net, I found that the version of NAnt we were using (0.84) did not yet handle the .NET 3.5 Framework. However there is a Beta version of the 0.86 NAnt tool available, so I downloaded that and installed it to my build server (copied it actually, there is no installer)

The I got the following error

Object reference not set to an instance of an object.


Hmmm. I cleaned up my build server, removing all previous versions of NANT, copied the files from 0.86 back into my NAnt directory, and tried again. Same problem.

Next, I downloaded the latest nightly build from SourceForge and tried my build again


Invalid element . Unknown task or datatype.


WTF!? Where the hell did my msbuild task go? Panic strikes. OMG, I just killed our build server! My boss is gonna kill me!

After a little digging, it turns out that when I cleaned up my server, I'd also removed the latest NAntContrib bin directory, where the msbuild task comes from. So I downloaded the latest NAntContrib package, copied the files to the NAnt directory.

And.... PRESTO! Project building! Well at least it would have been if I hadn't had a path error in my NUnit task. A quick fix, recheck the file into (gah) Visual Source Safe, and a green build light made me feel all warm and fuzzy inside.

All that took me a good chunk of my morning. It would be really nice if SourceForge offered a standard install with the latest NAnt and NAntContrib packaged together for us lazy asses who don't really have 4 hours to blow on getting a build running.

I think that's the number one argument against build servers is how long it takes to set them up. The time they end up saving is tremendous, but you have to be really determined to get it done, and then cross your fingers that you have accurately documented every step in the setup so it doesn't take so freaking long the next time.

My next task... upgrade CruiseControl 1.2.1 to 1.3, though I have heard rumors circulating about CCNET 1.4. Maybe I'll wait till then, and get some real work done.

ThinqLinq

I'm not exactly reading a book a week, but it is close. This morning, I finished the Pro-LINQ C#2008 book by Joseph Rattz. A lot of it is a reference manaul which I will be frequently breaking open while coding LINQ, but I definitely feel more comfortable in using LINQ now than I was a couple of weeks ago. I haven't gotten to the point of ThinqLinq (I just coined that term, I should copyright it.) *Note: I just found a website that already uses the term ThinqLinq. Guess I won't be getting rich on that one.

I'll define ThinqLinq as being able to code database queries directly in LINQ without first thinking in SQL. I think it actually works to my advantage that I am relatively new to SQL Server, with most of my database experience being in Informix and Oracle, so the transition to SQL Server is being done at the same time as I am learning LINQ.

One of the dangers of learning a new language (and that's what LINQ is to me), is that you can become proficient at the language, but it may take a while to learn how to architect an application properly using it. LINQ removes months of development effort for the data access layer, and that speed may encourage bad design. Not because you want to make a bad design, but because you aren't in the code long enough during the development cycle to know it is bad design.

To me, bad design has two definable results:
1. Poor performance. In some cases this is obvious right away,in some cases it isn't until much further down the road when you see your database load go up unexpectedly as your app reaches critical mass

2. High code maintenance costs. The idea behind a good design should be that future changes should take much less time than the original effort. But if your original effort on an application was less than a day, is there really a long term payback of spending three weeks doing a huge design effort to extract ten lines of code into another class? Purists will say yes, always. Realists, working with real deadlines and real customers will say it depends.

I think what it comes to is that only experience with a new language will tell you what is and what isn't a good design. Start simple. Don't try to out guess the code or the user. Prioritize optimization based on the critical nature of the code and the size of the project. As you learn a better technique, keep a list of places where you may want to refactor existing code using that technique, and check your list frequently to see if any of those tips apply to what you are doing.

Here are my current best practices for LINQ
1. Don't try to write a best practice until you tried something at least twice

2. Never alter the autogenerated code created by SQL-Metal. Use the partial classes instead

3. Always implement your data relationships on your database. If it doesn't have any, you will save time by going inand adding them as you need them in your LINQ applications. Undoubtedly you will need them later, and the foreign key relation ships defined on the database make it easier for new developers to navigate through a large database.

4. Don't try to do everything in one query. If you need to break things out into more than one query for readability or speed, do it. Even if it seems that multiple queries will result in a lot of extra database traffic, give it a try. You may be suprised at how much better the split queries acually perform, especially if you can cache the results of one of the queries.

5. NUnit is your friend. Write lots of test cases. Write them before you write your LINQ code. You'll need every case you can get your hands on.

My next LINQ goal is to understand how it fits in with tools like SubSonic and the Entity Framework, but for now, I'm just going to focus on mastering LINQ, and learning to ThinqLinq

The next book on my reading list is Programming SQL Server 2005. I need to brush up on the SQL Server basics, and fill in some of my knowledge gaps. I don't think there will be anything earth shattering in it, just a lot of 'Oh, that's cool' things that I could never have done in Informix.

Monday, March 3, 2008

Log4Net in VS2005 Web Application

Here's a link to a blog post on setting up log4net in a Visual Studio 2005 web application.

The real trick to this is to make sure that there is a global.asax file in the applciation and to add the following line to the Application_Start method


log4net.Config.XmlConfigurator.Configure();


The rest of the configuration is pretty straight forward.