473,809 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Addre ss = "666 somewhere street";
objPerson.City = "somecity";
objPerson.Conta ctTypeID = 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.Conta ctType, I want it to
return a "ContactTyp e" object.

--------------------------------
Method Two:
Contact objPerson = new Contact();
..... Set Generic Properties ...
ContactType objContactType = objAppFactory.G etContactType(3 );
objPerson.Conta ctType = 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.Conta ctType = objApp.GetConta ctType(3);
or
objPerson.SetCo ntactTypeID(3);

Problem:
Seems dumb

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

Method Four:
ContactType objEmployee = new ContactType();
Contact objPerson = new Contact();
objPerson.Name = "Jose";
objPerson.Addre ss = "666 somewhere street";
objPerson.City = "somecity";
objEmployee.Add Contact(objPers on);

Problem:
If the user attempts objPerson.Save( ) before it has been added to a
"ContactTyp e" 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.Add Contact
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 2330
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["ContactKin d "]));
//reading from a datareader

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

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

"msnews.microso ft.com" <aa***@yahoo.co m> wrote in message
news:ec******** ******@tk2msftn gp13.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.Addre ss = "666 somewhere street";
objPerson.City = "somecity";
objPerson.Conta ctTypeID = 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.Conta ctType, I want it to
return a "ContactTyp e" object.

--------------------------------
Method Two:
Contact objPerson = new Contact();
.... Set Generic Properties ...
ContactType objContactType = objAppFactory.G etContactType(3 );
objPerson.Conta ctType = 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.Conta ctType = objApp.GetConta ctType(3);
or
objPerson.SetCo ntactTypeID(3);

Problem:
Seems dumb

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

Method Four:
ContactType objEmployee = new ContactType();
Contact objPerson = new Contact();
objPerson.Name = "Jose";
objPerson.Addre ss = "666 somewhere street";
objPerson.City = "somecity";
objEmployee.Add Contact(objPers on);

Problem:
If the user attempts objPerson.Save( ) before it has been added to a
"ContactTyp e" 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.Add Contact 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
6392
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 def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the method foo. But the initialization of attr2 is wrong
13
2394
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. Two sub-classes have the relationship to communicate back and forth through this pointer. The pointer is responsible inside Top class for allocating and deallocating two sub-classes. CMemory class is responsible to allocate and deallocate memory...
15
9084
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 the purpose of learning to work with C++. It therefore makes some sense for me to give the situation the amount of consideration presented below. To be quite honest, I'm amazed at the amount there is to say about such a seemingly simple...
3
1821
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 abstract parent with four basic functions: Open, Read, Write, and Close. // Note alot of the stuff has been left out // just to give a basic picture class IO { public:
1
2274
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 ~A() ;
11
2241
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, Struct is may be better...but i am looking for some advise from the experts on this? Assumptions: it is possible for some operations, i may have 8000 to 10000 (or more) objects instantiated. what are some considerations in designing a system that...
6
2123
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 responsible for instantiating the orders class? Would it be the ui layer or the master class in the business layer? thanks,
43
2103
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 within main? (It just doesn't "feel" right to me). Example 1: class MyProg { public:
6
2146
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 times when I program, I will create objects for a specific purpose for a program and if I need to add to it I just add the code.
29
2235
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 "new" contract. I have a method in my contract class called "Save" which is called like this... dim oContract as new Contract
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10387
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.