Home
About
Contact
Tuesday, June 23, 2009

Recently I worked with a colleague on some code using decimal.TryParse() and tried to find a better way of using it. Specifically I had a string value representing a currency amount entered by a user in a web form. The amount needed more than just normal validation, so I needed to do some stuff with it.

Here's the code I started with:

public void SomeMethod(string userEnteredAmount)
{
    decimal amount;
    if(decimal.TryParse(userEnteredAmount, out amount) {
        //Do some stuff with amount
    }
    else {
        //Show message to user about invalid amount
    }
}

I've recently found many cases where I find the use of Value Objects to be very applicable. I found this case to be a particularly interesting example. Anyway, I created a Value Object called Amount to abstract away the TryParse stuff. Below is the class I created.

public class Amount
{
    private readonly decimal _value;
    private readonly bool _isValid;
    public Amount(string amount)
    {
        _isValid = decimal.TryParse(amount, out _value);
    }
    public decimal Value
    {
        get { return _value; }
    }
    public bool IsValid
    {
        get { return _isValid; }
    }
}

See how clean it got? At least I think so. The important part though is that it's usage is so much easier to understand and read:

var amount = new Amount(userEnteredAmount);
if(amount.IsValid) {
    //Do some stuff with amount.Value
}
else {
    //Show message to user about invalid amount
}

It's more code, but cleaner. Or did I say that already? :-) And of course it's reusable other places where amount has a meaning.

.Net | CSharp | DDD | Patterns
Tuesday, June 23, 2009 3:12:05 PM (W. Europe Daylight Time, UTC+02:00)
Sweet! I will steal that pattern.
Tuesday, June 23, 2009 6:22:06 PM (W. Europe Daylight Time, UTC+02:00)
Very nice way to do it
Tuesday, June 23, 2009 6:32:25 PM (W. Europe Daylight Time, UTC+02:00)
+1 on stealing that code. Simple but very nice.
Thursday, July 23, 2009 3:24:46 PM (W. Europe Daylight Time, UTC+02:00)
Nice!

I'm exploring the same ideas these days, but want to look into using true value objects; structs. I'm not sure if there is a point to doing it with structs, other than they are in fact value objects - and the difference will become clearer.
Thursday, July 23, 2009 3:32:45 PM (W. Europe Daylight Time, UTC+02:00)
Einar,

When you say "true value objects" do you mean it in the sense of DDD? I'm not sure I would implement all my value objects as structs. For one you loose polymorphism since structs are value types, secondly I usually find structs to be useful when I need a lot of that type created (like 1500 instances in a collection or something).
Monday, July 27, 2009 1:52:25 PM (W. Europe Daylight Time, UTC+02:00)
Not all objects would suit this, but in quite a few domains you have value objects that are truely value objects that can be discarded and used like structs would have.

I need to explore it a little bit more before I have a proper conclusion. :)

Late answer - I'm in a holiday mode these days, not so trigger-happy on the keys. One more week and I'm back in the rythm..
Friday, August 14, 2009 9:07:58 AM (W. Europe Daylight Time, UTC+02:00)
Nice work Jon! This must be the most elegant solution I have seen so far!
Paul
Friday, August 14, 2009 9:35:53 AM (W. Europe Daylight Time, UTC+02:00)

typo?

public void Amount(string amount)
{
_isValid = decimal.TryParse(amount, out _value);
}
Paul
Friday, August 14, 2009 9:50:30 AM (W. Europe Daylight Time, UTC+02:00)
Paul,

Yes! That's what happens when you code i Live Writer :-) Thanks for letting me know. I've fixed it.
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, strong, sub, sup, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Live Comment Preview
RSS RSS - Comments Twitter LinkedIn
         
SEARCH
 
 
         
TOP POSTS
   
         
NAVIGATION
   
         
CATEGORIES
  .Net (61) ADFS (3) Agile (31) Ajax (5) Architecture (20) Articles (1) ASP.NET (7) ASP.NET-MVC (1) Blogging (12) Books (2) BPEL (1) CleanCode (1) CloudComputing (7) Community (5) CSharp (11) DasBlog (5) Database (2) DDD (5) Deployment (17) DSL (1) Events (38) ExtremeProgramming (6) Fun (6) Gadgets (4) IIS (10) InfoQ (4) Java (2) Kanban (1) Lean (3) Linq (2) MemoryLeaks (5) Microsoft (37) MVC (1) NDC (2) NNUG (37) Other (10) Patterns (9) Performance (3) Scrum (17) Security (7) ServiceBus (1) Silverlight (4) Software (19) TeamManagement (12) TechEd (7) Testing (5) Tools (25) TvGuide (1) Vista (15) VisualStudio (16) WCF (8) Web (16) WebDeploy (2) WIF (3) Windows (10) WiX (9) Work (18) Workflow (3)  
         
ARCHIVE
   
         
BLOGROLL
   
         
ON THIS PAGE...