views:

560

answers:

3

In many cases in web applications you would need to return an error message, rather than simple true/false result. One would use exceptions for that, but I consider exceptions to be an indication of, you know, exceptional behavior. Let's take a Register() function for a class User for instance. If it was successful we can simply return true, but if something went wrong we would like to know what exactly: "passwords don't match", "e-mail is in invalid format" and so on (could be an error code instead of a message, doesn't matter).

The question is what is the best practice for returning such error messages in C# and .Net? There might be a struct ready, something like:

public struct Result {
    public bool OK;
    public string Message;
}

Or perhaps I should just use a parameter in the function? Like Register(out string Message).

Update. This pretty much describes everything I need: http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.aspx

+2  A: 

I think exceptions can and should be used here.

You can put your Register() call in a try/catch block therefore preventing application execution stop. In the catch block you will analyze what exactly got wrong and return an appropriate message.

Developer Art
+3  A: 

If it's for validation purposes I would recommend you the excellent Fluent Validation library.

Quote from the site:

using FluentValidation;

public class CustomerValidator: AbstractValidator<Customer> {
  public CustomerValidator() {
    RuleFor(customer => customer.Surname).NotEmpty();
    RuleFor(customer => customer.Forename).NotEmpty().WithMessage("Please specify a first name");
    RuleFor(customer => customer.Company).NotNull();
    RuleFor(customer => customer.Discount).NotEqual(0).When(customer => customer.HasDiscount);
    RuleFor(customer => customer.Address).Length(20, 250);
    RuleFor(customer => customer.Postcode).Must(BeAValidPostcode).WithMessage("Please specify a valid postcode");
  }

  private bool BeAValidPostcode(string postcode) {
    // custom postcode validating logic goes here
  }
}

Customer customer = new Customer();
CustomerValidator validator = new CustomerValidator();
ValidationResult results = validator.Validate(customer);

bool validationSucceeded = results.IsValid;
IList<ValidationFailure> failures = results.Errors;
Darin Dimitrov
This is great, but it's such a huge overhead when all you need to do is check a few conditions.Considering this link: http://richarddingwall.name/2009/08/19/asp-net-mvc-tdd-and-fluent-validation/ , 6 classes perform something that really should be done in 1 or 2.
HeavyWave
In the example I provided in my post there's a single class: CustomerValidator which allows you to keep your validation logic separated. Then all you do is use this class. In the link you provided there are six classes because there are six different things you are accomplishing: MVC controller, Validation, Repostory and the accompanying unit tests, which looks perfectly fine to me.
Darin Dimitrov
A: 

Throwing exceptions creates CPU utilization while adding all call stack and setting other fields of exception class.

I prefer not to throw Exceptions. If it is really casual error like say Argument is null,i would go for struct with an ExceptionMessage field with it.

But it is better to type-value check before calling a method and expecting primitive types to return from the method since you need a boolean value return to.

Emrah GOZCU
-1: Exceptions do not generate I/O.
John Saunders
Yes, i am wrong it is typo. I meant CPU. I was struggling to create best execution plan for my sql query.But, it would rather be helpfull if you simply edit it instead some magical down point :)
Emrah GOZCU
-1: I can't downvote, I need 100 =(.
bloparod
@bloparod, instead giving a downvote to help yourself to feel better, you may kindly write down some information why i am wrong than this can help me and everyone that reads that post. This was wrong beacuse i made typo error, John helped me to fix it. What is your cause? Tell me something and i will downvote my self friend. It has been almost a year, but whatever.
Emrah GOZCU
@bloparod, You can check some info at http://onjava.com/pub/a/onjava/2003/11/19/exceptions.html about exception my friend if you need some detailed information.
Emrah GOZCU