473,657 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need an OOP expert for some simple newie questions

Hi everyone,

I have 2 classes, Company and Contact, a company can have 1 or more
contacts. A contact can only be in one company.

I have created a Company class object that contains all the properties
for my company. I have created a CompanyFactory class that has the
methods to Create, Retrieve, Update and Delete a company.

I have done the same for the Contact class and ContactFactory.

Questions.
1. If i want to get all the contacts for a company, should the method
go in the CompanyFactory or the ContactFactory? What parameter should
i pass in?

2. When called methods to retrieve company data, should you pass the ID
of the company into the function and return a company object, or should
you pass in a company object, with the companyID assigned, then return
a company object??

If anyone can point me in the direction of a good guide that describes
this, i would be grateful, or even better, if someone could write a
short example, that would be great.

This is my first time trying to write a OOP application, we are
converting or current system to be OOP based, and i would like to start
off on the right foot.

here is some code of company class and companyfactory, so someone can
say if i am doing it correctly. I have added a connectstring property
to my companyFactory as different users have different connection
strings, and thought this way would allow me to pass in the correct
connectionstrin g.

I havent included all sub and functions, as didnt think u would want to
see them all.

Class Company
Private _CompanyID As Integer
Public Property CompanyID() As Integer
Get
Return _CompanyID
End Get
Set(ByVal value As Integer)
_CompanyID = value
End Set
End Property

Private _CompanyName As String
Public Property CompanyName() As String
Get
Return _CompanyName
End Get
Set(ByVal value As String)
_CompanyName = value
End Set
End Property

Private _Reference As String
Public Property Reference() As String
Get
Return _Reference
End Get
Set(ByVal value As String)
_CompanyReferen ce = value
End Set
End Property
End Class

Public Class CompanyFactory

Sub New()

_ConnectionStri ng = String.Empty

End Sub

Sub New(ByVal pConnectionStri ng As String)

_ConnectionStri ng = pConnectionStri ng

End Sub

Private _ConnectionStri ng As String
Public Property ConnectionStrin g() As String
Get
Return _ConnectionStri ng
End Get
Set(ByVal value As String)
_ConnectionStri ng = value
End Set
End Property

Public Function Add(ByVal pCompany As BuildSoft.Compa ny) As
BuildSoft.Compa ny

Dim SQL As New StringBuilder

