DennyDotNet

Awesome ASP.NET C# and other cool coding stuff

About the author

Denny Ferrassoli
Developer at Casting Networks. MCP / .NET
E-mail me Send mail
Add to Technorati Favorites

Recent posts

Recent comments

Authors

Categories

None


Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

ASP.NET MVC and SafeGet (my version of null dereferencing)

When working in ASP.NET MVC you sometimes pass a model to your view. In some cases your model is a few levels deep. For example:

Model.User.Address.ZipCode

(Yes this violates LoD but I don't like LoD anyways...)

Well I use models like this all the time and I constantly had to deal with properties being null when I was expecting a value. Since everything in a view gets rendered as string I decided to make a helper method that will allow me to pass anything in and return an empty string if the value was null (null dereferencing) instead of a NullReference Exception. Out came SafeGet. SafeGet is an extension method that applies to any object. It allows you to pass in the property, or value, you want to check as well as any constraints on the value. It's probably easier to see what I mean with code... check out the example:

 

SafeGet signature:


public static string SafeGet<T>( this T instance, Func<T, object> nonNullFunction, params Func<object>[] nullConstraint ) where T : class

 

Here is how it works:

In my view I wrap SafeGet around the properties I am not sure will have a value and include any constraints:


<%= Html.TextBox( "BirthDate", Model.User.SafeGet( o => o.BirthDate, () => DateTime.MinValue ) )%>

 

Yes, it uses Lambda expressions :)

 

The first param is "nonNullFunction" - this is where you pass in the value you want to check.

 

The second param is "nullConstraint" - Since I am working with a "DateTime" object (not "Nullable DateTime") I know DateTime must have a value but I also want it to consider the value as null IF the value of "o.DateTime" is "DateTime.MinValue" (basically saying: "if (o.DateTime == DateTime.MinValue) return null;"). You can have many nullConstraints if you need to check multiple values.

 

SafeGet then returns the value or "string.Empty" if the expression is null or it matches any constraints.

You could probably include another param as a default value to pass if it is null rather than always passing "string.Empty"

If you're using custom ViewModels you could certainly apply this at that level instead of in the view directly. I'm not sure if this breaks Seperation of Concerns by using it in a View.

 

Here's the SafeGet method:


        public static string SafeGet<T>(this T instance, Func<T, object> nonNullFunction, params Func<object>[] nullConstraint) where T : class
        {
            if(instance != null && nonNullFunction != null)
            {
                try
                {
                    var o = nonNullFunction(instance);

                    if(o == null) return string.Empty;

                    if(nullConstraint != null)
                    {
                        // Check each constraint to make sure it doesn't match
                        foreach(var constraint in nullConstraint)
                        {
                            if(constraint().Equals(o)) return string.Empty;
                        }
                    }

                    return o.ToString();
                }
                catch(NullReferenceException)
                {
                    return string.Empty;
                }
            }

            // in all other cases, return the default
            return string.Empty;
        }
    }

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Tags:
Posted by Denny on Thursday, July 16, 2009 7:00 AM
Permalink | Comments (1) | Post RSSRSS comment feed

Partial Azure Pricing Model Announced

Some information regarding the Azure pricing model has been released. Some good news is that the pricing is competitive to to Amazon's AWS service.

Windows Azure
Compute @  $0.12 / hour
Storage @ $0.15 / GB / month stored
Storage Transactions @ $0.01 / 10K

SQL Azure
Web Edition – Up to 1 GB relational database @ $9.99
Business Edition – Up to 10 GB relational database @ $99.99

.NET Services
Messages @ $0.15/100K message operations , including Service Bus messages and Access Control tokens

Bandwidth
All Services @ $0.10 in / $0.15 out per GB


Uptime availability is 99.95% and storage is 99.9%.

They will also offer more predictable pricing plans at PDC 09.

"While consumption based pricing provides great flexibility we have also heard it introduces a level of unpredictability and some customers prefer other options.  At launch we will share details of subscription offers that provide payment predictability and price discounts that reflect levels of usage commitment."

I'm hoping for some sort of low-cost developer pricing!

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Posted by Denny on Wednesday, July 15, 2009 6:39 AM
Permalink | Comments (0) | Post RSSRSS comment feed

PLINQO: Professional LINQ Objects

If you haven't heard of PLINQO yet then you're missing out. The CodeSmith guys have enhanced the hell out of LINQ to SQL, to name a few features:

  • Entity Clone
  • Entity Detach ** My favorite!
  • Many to Many relationships
  • Auditing
  • Rules
  • Performance Enhancements

In their own words:

  • "In the time that LINQ to SQL has been available, we have been identifying ways to make LINQ to SQL better. We have compiled all of those cool tips and tricks including new features into a set of CodeSmith templates. PLINQO opens the LINQ TO SQL black box giving you the ability to control your source code while adding many new features and enhancements. It's still LINQ to SQL, but better!"

I've been using PLINQO for over a month now and I really like it. It does require a CodeSmith license... BUT you can get a license free!!! Check out the details here.

There's also a great review of PLINQO by Kevin Lawry here. Enjoy!

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Tags: , ,
Posted by Denny on Sunday, July 05, 2009 3:46 AM
Permalink | Comments (0) | Post RSSRSS comment feed