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.

Thursday, February 28, 2008

.NET Extensions Anyone?

If you haven't seen .NET extension methods in action, they're pretty interesting. What are they, you ask? Let's say you have an existing .NET class provided by an external vendor. Let's say that vendor is Microsoft. You want to add a new function to, I don't know, lets say the string class. There's not a hope in hell that you'll get Microsoft to add it for you.

Of course, you could build your own string class, override one of the existing functions, or add a new one, but that seems like a lot of work. Here's an easy way to add a new function to the class, without having to use inheritance at all. And the best part is that within seconds, this class will show up in your Intellisense.

This function must be in a static class, and part of your own project / namespace or compiled into a DLL while you include in your project.

I wrote this function as I have been trying to find an efficient way to build dynamic Lambda queries for LINQTOSQL, but unfortunately, this won't work as 'FindWickedString' is not a function which can be interpreted by SQL Server, and the lambda function I am building executes on SQL Server. So I'll keep digging on the dynamic lambdas, but this is fun as an exercise. Wait. Did I say coding was fun. Egads. I meant work.




public enum WildcardMethod { Contains, StartsWith, EndsWith, Exact }

static class Extensions
{
public static bool FindWickedString(this string value, string checkValue, WildcardMethod method)
{
switch (method)
{
case WildcardMethod.Contains:
{
if (value.Contains(checkValue))
return true;
break;
}
case WildcardMethod.StartsWith:
{
if (value.StartsWith(checkValue))
return true;
break;
}
case WildcardMethod.EndsWith:
{
if (value.EndsWith(checkValue))
return true;
break;
}
case WildcardMethod.Exact:
{
if (value == checkValue)
return true;
break;
}

default:
break;
}

return false;
}