473,397 Members | 2,116 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,397 software developers and data experts.

Class Design Question

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
1 2320
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
3
by: fernandez.dan | last post by:
I'm still learning how to use Object Oriented concepts. I'm have a basic question dealing with design. I have two classes that deal with I/O pertaining to network and usb that inherit from an...
1
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
11
by: dimension | last post by:
If my intentions are to create objects that encapsulates data rows in a table, is it better to use a Struct or Class? Based on what i read, if my objects will simply have get and set methods,...
6
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
43
by: Tony | last post by:
I'm working with GUI messaging and note that MFC encapsulates the message loop inside of a C++ class member function. Is this somehow inherently less robust than calling the message loop functions...
6
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often...
29
by: Brad Pears | last post by:
Here is a simple OO design question... I have a Contract class. The user can either save an existing contract or they start off fresh with a blank contract, fill in the data and then save a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.