By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,338 Members | 1,350 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,338 IT Pros & Developers. It's quick & easy.

Class Design Question

P: n/a
What is the best solution for the following scenario:

I have a contacts database and I am creating the business layer obects to
access my data objects. (So the DB is invisible to the Business Layer
consumer)
Each contact can be of some type "Employee, Customer, Vendor, etc." . The
contact types are also stored in a table in the DB.

So in the business layer I am trying to a make a decision on how to model my
object flow. Here are some examples:
---------------------------------
Method One:
Contact objPerson = new Contact();
objPerson.Name = "Jose";
objPerson.Address = "666 somewhere street";
objPerson.City = "somecity";
objPerson.ContactTypeID = 3; //Each
objPerson.Save();

Problem:
If the ContactTypeID does not exist in the DB there will be an error thrown
by the DB. Also, when a user type objPerson.ContactType, I want it to
return a "ContactType" object.

--------------------------------
Method Two:
Contact objPerson = new Contact();
..... Set Generic Properties ...
ContactType objContactType = objAppFactory.GetContactType(3);
objPerson.ContactType = objContactType;

Problem:
This method allows me to throw an error as soon as the user tries to lookup
a nonexistent ContactTypeID before assigning to the Contact. However, I
have the extra round trip to the DB. Seems silly

-------------------------------

Method Three:
I can could provide to ways of setting the ContactType.
objPerson.ContactType = objApp.GetContactType(3);
or
objPerson.SetContactTypeID(3);

Problem:
Seems dumb

------------------------------

Method Four:
ContactType objEmployee = new ContactType();
Contact objPerson = new Contact();
objPerson.Name = "Jose";
objPerson.Address = "666 somewhere street";
objPerson.City = "somecity";
objEmployee.AddContact(objPerson);

Problem:
If the user attempts objPerson.Save() before it has been added to a
"ContactType" the DB will throw an error becuase it does not have a
"ContactTypeID". I can Validate the object before I attempt to save to the
DB by checking to see if the ContactTypeID property has been set; if not, I
can throw and exception with something like: "A Contact must be first added
to a ContactType before it can be saved to the DB".
Also, when I add the Contact to objEmployee, does the objEmployee.AddContact
method need to reoginize that objPerson has not been saved yet, and then
automatically save it to the DB?
Thanks. I am just trying to figure out a standard for accomplishing this so
I can replicate the concept into projects I am working on....
Josh
Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi,

If the only different among the different kinds of contacts is the type,
and the type is not dynamic I would suggest you to use an enum:
public enum ContactKind { Employee, Customer, Vendor }

then you can declare ContactType of that type:
public ContactKind ContactType{
get {}
set {}
}

By default the enum is of type int, so at load type you can cast it back
from an int :

contacttype = (ContactKind )( Convert.ToInt32( reader["ContactKind "]));
//reading from a datareader

and at save you can do the opposite:
com.Parameters.Add("@ContactKind ", SqlDbType.Int).Value =
(int)this.contacttype ;
Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"msnews.microsoft.com" <aa***@yahoo.com> wrote in message
news:ec**************@tk2msftngp13.phx.gbl...
What is the best solution for the following scenario:

I have a contacts database and I am creating the business layer obects to
access my data objects. (So the DB is invisible to the Business Layer
consumer)
Each contact can be of some type "Employee, Customer, Vendor, etc." . The
contact types are also stored in a table in the DB.

So in the business layer I am trying to a make a decision on how to model my object flow. Here are some examples:
---------------------------------
Method One:
Contact objPerson = new Contact();
objPerson.Name = "Jose";
objPerson.Address = "666 somewhere street";
objPerson.City = "somecity";
objPerson.ContactTypeID = 3; //Each
objPerson.Save();

Problem:
If the ContactTypeID does not exist in the DB there will be an error thrown by the DB. Also, when a user type objPerson.ContactType, I want it to
return a "ContactType" object.

--------------------------------
Method Two:
Contact objPerson = new Contact();
.... Set Generic Properties ...
ContactType objContactType = objAppFactory.GetContactType(3);
objPerson.ContactType = objContactType;

Problem:
This method allows me to throw an error as soon as the user tries to lookup a nonexistent ContactTypeID before assigning to the Contact. However, I
have the extra round trip to the DB. Seems silly

-------------------------------

Method Three:
I can could provide to ways of setting the ContactType.
objPerson.ContactType = objApp.GetContactType(3);
or
objPerson.SetContactTypeID(3);

Problem:
Seems dumb

------------------------------

Method Four:
ContactType objEmployee = new ContactType();
Contact objPerson = new Contact();
objPerson.Name = "Jose";
objPerson.Address = "666 somewhere street";
objPerson.City = "somecity";
objEmployee.AddContact(objPerson);

Problem:
If the user attempts objPerson.Save() before it has been added to a
"ContactType" the DB will throw an error becuase it does not have a
"ContactTypeID". I can Validate the object before I attempt to save to the DB by checking to see if the ContactTypeID property has been set; if not, I can throw and exception with something like: "A Contact must be first added to a ContactType before it can be saved to the DB".
Also, when I add the Contact to objEmployee, does the objEmployee.AddContact method need to reoginize that objPerson has not been saved yet, and then
automatically save it to the DB?
Thanks. I am just trying to figure out a standard for accomplishing this so I can replicate the concept into projects I am working on....
Josh

Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.