Re: Need an OOP expert for some simple newie questions
- From: "Nick Malik [Microsoft]" <nickmalik@xxxxxxxxxxxxxxxxxx>
- Date: Sat, 12 Aug 2006 10:54:08 -0700
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.
--
"Nemisis" <darrens2005@xxxxxxxxxxx> wrote in message
news:1155039554.250730.107300@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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
.
- References:
- Need an OOP expert for some simple newie questions
- From: Nemisis
- Need an OOP expert for some simple newie questions
- Prev by Date: Re: What do these BIZARE Messages mean - WTF.bmp [00/36]
- Next by Date: Re: Need an OOP expert for some simple newie questions
- Previous by thread: Need an OOP expert for some simple newie questions
- Next by thread: Re: Need an OOP expert for some simple newie questions
- Index(es):
Relevant Pages
|