1

Let's imagine I have a Service method which creates a customer:

public Customer CreateCustomer(string name)
{
  if (String.IsNullOrEmpty(name))
    throw new InvalidCustomerNameException(name);

  _customersRepository.Add(new Customer(name));
}

If this code is called from ASP.NET MVC I need to create ASP.NET MVC specific validation (to show to user which fields are filled incorrectly) before calling CreateCustomer. But in this case validation logic is duplicated. Is there any way to get rig of validation duplication?

1
  • Duplicate validation is not always a bad thing... Each layer should validate data according to their own needs and insights. Commented Jul 13, 2011 at 9:12

2 Answers 2

2

Take a look at FluentValidation framework.

You need just describe validation rules for certain model. It's integrative to ASP.NET MVC and may be used directly in code.

Sign up to request clarification or add additional context in comments.

Comments

0

Take a look at DataAnnotations. I think in CRUD based app this is all you need. But I personally hate littering my classes with attributes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.