From bdf92a9aeac440164a8e07d9a7665df2871059a6 Mon Sep 17 00:00:00 2001 From: Adrian Banks Date: Wed, 21 Jun 2017 22:53:20 +0100 Subject: [PATCH] Throw a better error when missing conference Currently, it throws a NullReferenceException and you have to deduce what is wrong. --- DDDEastAnglia/DataAccess/ConferenceLoader.cs | 15 +++++++++++++-- .../SimpleData/Builders/ConferenceBuilder.cs | 5 ----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/DDDEastAnglia/DataAccess/ConferenceLoader.cs b/DDDEastAnglia/DataAccess/ConferenceLoader.cs index e595045a..76435ce8 100644 --- a/DDDEastAnglia/DataAccess/ConferenceLoader.cs +++ b/DDDEastAnglia/DataAccess/ConferenceLoader.cs @@ -1,6 +1,7 @@ using System; using DDDEastAnglia.DataAccess.SimpleData.Builders; using DDDEastAnglia.Domain; +using Conference = DDDEastAnglia.DataAccess.SimpleData.Models.Conference; namespace DDDEastAnglia.DataAccess { @@ -36,13 +37,23 @@ public ConferenceLoader(IConferenceRepository conferenceRepository, ConferenceBu public IConference LoadConference() { var dataConference = conferenceRepository.GetByEventShortName(DefaultEventName); - return conferenceBuilder.Build(dataConference); + return BuildConference(dataConference); } public IConference LoadConference(int sessionId) { var dataConference = conferenceRepository.ForSession(sessionId); - return conferenceBuilder.Build(dataConference); + return BuildConference(dataConference); + } + + private IConference BuildConference(Conference conference) + { + if (conference == null) + { + throw new ArgumentNullException(nameof(conference), "Cannot find current conference"); + } + + return conferenceBuilder.Build(conference); } } } diff --git a/DDDEastAnglia/DataAccess/SimpleData/Builders/ConferenceBuilder.cs b/DDDEastAnglia/DataAccess/SimpleData/Builders/ConferenceBuilder.cs index 4e821c49..f6d1ce75 100644 --- a/DDDEastAnglia/DataAccess/SimpleData/Builders/ConferenceBuilder.cs +++ b/DDDEastAnglia/DataAccess/SimpleData/Builders/ConferenceBuilder.cs @@ -17,11 +17,6 @@ public ConferenceBuilder(ICalendarItemRepository calendarItemRepository, IBuild< public Domain.Conference Build(Conference item) { - if (item == null) - { - return null; - } - var conference = new Domain.Conference(item.ConferenceId, item.Name, item.ShortName, item.NumberOfTimeSlots, item.NumberOfTracks, item.AnonymousSessions); var calendarItems = calendarItemRepository.GetAll();