You are mixing objects and object creation.
This is not a good idea.
http://sholliday.spaces.live.com/
6/5/2006 5/24/2006
Check my samples there (2.0 and 1.1 respectively)
You want to seperate your objects and their creation, by using a
EntityController or EntityManager (where "Entity" is anything like Emp,
Person, Department , etc)
You need a validator type thing.
public Employee ValidateEmployeeJobRoleKey ( Employee emp , int jobRoleKey )
{
DataAccessLayer da = new DataAccessLayer();
bool blnValidJobRole = da.ValidateJobRole(jobRoleKey );
if (blnValidJobRole == true)
{
emp.JobRoleKeyProperty = jobRoleKey;
return emp; // Notice, if the rule passes, I set the
appropriate property and return it. Simple, yet smooth.
}
else
{
throw new JobRoleKeyException();
//alot of times, I return a null here. Instead of using exceptions.
That way, the caller can check for null, and know if or if not it passed,
without the expense of throwing exception around.
}
}
and then the code will look like this (if you use my null trick)
Employee e = new Employee ();
e = e.ValidateEmployeeJobRoleKey ( e , 1001 );
if(null==e)
{
// I know it didn't work.
}
The null trick is good sometimes, sometimes its not. Don't lose sight of
the bigger issue.
The Employee object containing too much stuff in it.
This code should NOT be a part of the Employee class, because you want to
seperate your dal access, from the object itself.
Why? One reason is becasue your rules might change. Today you're checking
against a datastore.
2 years from now, you might have a range. (1000 to 2000 are valid).
You don't want to have to open up and change the Employee object, because a
data validation rule changes.
Else, you have to retest (or you're supposed to anyways) the entire Employee
object.
This way, you have isolated what might/can change, and how you test against
it.
"Mike P" <mi*******@gmail.comwrote in message
news:em**************@TK2MSFTNGP03.phx.gbl...
I'm new to writing separate classes for each 'business object' and I'm
wondering if I'm going about it the right way. If I have a property of
a business class that has to be within a range of values specified in a
database table, is this how I would go about it?
public int JobRoleKey
{
get
{
return this.intJobRoleKey;
}
set
{
DataAccessLayer da = new DataAccessLayer();
bool blnValidJobRole = da.ValidateJobRole(value);
if (blnValidJobRole == true)
{
this.intJobRoleKey = value;
}
else
{
throw new JobRoleKeyException();
}
}
}
Many thanks,
Mike
*** Sent via Developersdex http://www.developersdex.com ***