Click here to Skip to main content
15,890,438 members
Home / Discussions / C#
   

C#

 
Questionknap sack Pin
Member 1076234920-Apr-14 9:43
Member 1076234920-Apr-14 9:43 
AnswerRe: knap sack Pin
Pete O'Hanlon20-Apr-14 9:50
mvePete O'Hanlon20-Apr-14 9:50 
GeneralRe: knap sack Pin
Manfred Rudolf Bihy20-Apr-14 10:22
professionalManfred Rudolf Bihy20-Apr-14 10:22 
GeneralRe: knap sack Pin
Wes Aday20-Apr-14 10:31
professionalWes Aday20-Apr-14 10:31 
AnswerRe: knap sack Pin
OriginalGriff20-Apr-14 20:48
mveOriginalGriff20-Apr-14 20:48 
GeneralRe: knap sack Pin
harold aptroot21-Apr-14 7:17
harold aptroot21-Apr-14 7:17 
QuestionRe: knap sack Pin
ZurdoDev21-Apr-14 9:38
professionalZurdoDev21-Apr-14 9:38 
QuestionDAL Responses Design Question Pin
Kevin Marois20-Apr-14 7:52
professionalKevin Marois20-Apr-14 7:52 
I am considering a change to the way I return objects from my DAL. I would like your opinion on this design.

Currently each method in the DAL returns void, an Entity, or a List<[SomeEntity]>. So as an example, my AddLookup method looks like this:

public LookupEntity AddLookup(LookupEntity entity)
{
    using (FMGDataContext dc = getDataContext())
    {
        // See if a lookup with the same Caption already exists
        var lookupExists = dc.Lookups.Where(l => l.Caption.Trim().ToLower() == entity.Caption.Trim().ToLower() &&
                                            l.LookupCode.Trim().ToLower() == entity.LookupCode.Trim().ToLower()).Any();

        // If the lookup doesn't already exist...
        if (!lookupExists)
        {
            // Add the lookup
            Lookup newLookup = new Lookup
            {
                LookupCode = entity.LookupCode,
                LookupTypeName = entity.LookupTypeName,
                Caption = entity.Caption,
                Description = entity.Description,
                ParentId = entity.ParentId ?? 0,
            };

            dc.Lookups.InsertOnSubmit(newLookup);
                    
            try
            {
                dc.SubmitChanges();

                entity.Id = newLookup.LookupId;
            }
            catch (Exception e)
            {
                // Handle exception here
            }
        }
        else
        {
            throw new DuplicateLookupException(string.Format("Lookup {0} already exists", entity.Caption));
        }
    }

    return entity;
}


Instead, I'm considering changing all my methods to return an instance of a response object:

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;

                Message = _Exception.Message;
                Result = Result.Failure;
            }
        }
    }
}


So now the AddLookup method would be:

public AddLookupResponse AddLookup(LookupEntity entity)
{
    AddLookupResponse response = new AddLookupResponse();

    using (FalconDataContext dc = getDataContext())
    {
        bool exsits = (from l in dc.Lookups
                        where l.LookupCode.Trim().ToLower() == entity.LookupCode.Trim().ToLower()
                        select l).Any();

        if (exsits)
        {
            response.Result = Result.Failure;
            response.Message = string.Format("Lookup code '{0}' already exists in the Lookups table", entity.LookupCode);
        }
        else
        {
            Lookup newRecord = new Lookup
            {
                LookupCode = entity.LookupCode,
                LookupName = entity.LookupName,
                Caption = entity.Caption,
                Description = entity.Description,
                SystemCode = entity.SystemCode,
                ParentId = entity.ParentId
            };

            dc.Lookups.InsertOnSubmit(newRecord);

            try
            {
                dc.SubmitChanges();

                entity.Id = newRecord.LookupId;
                response.Data = entity;
            }
            catch (Exception e)
            {
                response.Exception = e;
            }
        }
    }

    return response;
}


There are some good advantages of this design:

1. The ability to return more robust data & information back to the UI.
2. A consistent design pattern across all layers
3. Better exception handling

There also some drawbacks:
1. All calls to the DAL from the UI would now have to look like this:

AddLookupResponse response = Engine.APIProxy.AddLookup(SelectedLookupItem);

if (response.Result == Result.Success)
{
    // Do something
}
else
{
	HandleResponse(response);
}


2. There is more code for each method, even though it allows all the handling to be done in the UI.

I welcome your comments.

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

AnswerRe: DAL Responses Design Question Pin
Ravi Bhavnani20-Apr-14 15:04
professionalRavi Bhavnani20-Apr-14 15:04 
GeneralRe: DAL Responses Design Question Pin
Kevin Marois21-Apr-14 6:35
professionalKevin Marois21-Apr-14 6:35 
GeneralRe: DAL Responses Design Question Pin
Ravi Bhavnani21-Apr-14 7:41
professionalRavi Bhavnani21-Apr-14 7:41 
GeneralRe: DAL Responses Design Question Pin
Kevin Marois21-Apr-14 8:10
professionalKevin Marois21-Apr-14 8:10 
QuestionHow to develop on Windows 7 64 bit and output x32 exe that uses x32 usb driver ? Pin
Mario 5620-Apr-14 2:46
Mario 5620-Apr-14 2:46 
AnswerRe: How to develop on Windows 7 64 bit and output x32 exe that uses x32 usb driver ? Pin
OriginalGriff20-Apr-14 3:43
mveOriginalGriff20-Apr-14 3:43 
GeneralRe: How to develop on Windows 7 64 bit and output x32 exe that uses x32 usb driver ? Pin
Mario 5620-Apr-14 6:32
Mario 5620-Apr-14 6:32 
Questionhow to code to c# method call on html anchor tag but that anchor tag is placed in datalist. Pin
Ayang1719-Apr-14 9:14
Ayang1719-Apr-14 9:14 
AnswerRe: how to code to c# method call on html anchor tag but that anchor tag is placed in datalist. Pin
Wes Aday19-Apr-14 10:35
professionalWes Aday19-Apr-14 10:35 
QuestionDoubleClick Selected Row Datagrid (C# WPF Project) Pin
rattlerrFx19-Apr-14 8:27
rattlerrFx19-Apr-14 8:27 
AnswerRe: DoubleClick Selected Row Datagrid (C# WPF Project) Pin
Wes Aday19-Apr-14 10:34
professionalWes Aday19-Apr-14 10:34 
GeneralRe: DoubleClick Selected Row Datagrid (C# WPF Project) Pin
rattlerrFx19-Apr-14 12:10
rattlerrFx19-Apr-14 12:10 
GeneralRe: DoubleClick Selected Row Datagrid (C# WPF Project) Pin
Wes Aday20-Apr-14 2:22
professionalWes Aday20-Apr-14 2:22 
GeneralRe: DoubleClick Selected Row Datagrid (C# WPF Project) Pin
rattlerrFx20-Apr-14 4:24
rattlerrFx20-Apr-14 4:24 
GeneralRe: DoubleClick Selected Row Datagrid (C# WPF Project) Pin
Wes Aday20-Apr-14 4:29
professionalWes Aday20-Apr-14 4:29 
SuggestionRe: DoubleClick Selected Row Datagrid (C# WPF Project) Pin
Richard MacCutchan19-Apr-14 21:20
mveRichard MacCutchan19-Apr-14 21:20 
QuestionDisplaying UTM coordinates on a form Pin
Member 1068390219-Apr-14 8:07
Member 1068390219-Apr-14 8:07 

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.