M
msnews.microsoft.com
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
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