Is it any point to make further validate in the AnimalManager class before adding to the collection

T

Tony Johansson

Hello!

The GUI class is fully responsible for all input from the user.The GUI input
is a registration of a animal
with name, age, animal category, type of animal and what kind of food this
animl like and so on.
There is a button Add in the GUI that make all validation to make sure that
all input is correct.
When hitting this Add button another methodb Add in the AnimalManager class
is called along with with all the input arguments.
Below is the AnimalManager class listed.

Now to my question is it any point to make any kind of validation before
adding the animal object to the animals collection

class AnimalManager
{
const int IDStart = 10;
private List<Animal> animals = new List<Animal>();
private CategoryType category;
private AnimalType animal;
private string name;
private double age;
private GenderType gender;
private HousingType housing;
private EaterType eater;
private bool aggressive;
private List<string> foodMenuList;
private string ID;

public AnimalManager()
{}

public void Add(CategoryType category, AnimalType animal, string
name, double age, GenderType gender,
HousingType housing, EaterType eater, bool aggressive,
List<string> foodMenuList)
{
this.category = category;
this.animal = animal;
this.name = name;
this.age = age;
this.gender = gender;
this.housing = housing;
this.eater = eater;;
this.aggressive = aggressive;
this.foodMenuList = foodMenuList;

ID = GenerateID(category,animal);

switch (category)
{
case CategoryType.Mammal:
IAnimal animalObj = MammalFactory.CreateMammal(animal, name,
ID, age, gender, housing, eater, false, foodMenuList);
animals.Add((Animal)animalObj);
break;

case CategoryType.Bird:
break;

case CategoryType.Insect:
break;

case CategoryType.Marine :
break;

case CategoryType.Reptile :
break;
}
}

private string GenerateID(CategoryType category, AnimalType animal)
{
int count = IDStart + animals.Count;
return category.ToString().Substring(0, 3) +
animal.ToString().Substring(0, 3) + count;
}
}

//Tony
 
A

Arne Vajhøj

The GUI class is fully responsible for all input from the user.The GUI input
is a registration of a animal
with name, age, animal category, type of animal and what kind of food this
animl like and so on.
There is a button Add in the GUI that make all validation to make sure that
all input is correct.
When hitting this Add button another methodb Add in the AnimalManager class
is called along with with all the input arguments.
Below is the AnimalManager class listed.

Now to my question is it any point to make any kind of validation before
adding the animal object to the animals collection

class AnimalManager
{
const int IDStart = 10;
private List<Animal> animals = new List<Animal>();
private CategoryType category;
private AnimalType animal;
private string name;
private double age;
private GenderType gender;
private HousingType housing;
private EaterType eater;
private bool aggressive;
private List<string> foodMenuList;
private string ID;

public AnimalManager()
{}

public void Add(CategoryType category, AnimalType animal, string
name, double age, GenderType gender,
HousingType housing, EaterType eater, bool aggressive,
List<string> foodMenuList)
{
this.category = category;
this.animal = animal;
this.name = name;
this.age = age;
this.gender = gender;
this.housing = housing;
this.eater = eater;;
this.aggressive = aggressive;
this.foodMenuList = foodMenuList;

ID = GenerateID(category,animal);

switch (category)
{
case CategoryType.Mammal:
IAnimal animalObj = MammalFactory.CreateMammal(animal, name,
ID, age, gender, housing, eater, false, foodMenuList);
animals.Add((Animal)animalObj);
break;

case CategoryType.Bird:
break;

case CategoryType.Insect:
break;

case CategoryType.Marine :
break;

case CategoryType.Reptile :
break;
}
}

private string GenerateID(CategoryType category, AnimalType animal)
{
int count = IDStart + animals.Count;
return category.ToString().Substring(0, 3) +
animal.ToString().Substring(0, 3) + count;
}
}

You should do some format validation in the UI as well. You are
already doing that implicit, because a parse of "AB" to double for age
would fail. But being more explicit will usually enable you to
give better error messages to the user.

With this specific code, then you should check data in either
the switch or the factories (the latter making most sense to me).

But if the Add method took a complete Animal, then it would be
outside and I would prefer that type of Add method.

And maybe a factory factory would be nice.

And WTF is it about the AnimalManager having Animal fields??

Arne
 
T

Tony Johansson

Arne Vajhøj said:
You should do some format validation in the UI as well. You are
already doing that implicit, because a parse of "AB" to double for age
would fail. But being more explicit will usually enable you to
give better error messages to the user.

With this specific code, then you should check data in either
the switch or the factories (the latter making most sense to me).

But if the Add method took a complete Animal, then it would be
outside and I would prefer that type of Add method.

And maybe a factory factory would be nice.

And WTF is it about the AnimalManager having Animal fields??

Arne

I don't understand I mean when I activate the Add button I know that the
data is valid so I would not need any kind of
validation in the animalManager class. I can't see any scenario when tha
passed data to animalManager could give any error.
What kind of validation would you prefer in this specific case ?

//Tony
 
A

Arne Vajhøj

I don't understand I mean when I activate the Add button I know that the
data is valid so I would not need any kind of
validation in the animalManager class. I can't see any scenario when tha
passed data to animalManager could give any error.
What kind of validation would you prefer in this specific case ?

Validations that relate to the logical consistency of data.

The UI may verify that age is a valid double. Including handling
English versus local culture.

But the classes further down may impose additional checks
on the values. Like rejecting an age over 1000.

