p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 (http://p2p.wrox.com/forumdisplay.php?f=252)
-   -   Survey Engine (http://p2p.wrox.com/showthread.php?t=66331)

Dennis239 March 2nd, 2008 03:07 PM

Survey Engine
 
Hello. In chapter 4 Survey Engine I found this code. Procedure SaveResponses() writes user responses to the database. I can't understand how does it correlate with N-tier design where Data Access Level is completely independent from Application Level. What I mean.
Code:

Dim mform As HttpContext = HttpContext.Current
Dim QuestionIDs As Collection = GetQuestionIDsForSurvey(mform.Request.QueryString("surveyID"))

How this code is supposed to work if we move from website to Windows Application for example?

Code:

   
   Public Shared Function SaveResponses() As Boolean

        Using mConnection As New SqlConnection(Config.ConnectionString)

            ' get the request context...(we can access the values from the form
            'that the user last posted from here!)
            Dim mform As HttpContext = HttpContext.Current

            'get the list of questions from the db
            Dim QuestionIDs As Collection = GetQuestionIDsForSurvey(mform.Request.QueryString("surveyID"))

            'iterate through each of the question ID's and add the appropriate response
            'to the responses table
            For i As Integer = 0 To QuestionIDs.Count - 1

                'ensure the user selected one of the option buttons for this specific question ID
                If mform.Request.Form.Item("Q" & QuestionIDs(i + 1).ToString()) <> "" Then

                    'Create a command object
                    Dim mCommand As SqlCommand = New SqlCommand("sprocResponseInsertItem", mConnection)

                    'set it to the type of 'stored procedure'
                    mCommand.CommandType = CommandType.StoredProcedure

                    'add in two parameters: the question ID and the choice of the user (A,B,C,or D)
                    mCommand.Parameters.AddWithValue("@questionID", QuestionIDs(i + 1).ToString())
                    mCommand.Parameters.AddWithValue("@selection", mform.Request.Form.Item("Q" & QuestionIDs(i + 1).ToString()))

                    'open the connection and execute the stored procedure
                    mConnection.Open()
                    mCommand.ExecuteNonQuery()

                    'close the connection and dispose of the command
                    mConnection.Close()
                    mCommand.Dispose()

                End If
            Next

            Return True

        End Using


    End Function




dparsons March 2nd, 2008 04:41 PM

Hmmm. Imar may disagree with me and there may be a good reason why he did this, but here is my take on it. IMHO the function that you provided in your post could be further seperated out since there is a coupling of Business Logic along with DataAccess in this function. What I might do is something like this:

Public Class fooBLL
Public Sub SaveResponses()
     Dim QuestionIDs As Collection = GetQuestionIDsForSurvey(mform.Request.QueryString( "surveyID"))
     If Not fooDAL.SaveResponses(QuestionIDs) Then 'DoSomething
End Sub
End Class

Public Class fooDAL
Public Shared Function SaveResponses(ByVal QuestionIDs As Collection) as Boolean
         For i As Integer = 0 To QuestionIDs.Count - 1

                'ensure the user selected one of the option buttons for this specific question ID
                If mform.Request.Form.Item("Q" & QuestionIDs(i + 1).ToString()) <> "" Then

                    'Create a command object
                    Dim mCommand As SqlCommand = New SqlCommand("sprocResponseInsertItem", mConnection)

                    'set it to the type of 'stored procedure'
                    mCommand.CommandType = CommandType.StoredProcedure

                    'add in two parameters: the question ID and the choice of the user (A,B,C,or D)
                    mCommand.Parameters.AddWithValue("@questionID", QuestionIDs(i + 1).ToString())
                    mCommand.Parameters.AddWithValue("@selection", mform.Request.Form.Item("Q" & QuestionIDs(i + 1).ToString()))

                    'open the connection and execute the stored procedure
                    mConnection.Open()
                    mCommand.ExecuteNonQuery()

                    'close the connection and dispose of the command
                    mConnection.Close()
                    mCommand.Dispose()

                End If
            Next

            Return True

End Function
End Class

The only thing that i did not demonstrate abouve that you would need to do is create an additional array to pass into your DAL method to replace this line:
If mform.Request.Form.Item("Q" & QuestionIDs(i + 1).ToString()) <> "" Then

Once that is complete, if you were to port this to a windows app you simply would have to change the mechanisim by which you return the QuestionIDs and pass them off for processing in the DAL.

hth.

-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========

Dennis239 March 3rd, 2008 02:30 AM

Yes. Of course what you posted is better. And to be honest I can't believe Imar wrote Chapters 4 and 7 (I haven't read the remainder yet). Take this function from Capter 7 - Photogallery
Code:

    Public Shared Function InsertCollection(ByVal pc As PhotoCollection) As Boolean

        Try

            Dim conn As New SqlConnection()
            Dim cmd As New SqlCommand()
            conn.ConnectionString = PhotoDB.ConnectionString
            cmd.Connection = conn
            conn.Open()
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "add_collection"

            ' Create a SqlParameter for each parameter in the stored procedure.
            Dim nameParam As New SqlParameter("@name", pc.name)
            Dim descParam As New SqlParameter("@desc", pc.description)
            cmd.Parameters.Add(nameParam)
            cmd.Parameters.Add(descParam)

            cmd.ExecuteNonQuery()

            Return True

        Catch ex As Exception


            Throw (ex)

        End Try

    End Function

Where connection is closed?
Code:

Catch ex As Exception


      Throw (ex)

End Try

What is this?


Imar March 3rd, 2008 04:13 AM

Correct, I didn't write those chapters.

AFAICS, the connection is not closed.

The Throw (ex) is not the most optimal solution. I think the idea was this:

Try
  ' Some risky code
Catch
  LogError()
  Throw
End Try

This way, the LogError() method is called to log the error somehow. The exception is the rethrown to cause it to bubble up to the ASP.NET error handling.

If you don't have error logging like this, you can remove the Try Catch statement altogether and just rely on ASP.NET error handling.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004

Dennis239 March 3rd, 2008 05:09 AM

OK, thank you.
Imar, I really like your blog's article "Building Layered Web Applications". And I like like this book's approach because it gives an excellent almost real-life experience to the reader. (I haven't seen such a book which would describe several small real-life but not very complex projects). And I just think that you (as a very smart author for me) should supervise your co-authors :) Because readers can gain a wrong impression of this book.

PS I'm looking forward to new editions in ASP.NET 3.5 :)


dparsons March 3rd, 2008 09:08 AM

Appologies Imar, I didn't realize that there were co-authors on this book.


Dennis: Beginning ASP.NET 3.5 is available (depending on your locale of course) http://www.amazon.com/Beginning-ASP-...4549651&sr=8-2

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========

Imar March 3rd, 2008 02:34 PM

Hi Dennis,

There are currently no plans for a 3.5 version of the book. However, as Doug pointed out, I just released Beginning ASP.NET 3.5 with C# and VB.NET. You may also want to take a look at Professional ASP.NET 3.5, also published by Wrox.

Doug: you're right. The other two guys on the front cover of the book are just my other schizophrenic personalities.... ;) Don't tell anyone....

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004


All times are GMT -4. The time now is 06:27 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.