"Arne Vajhøj" <(E-Mail Removed)> skrev i meddelandet
news:4d4617a1$0$23751$(E-Mail Removed)...
> On 30-01-2011 20:48, Tony Johansson wrote:
>> "Arne Vajhøj"<(E-Mail Removed)> skrev i meddelandet
>> news:4d45f377$0$23760$(E-Mail Removed)...
>>> On 30-01-2011 17:34, Tony Johansson wrote:
>>>> 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??
>>
>> 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
>
>
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;
}
|