Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
New exception?
Message
De
03/10/2012 14:54:26
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01554234
Message ID:
01554236
Vues:
66
This message has been marked as a message which has helped to the initial question of the thread.
>I am writing a method to delete a cient in my Repository class. Here is what I've started from:
>
>
>public void DeleteClient(int clientId, bool autoCommit = true)
>        {
>            var ReordersQuery = from reord in this.Reorders
>                                where reord.ClientId == clientId
>                                select reord;
>
>            if (ReordersQuery.Any())
>                //throw "Client " + clientId.ToString() + " can not be deleted because it has related rows in the ReOrders table!";
>
>        }
>
>I read help on throw as I need to throw a new exception. My question is - what kind of exception it should be?

Not easy, depending on the LINQ request, there could be various variations which may apply. I prefer to remain generic. I built a generic LINQ class where I only have one Try/Catch for all LINQ queries, with a nQuery to identify the query ID. All this can go into a log as well as logging errors when they occur. So, basically, I simplified this management as follow with this class:
Namespace Framework

    Public Class Linq

        Public cMessage As String = ""
        Public cTableLogError As String = ""
        Public dDate As Date = Nothing
        Public lSuccess As Boolean = False
        Public nMaximumRecord As Integer = 0
        Public nQuery As Integer = 0
        Public oApp As Framework.App = Nothing
        Public oProcess As Framework.LXProcess = Nothing
        Private lLogData As Boolean = False
        Private oErrorLogField As Collection = New Collection

        ' This is when we access the class in a desktop mode
        Public Sub New(ByVal toApplication As Framework.App)
            oApp = toApplication
            lLogData = oApp.lLogData
        End Sub

        ' This is when we access the class in a Web mode
        Public Sub New(ByVal toProcess As Framework.LXProcess)
            oProcess = toProcess
            oApp = oProcess.oApp
            lLogData = oProcess.lLogData
        End Sub

        Public Function SQLExec() As Boolean
            Dim llSuccess As Boolean = False
            Dim loLogData As Framework.LogData = Nothing

            ' Get the proper definition as per the current scope
            If oProcess Is Nothing Then
                loLogData = New Framework.LogData(oApp)
            Else
                loLogData = New Framework.LogData(oProcess)
            End If

            ' Reset the values
            cMessage = ""
            lSuccess = False

            ' If we are logging
            If lLogData Then

                ' Start the log
                loLogData.cTitle = "LINK query ID=" + nQuery.ToString
                If Not loLogData.StartLog() Then
                    Return False
                End If

            End If

            Try

                ' Execute the client code
                If Not SQLExecClient() Then

                End If

                llSuccess = True
            Catch loError As Exception
                cMessage = loError.Message

                ' If we log the error in the Error table
                If cTableLogError.Length = 0 Then

                    ' Get the proper definition as per the current scope
                    If oProcess Is Nothing Then
                        oApp.ErrorSetup(loError)
                    Else
                        oProcess.ErrorSetup(loError)
                    End If

                Else

                    ' Log the error in a custom table
                    If Not ErrorLog(loError) Then
                        Return False
                    End If

                End If

            End Try

            ' If we are logging
            If lLogData Then

                ' Stop the log
                If Not loLogData.StopLog() Then
                    Return False
                End If

            End If

            ' Reset the value
            nQuery = 0

            Return llSuccess
        End Function

        ' Log an error
        ' expO1 Error exception object
        Private Function ErrorLog(ByVal toError As Exception) As Boolean
            Dim lcField As String = ""
            Dim loObject As Object = Nothing
            Dim loInsertRow As Framework.InsertRow = Nothing
            Dim loValue As Object = Nothing

            ' Get the proper definition as per the current scope
            If oProcess Is Nothing Then
                loInsertRow = New Framework.InsertRow(oApp)
            Else
                loInsertRow = New Framework.InsertRow(oProcess)
            End If

            ' Insert a record in the table for the log error
            loInsertRow.cAlias = cTableLogError
            loInsertRow.ParameterAdd("Message", cMessage)
            loInsertRow.ParameterAdd("Query", nQuery)
            loInsertRow.ParameterAdd("StackTrace", toError.StackTrace)

            ' For each field in the error log field
            For Each loObject In oErrorLogField

                ' Initialization
                lcField = loObject(1)
                loValue = loObject(2)

                ' Add the field
                loInsertRow.ParameterAdd(lcField, loValue)

            Next

            If Not loInsertRow.InsertRow() Then
                Return False
            End If

            Return True
        End Function

        ' Add the information for a field in the error log
        ' expC1 Field
        ' expO1 Value
        Public Function AddErrorLogField(ByVal tcField As String, ByVal toValue As Object) As Boolean
            Dim loObject(2) As Object

            loObject(1) = tcField
            loObject(2) = toValue

            oErrorLogField.Add(loObject)
            Return True
        End Function

        Public Overridable Function SQLExecClient() As Boolean
            Return True
        End Function

    End Class

End Namespace
This class contains some framework related functionalities but you'll be able to understand it in seconds.

Then, from the client application framework Linq sub class:
Public Class LinqProjectSpecificNameHere
    Inherits Framework.Framework.Linq

    Public oClient As ClientAPI.ClientService.Contact = Nothing

    Sub New(ByVal toApplication As Framework.Framework.App)
        MyBase.New(toApplication)
    End Sub

    Public Overrides Function SQLExecClient() As Boolean

        ' Based on the query
        Select Case nQuery

            ' Generate token to retrieve data from this shop
            Case 5

                ' Get all the clients
                oClient = (From ...

        End Select

        Return True
    End Function

End Class
...and, from the client application specific logic:
        Dim loLinqProjectSpecificNameHere As LinqProjectSpecificNameHere = New LinqProjectSpecificNameHere(oApp)

                ' Get all the clients
                loLinqProjectSpecificNameHere.nQuery = 5
                If Not loLinqProjectSpecificNameHere.SQLExecClient() Then
                    Return False
                End If
If you find something more into the exception, let me know. We are also into that at various projects and, so far, I found best to remain generic.
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform