473,320 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

advise for 3 tier OO beginner

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 ***
Sep 1 '06 #1
5 1187
There's a great book, which describes how to buidl a framework on which
to build business objects. Its called Expert C# 2005 Business Objects.
The framework is complete however, so if you like the functionality
you can actually build and use it.

The author is really good about making updates and supporting us on his
forum: http://forums.lhotka.net/forums/.

Andy

Mike P wrote:
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 ***
Sep 1 '06 #2
All I really want to know is whether I am going about this the right
way....can any experienced OO programmers help?
*** Sent via Developersdex http://www.developersdex.com ***
Sep 1 '06 #3
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 ***

Sep 1 '06 #4

Here is another option for the checking.

Again, the rule of thumb is that

"Exceptions should be EXCEPTIONAL"

If you expect the jobRoleKey to fail with any regular frequency, don't use
exceptions to handle this.
Use null or bool or some other kind of check.


You can test this, but you might have to pass the emp object in as a ref
value.

This is bool version.

public bool ValidateEmployeeJobRoleKey ( Employee emp , int jobRoleKey )

{

DataAccessLayer da = new DataAccessLayer();

bool blnValidJobRole = da.ValidateJobRole(jobRoleKey );

if (blnValidJobRole == true)
{
emp.JobRoleKeyProperty = jobRoleKey;
return true;
}

return false;
}
and then the code will look like this (if you use my null trick)

Employee e = new Employee ();

bool b = e.ValidateEmployeeJobRoleKey ( e , 1001 );
if(true==b)
{
Console.WriteLine ( e.JobRoleKeyProperty );
}
else
{
// we don't have a JobRoleKeyProperty !
}


Sep 1 '06 #5
I might look at using the specification design pattern (combined with the
notification design pattern) to do this.

There are two great "conceptual" books I will point you towards that will
help you alot.

Jimmy Nilsson Applying Domain Driven Design and Patterns
Eric Evans Domain Driven Design

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

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

Sep 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

77
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a...
3
by: Michael Crawford | last post by:
Hi, Where would one start for this type of application: I want to create an vb.net container application that has the gives the end user the ability to install and uninstall plugins or add-in...
25
by: David Noble | last post by:
We've been developing a web site using 3-tier architecture for 18 months now. There is a common layer that defines the classes - using XML schemas. The data layer acts as a wrapper to 3 databases...
8
by: John | last post by:
Hi I am making a simple contacts app involving clients, suppliers etc. Mostly it involves data entered by the user being saved in the db and then user able to search/browse the data. My question...
1
by: dgk | last post by:
How can I create a middle tier object using the standard databinding stuff? All the examples that I see in the books using the new 2005 controls just shows how to build stuff in two tiers. It...
11
by: iTISTIC | last post by:
Developing a new app and am trying to make this my first truly OOP/3-Tier app. I understand the principles of the presentation, business, and data layers. I do, however, have some questions on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.