473,378 Members | 1,543 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,378 software developers and data experts.

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
connectionstring.

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)
_CompanyReference = value
End Set
End Property
End Class

Public Class CompanyFactory

Sub New()

_ConnectionString = String.Empty

End Sub

Sub New(ByVal pConnectionString As String)

_ConnectionString = pConnectionString

End Sub

Private _ConnectionString As String
Public Property ConnectionString() As String
Get
Return _ConnectionString
End Get
Set(ByVal value As String)
_ConnectionString = value
End Set
End Property

Public Function Add(ByVal pCompany As BuildSoft.Company) As
BuildSoft.Company

Dim SQL As New StringBuilder

' create SQL for inserting company into database
SQL.Append("INSERT INTO TBLCOMPANY (name, ref) values
(@name, @ref) Select @@SCOPE_IDENTITY")

Dim oConn As New SqlConnection(Me.ConnectionString)
Dim oComm As New SqlCommand(SQL.ToString, oConn)

Try

' add parameters to sql here
ocomm.parameters.add(new sqlparameter("@name",
pCompany.CompanyName))
ocomm.parameters.add(new sqlparameter("@ref",
pCompany.Reference))

' open connection
oConn.Open()

' begin the transaction for updating company data
oComm.Transaction = oConn.BeginTransaction

' execute command
pCompany.CompanyID = oComm.ExecuteScalar()

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

Catch ex As Exception

' roll back the transaction if an error has occurred
oComm.Transaction.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 1157
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*********@hotmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.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
connectionstring.

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)
_CompanyReference = value
End Set
End Property
End Class

Public Class CompanyFactory

Sub New()

_ConnectionString = String.Empty

End Sub

Sub New(ByVal pConnectionString As String)

_ConnectionString = pConnectionString

End Sub

Private _ConnectionString As String
Public Property ConnectionString() As String
Get
Return _ConnectionString
End Get
Set(ByVal value As String)
_ConnectionString = value
End Set
End Property

Public Function Add(ByVal pCompany As BuildSoft.Company) As
BuildSoft.Company

Dim SQL As New StringBuilder

' create SQL for inserting company into database
SQL.Append("INSERT INTO TBLCOMPANY (name, ref) values
(@name, @ref) Select @@SCOPE_IDENTITY")

Dim oConn As New SqlConnection(Me.ConnectionString)
Dim oComm As New SqlCommand(SQL.ToString, oConn)

Try

' add parameters to sql here
ocomm.parameters.add(new sqlparameter("@name",
pCompany.CompanyName))
ocomm.parameters.add(new sqlparameter("@ref",
pCompany.Reference))

' open connection
oConn.Open()

' begin the transaction for updating company data
oComm.Transaction = oConn.BeginTransaction

' execute command
pCompany.CompanyID = oComm.ExecuteScalar()

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

Catch ex As Exception

' roll back the transaction if an error has occurred
oComm.Transaction.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*********@hotmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.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
connectionstring.

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)
_CompanyReference = value
End Set
End Property
End Class

Public Class CompanyFactory

Sub New()

_ConnectionString = String.Empty

End Sub

Sub New(ByVal pConnectionString As String)

_ConnectionString = pConnectionString

End Sub

Private _ConnectionString As String
Public Property ConnectionString() As String
Get
Return _ConnectionString
End Get
Set(ByVal value As String)
_ConnectionString = value
End Set
End Property

Public Function Add(ByVal pCompany As BuildSoft.Company) As
BuildSoft.Company

Dim SQL As New StringBuilder

' create SQL for inserting company into database
SQL.Append("INSERT INTO TBLCOMPANY (name, ref) values
(@name, @ref) Select @@SCOPE_IDENTITY")

Dim oConn As New SqlConnection(Me.ConnectionString)
Dim oComm As New SqlCommand(SQL.ToString, oConn)

Try

' add parameters to sql here
ocomm.parameters.add(new sqlparameter("@name",
pCompany.CompanyName))
ocomm.parameters.add(new sqlparameter("@ref",
pCompany.Reference))

' open connection
oConn.Open()

' begin the transaction for updating company data
oComm.Transaction = oConn.BeginTransaction

' execute command
pCompany.CompanyID = oComm.ExecuteScalar()

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

Catch ex As Exception

' roll back the transaction if an error has occurred
oComm.Transaction.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*********@hotmail.comschreef in bericht
news:11**********************@m73g2000cwd.googlegr oups.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
connectionstring.

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)
_CompanyReference = value
End Set
End Property
End Class

Public Class CompanyFactory

Sub New()

_ConnectionString = String.Empty

End Sub

Sub New(ByVal pConnectionString As String)

_ConnectionString = pConnectionString

End Sub

Private _ConnectionString As String
Public Property ConnectionString() As String
Get
Return _ConnectionString
End Get
Set(ByVal value As String)
_ConnectionString = value
End Set
End Property

Public Function Add(ByVal pCompany As BuildSoft.Company) As
BuildSoft.Company

Dim SQL As New StringBuilder

' create SQL for inserting company into database
SQL.Append("INSERT INTO TBLCOMPANY (name, ref) values
(@name, @ref) Select @@SCOPE_IDENTITY")

Dim oConn As New SqlConnection(Me.ConnectionString)
Dim oComm As New SqlCommand(SQL.ToString, oConn)

Try

' add parameters to sql here
ocomm.parameters.add(new sqlparameter("@name",
pCompany.CompanyName))
ocomm.parameters.add(new sqlparameter("@ref",
pCompany.Reference))

' open connection
oConn.Open()

' begin the transaction for updating company data
oComm.Transaction = oConn.BeginTransaction

' execute command
pCompany.CompanyID = oComm.ExecuteScalar()

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

Catch ex As Exception

' roll back the transaction if an error has occurred
oComm.Transaction.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*********@hotmail.coma écrit dans le message de news:
11**********************@m73g2000cwd.googlegroups. 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
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
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...
2
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
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...
162
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...
34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
5
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.