jon torresdal

  • About
  • Contact

    Pricing For Azure

    19. July 2009

    This escaped my radar. The pricing for Azure is now public. From Mary-Jo Foley’s blog:

    Windows Azure:

    Compute @  $0.12 / hour
    Storage @ $0.15 / GB 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 across all three services will be charged at $0.10 in / $0.15 out / GB

    It’s quite similar to Amazon’s prices. Be interesting to see how long that lasts…

    Start Using More Value Objects!

    19. July 2009

    At QCon London 2009, Dan Bergh Johnsson had a talk on the DDD track about how to make use of Value Objects (VO’s) (The Power of Value – Power Use of Value Objects in Domain Driven Design (PDF)). In his talk he took a piece of code and refactored it into a better piece of code by using VO’s. By doing this Dan showed some really good concepts of using VO’s. Even though I knew about the concept of VO’s before his talk, he made me realize many new scenarios where they are applicable. And more importantly that it is not limited to DDD.

    So what is a VO anyway? Is it related to value type in .NET?

    To answer the last question first: The real answer is no, but the way I see it there is a relation. A value type in .NET is everything that is not an object. Typically simple types like int, char, double, but also enums and structs. The relation is that often you find yourself creating VO’s where value types was previously used. It is however not limited to value types.

    Now to the first question. I first came across VO’s after reading Evans DDD book (commonly known as the blue book). In DDD a Value Object is an object without an Id and are usually immutable. In examples of VO’s, Address is commonly used, which I don’t find particularly useful. I think Address way too often has an Id and people have a hard time relating to that as a Value Object. I find the Amount example I gave in an earlier post to a better example or the one below illustrated as an interface (borrowed from Dan):

    public interface IPhoneNumber {
    
      bool IsValid {get;}
    
      string Number {get;}
    
      string AreaCode {get;}
    
    }

    How many times have you used the concept of a phone number in your code? Like mobile, fax, fixed etc… How often have you kept it as a string? How much code do you have for doing different types of validation, getting area code etc? Do you have it all in one place or scattered around in different places? Other examples are Money, ZipCode etc… These are great candidates for Value Objects.

    Now that we know what Value Objects are, what is so powerful about them and why should I use them more? Great question :-) Below is my three reasons:

    1. Code readability – I find that using Value Objects greatly improve the overall code readability and eliminates comments. Just like good names on methods and properties does.
    2. DRY (Don’t Repeat Yourself) – By having a Value Object for PhoneNumber, you avoid writing code to handle specific cases around phone numbers and you reuse them wherever the concept of a phone number is used.
    3. Helps people getting back into OO thinking – We who have been living in the Microsoft .NET world for a long time have been thought to think of programming from a data/database perspective and not object oriented programming as we once learned it. In other words: think of behavior instead of data.

    When I started to use VO’s, I soon discovered I used them all over. They really add great value to my code. As an example, lets implement the IPhoneNumber above:

    public class PhoneNumber : IPhoneNumber
    
    {
    
        private readonly string _phoneNumber;
    
    
    
        public PhoneNumber(string phoneNumber)
    
        {
    
            _phoneNumber = phoneNumber;
    
        }
    
    
    
        public bool IsValid
    
        {
    
            get { return Regex.IsMatch(_phoneNumber, @"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$"); }
    
        }
    
    
    
        public string Number
    
        {
    
            get { return _phoneNumber; }
    
        }
    
    
    
        public string AreaCode
    
        {
    
            get
    
            {
    
                return Regex.Match(_phoneNumber, @"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})").ToString();
    
            }
    
        }
    
    }

    The above code handles US phone numbers. To be used in a production system there should probably be even more validation in the regexp’s, but I hope the example gives you and idea of what I’m thinking.

    Different Ways of Refactoring Switch/Case

    18. July 2009

    One of the code smells I see most often in C# and other OO languages is the misuse of switch/case statements. When I was a young Jedi (some might argue I still am :-)) I didn’t see anything wrong with using the switch statement. Actually there still are a few cases where it’s still applicable, but most usages I come across should have been solved differently. So why is this? What is so bad with the switch statement?

    As Dan North tweeted me when I asked for the perfect refactoring solution for switch statements :-) :

    A switch is often a sign of behaviour that should live elsewhere – so where should the code in the cases live?

    Functions should only do one thing, the one thing which they state. Like CalculateInterest and ConvertDateToString should do exactly that; calculate interest and convert a date to string. Since switch always reside inside functions it almost always violates that principle.

    Let’s look at what Uncle Bob (Robert C. Martin) says in his Clean Code book about switch statements:

    Even a switch statement with only two cases is larger than I’d like a single block or function to be. It’s also hard to make a switch statement that does one thing. By their nature, switch statements always do N things.

    My answer to Dan’s question above was using polymorphism. The cases should live in their own classes. By doing that you don’t have to modify existing code when introducing new behavior, you just create a new state (read class). Btw Dan is really good at asking questions to questions where you end up answering your own question :-)

    Enough with the coding theory. Below is a method using a switch statement for getting messages from different systems. Typically these are systems that another system depends on (the system containing the below code). The messages might be if the system is available or not, or if there are any maintenance planned.

    public string GetSystemMessage(System system)
    
    {
    
        switch (system)
    
        {
    
            case System.System1:
    
                return GetSystem1Message();
    
            case System.System2:
    
                return GetSystem2Message();
    
            case System.System3:
    
                return GetSystem3Message();
    
            case System.System4:
    
                return GetSystem4Message();
    
            default:
    
                throw new ApplicationException("System not found");
    
        }
    
    }

    This is actually quite a good switch. It uses return instead of break and it only has one line per case. If you can’t avoid writing a switch, this is how you should do it.

    Now let’s pull it apart and make it better and extendable. Time for refactoring.

    Polymorphism by using Strategy Template Pattern

    Here’s my code using the Strategy pattern:

    public interface ISystemStrategy
    
    {
    
        string GetMessage();
    
    }
    
    
    
    public class System1Strategy : ISystemStrategy
    
    {
    
        public string GetMessage()
    
        {
    
            return "System1";
    
        }
    
    }
    
    
    
    public class System2Strategy : ISystemStrategy
    
    {
    
        public string GetMessage()
    
        {
    
            return "System2";
    
        }
    
    }
    
    
    
    public class System3Strategy : ISystemStrategy
    
    {
    
        public string GetMessage()
    
        {
    
            return "System3";
    
        }
    
    }
    
    
    
    public class System4Strategy : ISystemStrategy
    
    {
    
        public string GetMessage()
    
        {
    
            return "System4";
    
        }
    
    }
    
    
    
    public class SystemMessenger
    
    {
    
        private readonly ISystemStrategy _system;
    
    
    
        public SystemMessenger(ISystemStrategy system)
    
        {
    
            _system = system;
    
        }
    
    
    
        public string GetMessage()
    
        {
    
            return _system.GetMessage();
    
        }
    
    {
    
    }

    First I define an interface to be used by the different systems (ISystemStrategy) and next each system (1-4) implements that interface, resulting in a class for each case in the switch.In addition I have a class called SystemMessenger that is responsible for accessing the system and get a message.

    The switch can now be replaced by this code:

    public string GetSystemMessage(ISystemStrategy system)
    
    {
    
        return new SystemMessenger(system).GetMessage();
    
    }

    Note that the "client" only relates to the SystemMessenger class and not to the different implementations of ISystemStrategy.

    The benefit of this code might not be obvious. Actually it might give the impression that this was a lot of code for almost nothing. Well, don’t be fooled. The strongest aspect of the above solution is that you don’t have to change the GetSystemMessage later when/if a new system is added, you just implement a new class.

    To prove the above, here’s what I need to do to introduce a new system:

    public class System5 : ISystemStrategy
    
        public string GetMessage()
    
        {
    
            return "System5";
    
        }
    
    }

    To use the new system, an instance of System5 will be sent into GetSystemMessage just as before, and SystemMessenger handles the rest as before. No code change!

    Dictionary

    Another refactoring option is using some type of lookup table. In my example I’ll use a dictionary together with the Func<TResult> delegate. However, I don’t recommend doing this unless you have a really good reason to do so. Stick with the Strategy pattern if you can.

    public class SystemMessengerDictionary
    
    {
    
        private readonly Dictionary<System, Func<string>> _systems;
    
    
    
        public SystemMessengerDictionary()
    
        {
    
            _systems = new Dictionary<System, Func<string>>
    
                               {
    
                                   {System.System1, GetSystem1Message},
    
                                   {System.System2, GetSystem2Message},
    
                                   {System.System3, GetSystem3Message},
    
                                   {System.System4, GetSystem4Message}
    
                               };
    
        }
    
    
    
        public string GetSystemMessage(System system)
    
        {
    
            return _systems[system]();
    
        }
    
    
    
        //GetSystemX Methods
    
        ...
    
    }
    
    
    

    I decided to show this example even if I don’t like it. The reason is that it is better than the switch. The worst about this in my eyes is that the enum is still there. I just hate enums! When reading the code they look like classes with interesting behavior, but then you discover they’re just glorified int’s :-|

    Conclusion

    Hopefully this example will help you understand why switch is something you should avoid and also give you an example of how it should be done. But before you consider doing any refactoring of existing code, consider a second question Dan asked (answered?) me:

    How would it evolve if you added a few more cases? And how likely are a few more cases?

    This is an important question, because you don’t want to refactor just for the sake of refactoring. Keep this in mind before making use of any of the suggested solutions above.

    WiX 3.0 Released

    18. July 2009

    A bit late announcement from me, but 4th of July WiX version 3.0 was declared stable. You can also find updated documentation for v.3.

    Regarding my focus on WiX lately I haven’t done much in WiX for a while. I was planning to add some more posts to my WiX tutorials, but currently this has not been my priority after I changed job. I used to work for an ISV where deployment was very important. Where I currently work we’re in full control of all servers we deploy to. I do however plan to do some stuff with WiX here as well, but that will have to be later.

  • Recent Posts

    • How ConDep came to life
    • Introducing ConDep
    • Lightning Talk: Why you shouldn’t track bugs
    • How Do We Track Bugs? Check In a Failing Test!
    • Stepping Down from NNUG Bergen, Still Chairman of NNUG National
  • Archives

    • March 2013
    • February 2013
    • November 2012
    • January 2012
    • June 2011
    • May 2011
    • September 2010
    • August 2010
    • June 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
  • Categories

    • .Net
    • ADFS
    • Agile
    • Ajax
    • Architecture
    • Articles
    • ASP.NET
    • ASP.NET-MVC
    • Blogging
    • Books
    • BPEL
    • CleanCode
    • CloudComputing
    • Community
    • ContinuousDelivery
    • ContinuousDeployment
    • CSharp
    • DasBlog
    • Database
    • DDD
    • Deployment
    • DevOps
    • DSL
    • Events
    • ExtremeProgramming
    • Fun
    • Gadgets
    • IIS
    • InfoQ
    • Java
    • Kanban
    • Lean
    • Linq
    • MemoryLeaks
    • Microsoft
    • MVC
    • NDC
    • NNUG
    • Other
    • Patterns
    • Performance
    • Scrum
    • Security
    • Silverlight
    • Software
    • TeamManagement
    • TechEd
    • Testing
    • Tools
    • TvGuide
    • Uncategorized
    • Vista
    • VisualStudio
    • WCF
    • Web
    • WebDeploy
    • WIF
    • Windows
    • WiX
    • Work
    • Workflow
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org

Tumblog WordPress Themes by Theme created by Obox