Thursday, June 05, 2008

In my quest to find a suitable generic update method for a generic LINQ Data Access class I have found a decent amount of code and even more theories (sans code).  Some good, some verbose, and a lot bad.  It seems that Extension Methods are a fan favorite for just about everything nowadays and I wasn't suprised to see Extension Methods being used for LINQ to SQL updates.

In reading about Extension Methods I have heard good and bad feedback.  The good usually involves the convenience of bypassing a utility class and the bad involving code readability / maintability.  Forgive me for I am the William Hung of LINQ as I have had no formal training in this arena, but IMHO Extension Methods are a pain-in-the-ass when ExtensionMethodA returns the result of ExtensionMethodB which depends on the output of ExtensionMethodC which ..... Here is an example of what I am talking about in pseudo-code:

public static bool Update<T>(this Table<T> table, T instance) where T: class
{
   try
   
{
      // do stuff here with parameters...
      
return table.Foo() > 0;
   }
   catch (Exception)
   {
      return false;
   }
}

public static int Foo<T>(this Table<T> table) where T: class
{
   return table.GetEnumerator().Bar();
}

public static int Bar<T>(this IEnumerator<T> e) where T : class
{
   return 1;

Horrible example yes and for that I apologize.  Hopefully you see where I am going with this and that if you are given a similar codebase it can be quite confusing.  When I first heard of extension methods I though they were for simple utility tasks such as outputting a decimal value as a dollar amount (again, doesn't REALLY warrant an extension method but...):

public static string PrintWithDollarSign(this decimal input)
{
   return input.ToString("c");
}

Simple. Effective. Here's how you would use:

Console.WriteLine(instance.Price.PrintWithDollarSign());

Cheap. No frills.  I could have just bypassed this extension method altogether but it provides a very "Hello World"'y introduction to extension methods for those of you not hip to them.  In my previous example if you had an object with a decimal property and the value was 5.00 the output would be $5.00. Easy.

Anyone care to chime in on extension methods relying on other extension methods? I'd love to get some feedback.

C# | Will
Thursday, June 05, 2008 6:19:17 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Wednesday, June 04, 2008

Now that I'm diggging a little deeper into my pet project web site, I'm learning how I really use the data. The Get() methods provided by .netTiers are great but I thought I would have more. I started looking at my database starting with the aspnet_Membership tables. I would expect unique indexes on something as obvious as the email column but it is not there. Same goes for other obvious columns in these microsoft-provided tables. So I add unique indexes to the membership tables. Of course, I do the same for my own custom tables and find that some of the text or varchar(max) have to be changed. No biggie but another step I have to take.

Now that the database is set, I need to regen the .netTiers templates, rerun the GrantUser stored proc, build the .netTiers stuff and copy it into the /bin of my project. Not hard work but just tedious to get each step completed and move on to the next one. All this so my .Get methods are consistent with my usage of the library.

All this so I can get my validators working - I really hate validators.

Wednesday, June 04, 2008 8:35:07 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [1]  | 
 Tuesday, June 03, 2008

Yesterday, I was reading the MSDN FLASH email newsletter and saw a listing for another SQL injection article. Since one of Wayne's sites was recently hacked, I thought I would read it. The article is clear and gives explicit steps to take. Since I had only done one of the three things (SQL stored procs), I thought I would mess with the last one: execute permissions only.

Wayne pointed me to a stored proc to grant permissions to a user. Since users and roles are one of those areas that after you have them set up correctly, you don't mess with them, I stumbled my way through creating a user. I ran the stored proc (which will have to be run after every .netTiers generation), modified my sql connection in web.config and tried my pet project site. I get an error about SELECT statements and permissions. .netTiers was supposed to be configured in web.config to use stored procs but apparently not. So I change the file, build, and still get the error. Someone, somewhere is caching something. Close everything done. Reopen. Change web.config and checkin with comment. Test again and it works.

This is a really bad way to find a problem. It's getting to the point that I need someone to check my security changes so this type of thing isn't missed.

Tuesday, June 03, 2008 7:00:10 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Sunday, June 01, 2008

Volume 14 of Make Magazine has an interesting article on free web sites - web site builders that also host the end resulting web site.

Sunday, June 01, 2008 6:52:45 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Thursday, May 29, 2008

Here is my latest article on 15seconds.com: http://www.15seconds.com/issue/080529.htm titled "Implementing the .netTiers Template Library as a .NET Website's Data Layer - Part I"
 

Thursday, May 29, 2008 6:22:45 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Wednesday, May 28, 2008

Now I'm trying to get all my blogs tied together. Most are DasBlog so no big deal only what is the endpoint? The documentation lists blogger.aspx off the root but the file isn't there. Apparently from the forums I'm not the only one with the issue. However, I thought I bet it is there virtual. So I enter that in the address bar and I get a response of web service methods available. Nice - why doesn't the documentation say the page isn't there but the dll will respond.

Wednesday, May 28, 2008 8:46:55 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Thursday, May 08, 2008

I get an email every friday about the new events for families on the weekend on www.neighborhood-kids.com. The design is great. I love it, but my eyes and mind do skip over things in the page just assuming they are pretty but useless. Then I noticed the clock at the top of the page was right about the time. Wow, that is cool. I'm going to have to pay closer attention to what is technically happening on web sites.

Thursday, May 08, 2008 8:14:25 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Friday, May 02, 2008

Living in the northwest is wonderful but when spring and summer start to show up (meaning the sun makes a regular appearance), I totally loose all motivation. My brother from England worked a summer for Boeing in Seattle a few years ago. He was shocked that people didn't do any work in the summer. I was always luck at Microsoft to not have a summer ship date. That would have totally sucked.

How can I think about writing code and the project when the sun is out just laughing at me, taunting me to come out.

Friday, May 02, 2008 8:08:07 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  |