DCSIMG
July 2008 - Posts - IronShay

July 2008 - Posts

Did you know?

The streets where the big high-tech companies are located:

Apple – 1 Infinite Loop St.
Microsoft - 1 Microsoft Way
Dell – 1 Dell Way
Sun Microsystems - 4150 Network Circle

 By the way, Google does not stand out here, they're located at the very un-geeky address of "1600 Amphitheatre Parkway"…

Shay.
Posted by shayf | with no comments
תגים:

My RSS feed should be OK now...

It turned out that some readers had problems with my rss feed.
My guess is that it has something to do with the space I had there in the middle of the URL (my mistake, sorry...).

Anyways, I've just changed it and it should be OK now - so subscribe and enjoy reading!

Thanks for notifing,
Shay. 

Posted by shayf | with no comments

Extension Methods + Attributes = A Whole New World!

Earlier this week, a friend of mine (read his great blog here) had tried to explain me why attributes were useful. We ended up with an extension method that reads attributes of enum values.
Today I have the time to really think about that and I've just realized - this has tons of potential!

Consider the following scenario - you have the following class:

public string GetCityName(long userID)

{

    //

    // Get data from some datasource...

    //

 

    return "Ney York City";

}

And now you're told to add authentication mechanism, so only authenticated users will be able to get the information. What can you do?
Let's create a very simple attribute:

public class AuthenticationRequiredAttribute : Attribute

{

 

}

We'll use this attribute as a flag - if it's there, authenticate. Otherwise, do not. This is why we don't really need any information there. We'll add the attribute to our method as well:

[AuthenticationRequired]

public string GetCityName(long userID)

{

    //

    // Get data from some datasource...

    //

 

    return "Ney York City";

}

Now we have an attributed method! what's next? Let's create an extension method (remember, we don't want to change the original code). This method will check if the method requires authentication and do that if needed:

public static string GetCityNameAuthenticated(this MyClass myClass, long userId)

{

    // Get the method information

    MethodInfo mi = typeof(MyClass).GetMethod("GetCityName");

 

    // Check if our attribute appears

    object[] atts = mi.GetCustomAttributes(typeof(AuthenticationRequiredAttribute), false);

    if (atts != null && atts.Length > 0)

    {

        // Authenticate

        if (!AuthenticateSomehow(userId))

        {

            // User is not authenticated, raise an exception

            throw new UnauthorizedAccessException();

        }

    }

 

    return myClass.GetCityName(userId);

}

So what have we achieved? we've added authentication to a method without changing its original code! (I know it sounds a bit like AOP, but who cares? it's cool!).

I want to show you another example, maybe even more useful than the last one. It's about expanding enum values data (many thanks to Adrian who's helped me with that). The idea is the same - add an attribute and an extension method that reads it:

public enum Cars

{

    [CountryOfOrigin(Name="Japan")]

    Mazda,

    [CountryOfOrigin(Name="Sweden")]

    Volvo,

    [CountryOfOrigin(Name="France")]

    Peugeot,

    [CountryOfOrigin(Name="USA")]

    Chrysler

}

 

public class CountryOfOriginAttribute : Attribute

{

    public string Name { get; set; }

}

 

public static class Extension

{

    public static string GetCountryOfOrigin(this Cars car)

    {

        object[] atts = typeof(Cars).GetField(car.ToString()).GetCustomAttributes(typeof(CountryOfOriginAttribute), false);

 

        if (atts != null && atts.Length > 0)

        {

            return (atts[0] as CountryOfOriginAttribute).Name;

        }

 

        return String.Empty;

    }

}

Shay.

Share this post : del.icio.us it! digg it! dotnetkicks it! technorati!