Maybe you would want to detect inconsistencies between
eater type and food menu list as well.

Whether you should reject negative age in UI or further
down can be debated.

Arne
 
T

Tony Johansson

Arne Vajhøj said:
Validations that relate to the logical consistency of data.

The UI may verify that age is a valid double. Including handling
English versus local culture.

But the classes further down may impose additional checks
on the values. Like rejecting an age over 1000.

Maybe you would want to detect inconsistencies between
eater type and food menu list as well.

Whether you should reject negative age in UI or further
down can be debated.

Arne

What kind of rules or validation check should be considered to belong to the
UI and what kind of rule(business rules) is to be stored in the
animalmanager class. Below is the validation method that exist in the UI and
it would seem strange if some of these checks should be considered as
business rules and be stored further down in the animalmanager class ?
As you can see I use TryParse to check that an age can't be negative or
zero. Zero could have been valid for newborn but it seems strange to have an
age of zero.

Give me a comment about this.

private bool ValidateInput()
{
int age;
txtName.Text = txtName.Text.Trim();

if (!int.TryParse(txtAge.Text, out age) || age <= 0)
{
MessageBox.Show("Age is mandatory and numeric");
return false;
}

if (txtName.Text == string.Empty)
{
MessageBox.Show("Name is mandatory input");
return false;
}

if (lstCategory.SelectedIndex == -1)
{
MessageBox.Show("You must select a category");
return false;
}

if (lstAnimalType.SelectedIndex == -1)
{
MessageBox.Show("You must select an animalType");
return false;
}

if (lstEaterType.SelectedIndex == -1)
{
MessageBox.Show("You must select which type of EaterType this
animal is");
return false;
}

if (lstGender.SelectedIndex == -1)
{
MessageBox.Show("You must select what gener it is");
return false;
}

if (checkedListBox.CheckedItems.Count == 0)
{
MessageBox.Show("You must select at least one food Item that is
suitable for selected animal");
return false;
}

return true;
}
 
A

Arne Vajhøj

What kind of rules or validation check should be considered to belong to the
UI and what kind of rule(business rules) is to be stored in the
animalmanager class.

I would suggest that you:
* put the format validations in the UI. Examples: verify that the
field is indeed a valid integer, verify that a field is filled out.
* put real business rules in classes further down. Examples: check
whether an age is realistic, check that a zip code is an existing
zip code.
Below is the validation method that exist in the UI and
it would seem strange if some of these checks should be considered as
business rules and be stored further down in the animalmanager class ?
As you can see I use TryParse to check that an age can't be negative or
zero. Zero could have been valid for newborn but it seems strange to have an
age of zero.

0 is a valid age.
Give me a comment about this.

private bool ValidateInput()
{
int age;
txtName.Text = txtName.Text.Trim();

if (!int.TryParse(txtAge.Text, out age) || age<= 0)
{
MessageBox.Show("Age is mandatory and numeric");
return false;
}

if (txtName.Text == string.Empty)
{
MessageBox.Show("Name is mandatory input");
return false;
}

if (lstCategory.SelectedIndex == -1)
{
MessageBox.Show("You must select a category");
return false;
}

if (lstAnimalType.SelectedIndex == -1)
{
MessageBox.Show("You must select an animalType");
return false;
}

if (lstEaterType.SelectedIndex == -1)
{
MessageBox.Show("You must select which type of EaterType this
animal is");
return false;
}

if (lstGender.SelectedIndex == -1)
{
MessageBox.Show("You must select what gener it is");
return false;
}

if (checkedListBox.CheckedItems.Count == 0)
{
MessageBox.Show("You must select at least one food Item that is
suitable for selected animal");
return false;
}

return true;
}

I think the checks you do are fine.

I would be surprised in win forms does not provide some means
for having win forms code do some of this checking.

But then I am not a win forms expert.

Arne
 
A

Arne Vajhøj

I am maintenancing a solution right now, that has all the business rules
in the forms, Javascript, and in sprocs. It's a total nightmare of a
solution. I will be tasks to convert the solution form VS 2003 VB , VB
2005 and VB6 to VS 2010 C# and using MVP, service layer, BLL model and
DAL, to give the solution some consistency.
Business rules in the UI, pfft......

If they are true business rules, then it is not good to check
them in UI.

Business rules in the database is bit more blurred. I don't
like it either. But if the database is used by different systems
in multiple technologies, then it can be the only way of enforcing
rules a single place.

There are traditionally a lot of stored procedure usage in the
SQLServer and Oracle DB world.

Arne
 
T

Tony Johansson

Arne Vajhøj said:
I would suggest that you:
* put the format validations in the UI. Examples: verify that the
field is indeed a valid integer, verify that a field is filled out.
* put real business rules in classes further down. Examples: check
whether an age is realistic, check that a zip code is an existing
zip code.


0 is a valid age.


I think the checks you do are fine.

I would be surprised in win forms does not provide some means
for having win forms code do some of this checking.

But then I am not a win forms expert.

Arne
Where would you place a check of age ==0 in UI or business class here
AnimalManager class.
I would say UI

//Tony
 
A

Arne Vajhøj

Where would you place a check of age ==0 in UI or business class here
AnimalManager class.
I would say UI

First of all I don't consider 0 an invalid value for age, so I
would probably not check!

:)

If for whatever reason it is not valid in your context, then I would
say that you could argue all of UI, manager class or both. I don't have
any strong opinions on it. Pick what you prefer. Or throw a dice.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top