Need an OOP expert for some simple newie questions

N

Nemisis

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
 
N

Nick Malik [Microsoft]

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/archive/2006/03/06/15608.aspx
http://weblogs.asp.net/scottgu/archive/2006/06/22/454436.aspx

If you are not on 2.0 yet, then this one may be more appropriate.
http://www.theserverside.net/tt/articles/showarticle.tss?id=DataSetDesigner

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

Nick Malik [Microsoft]

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

Cor Ligthert [MVP]

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
 
J

Joanna Carter [TeamB]

"Nemisis" <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top