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;
}

Wednesday, February 27, 2008

PredicateBuilder

One of the really cool LINQ tools out there is PredicateBuilder by Joseph Albahari. http://www.albahari.com/nutshell/predicatebuilder.html

His book is next on my reading list, but I am already using PredicateBuilder as part of my current project with is a dynamic query engine for a billing systm.

The only difficulty I am having is passing in column names dynamically. The consensus is that this can't be done, but that doesn't stop me from trying (or at least whining about it)

Fun with LINQ

I've really been getting into LINQ these days. I'm currently reading Pro LINQ by Joseph Rattz. The writing style is not a good as the Pro ASP.NET 3.5 book I've previously raved about, but there is a chance that this book is just covering a muchmore difficult topic.

If you haven't ready about LINQ yet, do so. It is coming to a Visual Studio near you. Even if you don't use SQL Server, you can still use it for XML, DataSets and anything that inherits from IEnumerable.

But I do fear that the downside of LINQ is that Microsoft is about to lose a lot of developers because of it. As easy as it is, it is also very, very difficult. 75% of the developers will look at the syntax, the ridiculous numbers of < and >, and run back to hand coding SQL statements and returning plain old data readers. The problem lies in the fact that LINQ Expressions are extremely difficult to read. They're powerful, yes, but the numbers of function delegates and advanced concepts of the .NET Framework that LINQ requires you to know in order to go beyond the most simple queries is a little frustrating.

I'm sure that with time, we will grow use to reading Lambda expressions just like we read regular code, but I think a large percentage of developers will be scared off from the syntax and will never try. They will stay with their tried and true methods of data access, and count the days to retirement. I don't blame them. A new concept like LINQ or NHibernate or The Entity Framework comes along every year. I skipped a whole generate of these tools, and here I am, now working in LINQ.

Visual Studio Tip - TODO

Here's a tip that seems pretty obvious, but I've never used it before, and it's really useful, especially if you are going to hand code off to someone else to finish

Add a comment line to your code as follows

// TODO - Add a task to complete here

Then, go to the main menu bar, and select View - Task list. In the task list Window, there si a drop down list that says either Comments or User Tasks. Switch to comments. TADA! TODO!

Quick, easy, and professional! No more sticky notes or long emails about what is left to do. And it gets tracked with your source control so you can see who made the change and when. You are use isource control, right?

Wednesday, February 20, 2008

Recent Reading

I recently finished my first pass of ASP.NET 3.5 in C#2008 by MacDonald and Szpuszta. What an amazing book. I learned something new on just about every page. It's a pain in the but to lug this monster around every day (it's 1500 pages), but I learned so much that I consider it required reading for developers trying to get caught back up with technology if you've been stuck in a rut.

I've also almost completed reading Head First HTML with CSS and XHTML. The first half of this book wasn't anythingnew to me, but the second half really helped get me up to speed with CSS and since that is exactly what I am working in this week, it is really helping. It's a very quick read. I'm getting through about 200 pages a day (there are a lot of graphics). The Head First series isn't for everyone, and I thought I'd give it a try. It's great for doing catch up on technology, but I wouldn't use it to try to learn some huge new concept.

I'm debating what will be my next work topic to read about. It may be Code Complete 2 or perhaps Programming SQL Server 2005, or maybe a book specializing on LINQ.

I can't believe how much reading I am able to get done while riding the Sounder back and forth to work. It would have taken me a year to work through ASP.NET 3.5 at home. I'm definitely in the learning groove, and on track to get caught back up on the technology I need to do my job.

And the writing bug is hitting again. YAY!

CSS Path Lesson Learned

Situation:

A graphic I was trying to display in the main page header on a website was not showing up. The header was build via a CSS page. All other graphics on the page were working.

Solution: CSS background:image links must be specified with the path from the css file, not from the page using it. (DUH)