Connecting Tech Pros Worldwide Forums | Help | Site Map

Class Design Question

msnews.microsoft.com
Guest
 
Posts: n/a
#1: Nov 13 '05
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



Ignacio Machin
Guest
 
Posts: n/a
#2: Nov 13 '05

re: Class Design Question


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" <aa7im@yahoo.com> wrote in message
news:ec3Ra7nSDHA.3192@tk2msftngp13.phx.gbl...[color=blue]
> 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[/color]
my[color=blue]
> 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[/color]
thrown[color=blue]
> 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[/color]
lookup[color=blue]
> 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[/color]
the[color=blue]
> DB by checking to see if the ContactTypeID property has been set; if not,[/color]
I[color=blue]
> can throw and exception with something like: "A Contact must be first[/color]
added[color=blue]
> to a ContactType before it can be saved to the DB".
> Also, when I add the Contact to objEmployee, does the[/color]
objEmployee.AddContact[color=blue]
> 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[/color]
so[color=blue]
> I can replicate the concept into projects I am working on....
> Josh
>
>[/color]


Closed Thread


Similar C# / C Sharp bytes