Click here to Skip to main content
15,885,182 members
Home / Discussions / C#
   

C#

 
GeneralRe: Problem with get() method! Pin
LAPEC20-Jun-14 8:14
LAPEC20-Jun-14 8:14 
GeneralRe: Problem with get() method! Pin
Eddy Vluggen20-Jun-14 8:36
professionalEddy Vluggen20-Jun-14 8:36 
GeneralRe: Problem with get() method! Pin
LAPEC20-Jun-14 8:43
LAPEC20-Jun-14 8:43 
GeneralRe: Problem with get() method! Pin
LAPEC20-Jun-14 8:46
LAPEC20-Jun-14 8:46 
GeneralRe: Problem with get() method! Pin
Eddy Vluggen20-Jun-14 9:01
professionalEddy Vluggen20-Jun-14 9:01 
GeneralRe: Problem with get() method! Pin
LAPEC20-Jun-14 9:05
LAPEC20-Jun-14 9:05 
GeneralRe: Problem with get() method! Pin
Eddy Vluggen20-Jun-14 9:23
professionalEddy Vluggen20-Jun-14 9:23 
QuestionException Handling / Response Question Pin
Kevin Marois20-Jun-14 6:45
professionalKevin Marois20-Jun-14 6:45 
I posted on this a few months ago, and I need to revisit it...

I have a typical n-tier app - DAL, BL, WebAPI, Controllers, UI, etc.

I've seen many discussions on how to let the UI know that an exception happened in the back end. All have both good & bad points.

If you put a TRY/CATCH in the DAL, log the error, then what..??? Throw again and catch it again in a UI side TRY/CATCH?

What about situations where it's NOT an exception, but the UI needs to know something, like a duplicate record problem, or a record in use and can't be deleted problem? Would you throw for these also?

What I WAS doing was passing back a custom response object from each DAL method. My Response class:

[DataContract]
[Serializable]
public class _ResponseBase
{
    [DataMember]
    public Result Result { get; set; }

    [DataMember]
    public string Message { get; set; }

    private Exception _Exception;
    [DataMember]
    public Exception Exception 
    {
        get { return _Exception; }
        set 
        {
            if (_Exception != value)
            {
                _Exception = value;
                Result = Result.Failure;
                Message = _Exception.Message;
            }
        } 
    }

    public _ResponseBase()
    {
        Result = Result.Success;
        Message = string.Empty;
        Exception = null;
    }
}


This class allows you to pass back Result.Success with the data from the DAL, or Result.Failure with a message such "Duplicate record", or Result.Failure with an Exception.

Then in the UI's base View Model I have a method called HandleResponse that is called like:

var response = Engine.APIProxy.GetLookups(AppConstants.TRAINING_CATEGORIES);

if (response.Result == Result.Success)
{
    Categories = new ObservableCollection<LookupEntity>(response.Data);
}
else
{
    HandleResponse(response);
}


and the HandleResponse method:

protected void HandleResponse(_ResponseBase response)
{
    if (response.Exception == null)
    {
        if (!string.IsNullOrEmpty(response.Message))
        {
            MessageBox.Show(response.Message, "Response", MessageBoxButton.OK, MessageBoxImage.Information);
        }
        else
        {
            throw new ArgumentException("The response did not contain an exception or a message");
        }
    }
    else
    {
        logException(response.Exception);
        throw response.Exception;
    }
}


There is a WPF UI, a WPF Tablet App, and a Web App that all go through the Web API, so having a consistent response object returned is very helpful, although it does mean a bit more UI code.

So far this works well, but I'd like to get your opinion. Do you have a better way?

Thanks
If it's not broken, fix it until it is

GeneralRe: Exception Handling / Response Question Pin
PIEBALDconsult20-Jun-14 7:37
mvePIEBALDconsult20-Jun-14 7:37 
GeneralRe: Exception Handling / Response Question Pin
Kevin Marois20-Jun-14 7:39
professionalKevin Marois20-Jun-14 7:39 
GeneralRe: Exception Handling / Response Question Pin
PIEBALDconsult20-Jun-14 7:44
mvePIEBALDconsult20-Jun-14 7:44 
GeneralRe: Exception Handling / Response Question Pin
Kevin Marois20-Jun-14 7:52
professionalKevin Marois20-Jun-14 7:52 
GeneralRe: Exception Handling / Response Question Pin
PIEBALDconsult20-Jun-14 8:05
mvePIEBALDconsult20-Jun-14 8:05 
AnswerRe: Exception Handling / Response Question Pin
Eddy Vluggen20-Jun-14 8:05
professionalEddy Vluggen20-Jun-14 8:05 
GeneralRe: Exception Handling / Response Question Pin
Kevin Marois20-Jun-14 8:18
professionalKevin Marois20-Jun-14 8:18 
GeneralRe: Exception Handling / Response Question Pin
Eddy Vluggen20-Jun-14 9:21
professionalEddy Vluggen20-Jun-14 9:21 
GeneralRe: Exception Handling / Response Question Pin
Kevin Marois20-Jun-14 10:22
professionalKevin Marois20-Jun-14 10:22 
GeneralRe: Exception Handling / Response Question Pin
Eddy Vluggen20-Jun-14 11:05
professionalEddy Vluggen20-Jun-14 11:05 
QuestionPass specific object to a method expecting ref object parameter? Pin
arnold_w20-Jun-14 3:24
arnold_w20-Jun-14 3:24 
AnswerRe: Pass specific object to a method expecting ref object parameter? Pin
Richard Deeming20-Jun-14 3:58
mveRichard Deeming20-Jun-14 3:58 
GeneralRe: Pass specific object to a method expecting ref object parameter? Pin
OriginalGriff20-Jun-14 3:59
mveOriginalGriff20-Jun-14 3:59 
GeneralRe: Pass specific object to a method expecting ref object parameter? Pin
Richard Deeming20-Jun-14 4:01
mveRichard Deeming20-Jun-14 4:01 
AnswerRe: Pass specific object to a method expecting ref object parameter? Pin
OriginalGriff20-Jun-14 3:59
mveOriginalGriff20-Jun-14 3:59 
AnswerRe: Pass specific object to a method expecting ref object parameter? Pin
PIEBALDconsult20-Jun-14 7:41
mvePIEBALDconsult20-Jun-14 7:41 
AnswerRe: Pass specific object to a method expecting ref object parameter? Pin
Eddy Vluggen20-Jun-14 8:47
professionalEddy Vluggen20-Jun-14 8:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.