How to stay dry with validation in domain objects and services versus validation in the user interface layer

I searched for answers and even asked a few questions on this question, but so far I have not found the right answer. How do I show validation methods in my POCO domain objects and services at the user interface level? I am currently using web forms.

For example, I have the following domain object:

class Person
{
    public string Name { get; set; }
    public string Email { get; set; }

    public bool IsValidEmail(string email) {}
    public bool IsValidName(string name) {}

    public bool IsValidPerson()
    {
        if (IsValidEmail(Email) && IsValidName(Name)) { return true; }
        return false;
    }
}

and domain service:

class PersonService
{

    private Person person;
    private PersonRepository pRepo;

    public PersonService()
    {
        person = new Person();
        pRepo = new PersonRepository();
    }

    public AddPerson(Person p)
    {
        if (p.IsValidEmail(p.Email) && p.IsValidName(p.Name) && !DoesEmailExistInDatabase(p.Email))
        { pRepo.Save(p); }
        else
        { throw new ArgumentException(); }
    }

    public GetPersonByEmail(string email)
    {
        if (person.IsValidEmail(Email))
        { pRepo.GetByEmail(email)); }
        else
        { throw new ArgumentException(); }
    }

    public bool DoesEmailExistInDatabase(string email) { //code if exists.. }
}

and the UI / Codebehind layer:

Receive person by email

string emailInput = EmailTextBox.Text;

PersonService pService = new PersonService();
Person p = new Person();

if(p.IsValidEmail(emailInput))
{
    Person myPerson = pService.GetPersonByEmail(emailInput);
}
else
{
    //give user error here...
}
  • Is it right to create separate verification methods for each property of a domain object that may need verification?

  • Should these methods in the domain object and service be static, so I don’t need to instantiate the person just for verification?

  • Person , , ( , , POCO )?

4. ?

+3
2

Re # 1 - ( ) , "", .

Re # 2 - .

Re # 3 - , , - , / , , ?

, , " " , . № 4...

Re # 4 - , - / , , ; - , , , .

, ( ), - , () . , , ( ). , .

, : " ", , , .

"" , - . , , , , "" ValidateGenericEmail(), , ValidateCorporateEmail(). , , .

, , , , .

+2

. , , ( , ?). , ?   , . , , .

0

All Articles