' create SQL for inserting company into database
SQL.Append("INS ERT INTO TBLCOMPANY (name, ref) values
(@name, @ref) Select @@SCOPE_IDENTIT Y")

Dim oConn As New SqlConnection(M e.ConnectionStr ing)
Dim oComm As New SqlCommand(SQL. ToString, oConn)

Try

' add parameters to sql here
ocomm.parameter s.add(new sqlparameter("@ name",
pCompany.Compan yName))
ocomm.parameter s.add(new sqlparameter("@ ref",
pCompany.Refere nce))

' open connection
oConn.Open()

' begin the transaction for updating company data
oComm.Transacti on = oConn.BeginTran saction

' execute command
pCompany.Compan yID = oComm.ExecuteSc alar()

' commit transaction as no error has occurred
oComm.Transacti on.Commit()

Catch ex As Exception

' roll back the transaction if an error has occurred
oComm.Transacti on.Rollback()

Finally

' close connection
oConn.Close()

' dispose of command and connection
oComm.Dispose()
oConn.Dispose()

End Try

' return company added
Return pCompany

End Function

Aug 8 '06 #1
4 1170
I'm going to suggest a pattern strongly typed datasets for this. You should
not have to write ANY of this code anymore. It's a waste of your time.
Stop doing it.

References:
http://aspadvice.com/blogs/rjdudley/.../06/15608.aspx
http://weblogs.asp.net/scottgu/archi...22/454436.aspx

If you are not on 2.0 yet, then this one may be more appropriate.
http://www.theserverside.net/tt/arti...ataSetDesigner

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Nemisis" <da*********@ho tmail.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
Hi everyone,

I have 2 classes, Company and Contact, a company can have 1 or more
contacts. A contact can only be in one company.

I have created a Company class object that contains all the properties
for my company. I have created a CompanyFactory class that has the
methods to Create, Retrieve, Update and Delete a company.

I have done the same for the Contact class and ContactFactory.

Questions.
1. If i want to get all the contacts for a company, should the method
go in the CompanyFactory or the ContactFactory? What parameter should
i pass in?

2. When called methods to retrieve company data, should you pass the ID
of the company into the function and return a company object, or should
you pass in a company object, with the companyID assigned, then return
a company object??

If anyone can point me in the direction of a good guide that describes
this, i would be grateful, or even better, if someone could write a
short example, that would be great.

This is my first time trying to write a OOP application, we are
converting or current system to be OOP based, and i would like to start
off on the right foot.

here is some code of company class and companyfactory, so someone can
say if i am doing it correctly. I have added a connectstring property
to my companyFactory as different users have different connection
strings, and thought this way would allow me to pass in the correct
connectionstrin g.

I havent included all sub and functions, as didnt think u would want to
see them all.

Class Company
Private _CompanyID As Integer
Public Property CompanyID() As Integer
Get
Return _CompanyID
End Get
Set(ByVal value As Integer)
_CompanyID = value
End Set
End Property

Private _CompanyName As String
Public Property CompanyName() As String
Get
Return _CompanyName
End Get
Set(ByVal value As String)
_CompanyName = value
End Set
End Property

Private _Reference As String
Public Property Reference() As String
Get
Return _Reference
End Get
Set(ByVal value As String)
_CompanyReferen ce = value
End Set
End Property
End Class

Public Class CompanyFactory

Sub New()

_ConnectionStri ng = String.Empty

End Sub

Sub New(ByVal pConnectionStri ng As String)

_ConnectionStri ng = pConnectionStri ng

End Sub

Private _ConnectionStri ng As String
Public Property ConnectionStrin g() As String
Get
Return _ConnectionStri ng
End Get
Set(ByVal value As String)
_ConnectionStri ng = value
End Set
End Property

Public Function Add(ByVal pCompany As BuildSoft.Compa ny) As
BuildSoft.Compa ny

Dim SQL As New StringBuilder

' create SQL for inserting company into database
SQL.Append("INS ERT INTO TBLCOMPANY (name, ref) values
(@name, @ref) Select @@SCOPE_IDENTIT Y")

Dim oConn As New SqlConnection(M e.ConnectionStr ing)
Dim oComm As New SqlCommand(SQL. ToString, oConn)

Try

' add parameters to sql here
ocomm.parameter s.add(new sqlparameter("@ name",
pCompany.Compan yName))
ocomm.parameter s.add(new sqlparameter("@ ref",
pCompany.Refere nce))

' open connection
oConn.Open()

' begin the transaction for updating company data
oComm.Transacti on = oConn.BeginTran saction

' execute command
pCompany.Compan yID = oComm.ExecuteSc alar()

' commit transaction as no error has occurred
oComm.Transacti on.Commit()

Catch ex As Exception

' roll back the transaction if an error has occurred
oComm.Transacti on.Rollback()

Finally

' close connection
oConn.Close()

' dispose of command and connection
oComm.Dispose()
oConn.Dispose()

End Try

' return company added
Return pCompany

End Function

Aug 12 '06 #2
also you may want to consider the NHibernate framework for persisting
objects. This is an alternative to the Strongly Typed Datasets link from my
prior post. Depends on your familiarity with the Hibernate model.

http://www.hibernate.org/343.html

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Nemisis" <da*********@ho tmail.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
Hi everyone,

I have 2 classes, Company and Contact, a company can have 1 or more
contacts. A contact can only be in one company.

I have created a Company class object that contains all the properties
for my company. I have created a CompanyFactory class that has the
methods to Create, Retrieve, Update and Delete a company.

I have done the same for the Contact class and ContactFactory.

Questions.
1. If i want to get all the contacts for a company, should the method
go in the CompanyFactory or the ContactFactory? What parameter should
i pass in?

2. When called methods to retrieve company data, should you pass the ID
of the company into the function and return a company object, or should
you pass in a company object, with the companyID assigned, then return
a company object??

If anyone can point me in the direction of a good guide that describes
this, i would be grateful, or even better, if someone could write a
short example, that would be great.

This is my first time trying to write a OOP application, we are
converting or current system to be OOP based, and i would like to start
off on the right foot.

here is some code of company class and companyfactory, so someone can
say if i am doing it correctly. I have added a connectstring property
to my companyFactory as different users have different connection
strings, and thought this way would allow me to pass in the correct
connectionstrin g.

I havent included all sub and functions, as didnt think u would want to
see them all.

Class Company
Private _CompanyID As Integer
Public Property CompanyID() As Integer
Get
Return _CompanyID
End Get
Set(ByVal value As Integer)
_CompanyID = value
End Set
End Property

Private _CompanyName As String
Public Property CompanyName() As String
Get
Return _CompanyName
End Get
Set(ByVal value As String)
_CompanyName = value
End Set
End Property

Private _Reference As String
Public Property Reference() As String
Get
Return _Reference
End Get
Set(ByVal value As String)
_CompanyReferen ce = value
End Set
End Property
End Class

Public Class CompanyFactory

Sub New()

_ConnectionStri ng = String.Empty

End Sub

Sub New(ByVal pConnectionStri ng As String)

_ConnectionStri ng = pConnectionStri ng

End Sub

Private _ConnectionStri ng As String
Public Property ConnectionStrin g() As String
Get
Return _ConnectionStri ng
End Get
Set(ByVal value As String)
_ConnectionStri ng = value
End Set
End Property

Public Function Add(ByVal pCompany As BuildSoft.Compa ny) As
BuildSoft.Compa ny

Dim SQL As New StringBuilder

' create SQL for inserting company into database
SQL.Append("INS ERT INTO TBLCOMPANY (name, ref) values
(@name, @ref) Select @@SCOPE_IDENTIT Y")

Dim oConn As New SqlConnection(M e.ConnectionStr ing)
Dim oComm As New SqlCommand(SQL. ToString, oConn)

Try

' add parameters to sql here
ocomm.parameter s.add(new sqlparameter("@ name",
pCompany.Compan yName))
ocomm.parameter s.add(new sqlparameter("@ ref",
pCompany.Refere nce))

' open connection
oConn.Open()

' begin the transaction for updating company data
oComm.Transacti on = oConn.BeginTran saction

' execute command
pCompany.Compan yID = oComm.ExecuteSc alar()

' commit transaction as no error has occurred
oComm.Transacti on.Commit()

Catch ex As Exception

' roll back the transaction if an error has occurred
oComm.Transacti on.Rollback()

Finally

' close connection
oConn.Close()

' dispose of command and connection
oComm.Dispose()
oConn.Dispose()

End Try

' return company added
Return pCompany

End Function

Aug 12 '06 #3
Nemesis,

In addition to Nick,

I have seen often in these newsgroups that people think that writing
DataClasses is the ultimate OOP.

DataClasses, as the strongly typed datasets (or just the dataset class), are
a part of that, but just an effect of using OOP, it is a very small aspect
from OOP.

You are able to make a program with only Static/Shared classes and than
there is not any OOP in it.

I hope this gives an idea

Cor

"Nemisis" <da*********@ho tmail.comschree f in bericht
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
Hi everyone,

I have 2 classes, Company and Contact, a company can have 1 or more
contacts. A contact can only be in one company.

I have created a Company class object that contains all the properties
for my company. I have created a CompanyFactory class that has the
methods to Create, Retrieve, Update and Delete a company.

I have done the same for the Contact class and ContactFactory.

Questions.
1. If i want to get all the contacts for a company, should the method
go in the CompanyFactory or the ContactFactory? What parameter should
i pass in?

2. When called methods to retrieve company data, should you pass the ID
of the company into the function and return a company object, or should
you pass in a company object, with the companyID assigned, then return
a company object??

If anyone can point me in the direction of a good guide that describes
this, i would be grateful, or even better, if someone could write a
short example, that would be great.

This is my first time trying to write a OOP application, we are
converting or current system to be OOP based, and i would like to start
off on the right foot.

here is some code of company class and companyfactory, so someone can
say if i am doing it correctly. I have added a connectstring property
to my companyFactory as different users have different connection
strings, and thought this way would allow me to pass in the correct
connectionstrin g.

I havent included all sub and functions, as didnt think u would want to
see them all.

Class Company
Private _CompanyID As Integer
Public Property CompanyID() As Integer
Get
Return _CompanyID
End Get
Set(ByVal value As Integer)
_CompanyID = value
End Set
End Property

Private _CompanyName As String
Public Property CompanyName() As String
Get
Return _CompanyName
End Get
Set(ByVal value As String)
_CompanyName = value
End Set
End Property

Private _Reference As String
Public Property Reference() As String
Get
Return _Reference
End Get
Set(ByVal value As String)
_CompanyReferen ce = value
End Set
End Property
End Class

Public Class CompanyFactory

Sub New()

_ConnectionStri ng = String.Empty

End Sub

Sub New(ByVal pConnectionStri ng As String)

_ConnectionStri ng = pConnectionStri ng

End Sub

Private _ConnectionStri ng As String
Public Property ConnectionStrin g() As String
Get
Return _ConnectionStri ng
End Get
Set(ByVal value As String)
_ConnectionStri ng = value
End Set
End Property

Public Function Add(ByVal pCompany As BuildSoft.Compa ny) As
BuildSoft.Compa ny

Dim SQL As New StringBuilder

' create SQL for inserting company into database
SQL.Append("INS ERT INTO TBLCOMPANY (name, ref) values
(@name, @ref) Select @@SCOPE_IDENTIT Y")

Dim oConn As New SqlConnection(M e.ConnectionStr ing)
Dim oComm As New SqlCommand(SQL. ToString, oConn)

Try

' add parameters to sql here
ocomm.parameter s.add(new sqlparameter("@ name",
pCompany.Compan yName))
ocomm.parameter s.add(new sqlparameter("@ ref",
pCompany.Refere nce))

' open connection
oConn.Open()

' begin the transaction for updating company data
oComm.Transacti on = oConn.BeginTran saction

' execute command
pCompany.Compan yID = oComm.ExecuteSc alar()

' commit transaction as no error has occurred
oComm.Transacti on.Commit()

Catch ex As Exception

' roll back the transaction if an error has occurred
oComm.Transacti on.Rollback()

Finally

' close connection
oConn.Close()

' dispose of command and connection
oComm.Dispose()
oConn.Dispose()

End Try

' return company added
Return pCompany

End Function

Aug 13 '06 #4
"Nemisis" <da*********@ho tmail.coma écrit dans le message de news:
11************* *********@m73g2 00...legr oups.com...

| I have 2 classes, Company and Contact, a company can have 1 or more
| contacts. A contact can only be in one company.

| Questions.
| 1. If i want to get all the contacts for a company, should the method
| go in the CompanyFactory or the ContactFactory? What parameter should
| i pass in?

Does the Company "own" the contacts or not ? In other words, can Contacts be
used/created/destroyed by any other classes in the system ?

| 2. When called methods to retrieve company data, should you pass the ID
| of the company into the function and return a company object, or should
| you pass in a company object, with the companyID assigned, then return
| a company object??

This depends on your persistence mechanism. Personally, I don't allow the
business layer to know anything at all about identity of objects, apart from
the pointers that hold the objects. The presistence mechanism keeps a record
of all objects saved/dispensed and manages the IDs internally. In this
situation, you can only ever retrieve lists of objects that meet certain
criteria like name, code, etc. If you are looking for a single object, then
the factory method that you ask for the object should take a criteria as a
parameter and either return one object or null, depending on whether one
single object satisifies that criteria.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Sep 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2170
by: Robert Maas, see http://tinyurl.com/uh3t | last post by:
System login message says PHP is available, so I tried this: http://www.rawbw.com/~rem/HelloPlus/h.php It doesn't work at all. Browser just shows the source. What am I doing wrong?
55
3918
by: Alex | last post by:
Hello people, The following is not a troll but a serious request. I found myself in a position where I have to present a Pro/Con list to management and architects in our company with regard to developing new products (specifically - desktop products) in C#/.NET instead of the usual C++/COM that we do. Since I am not an experienced .NET developer by any definition, I don't have a good grip on the "Pro" part. The argument that I hear...
2
1568
by: Nikolay Bogolioubov | last post by:
Anybody knows where can I get a good "intermediate to expert" level C++ questions list. Need about 500 questions. Appreciate any ideas.
15
1492
by: Don Vaillancourt | last post by:
I do a lot of hiring for my company and a lot of the people I interview say that they are experts at SQL queries, but when I give them something simple just beyond the typical SELECT type of queries, they choke. For example I have a table that looks like this: PK_ID - primary key PARENT_ID - a FK to another row in the same table This essentially is a tree structure. I will ask interviewees to write
162
14814
by: techievasant | last post by:
hello everyone, Iam vasant from India.. I have a test+interview on C /C++ in the coming month so plz help me by giving some resources of FAQS, interview questions, tracky questions, multiple choice questions.etc.. I'll be indebted to everyone.. Thanks in advance.. regards vasant shetty Bangalore
34
7303
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
5
3361
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that I could better understand the concept, he was trying to convey to me. I ran my own example and it crashed and burn "what a surprise!" : (. I ran the authors example out of the book and quess what, it crashed also, : 0. I ran them both on my...
0
8411
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
8838
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
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
8513
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
7351
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
5638
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
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.