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