PC Review


Reply
Thread Tools Rate Thread

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

 
 
Tony Johansson
Guest
Posts: n/a
 
      30th Jan 2011
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


 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      30th Jan 2011
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??

Arne



 
Reply With Quote
 
Tony Johansson
Guest
Posts: n/a
 
      31st Jan 2011

"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??
>
> 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


 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      31st Jan 2011
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




 
Reply With Quote
 
Tony Johansson
Guest
Posts: n/a
 
      31st Jan 2011
"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;
}


 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      2nd Feb 2011
On 31-01-2011 06:46, Tony Johansson wrote:
> "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
>>>> 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.

>
> 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



 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      2nd Feb 2011
On 30-01-2011 22:31, Big Steel wrote:
> 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
 
Reply With Quote
 
Tony Johansson
Guest
Posts: n/a
 
      2nd Feb 2011

"Arne Vajhøj" <(E-Mail Removed)> skrev i meddelandet
news:4d48b688$0$23764$(E-Mail Removed)...
> On 31-01-2011 06:46, Tony Johansson wrote:
>> "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
>>>>> 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.

>>
>> 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
>
>

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

//Tony


 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      3rd Feb 2011
On 02-02-2011 05:34, Tony Johansson wrote:
> "Arne Vajhøj"<(E-Mail Removed)> skrev i meddelandet
> news:4d48b688$0$23764$(E-Mail Removed)...
>> On 31-01-2011 06:46, Tony Johansson wrote:
>>> 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.
>>

> 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




 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The importance of the right 'entry point' when adding your class inC# Form constructor raylopez99 Microsoft C# .NET 3 2nd Aug 2008 10:29 AM
Adding class object to collection repeats same object through collection? Erazmus Microsoft Excel Programming 2 17th Sep 2007 04:35 AM
The way to make a Class and a Collection static =?Utf-8?B?VGV0c3V5YSBPZ3VtYQ==?= Microsoft Excel Programming 1 28th Mar 2007 01:50 PM
Can't get collection to save when using collection of custom class as property of control in VS 2005 J.Edwards Microsoft Dot NET Compact Framework 0 10th Jan 2006 04:44 AM
Class X Creates Collection of Instances of Class Y. Y's need info from X Jim Frazer Microsoft C# .NET 3 16th Jun 2004 09:17 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:46 AM.