3
\$\begingroup\$

I have a CRM system that has Notes, Payments, DairyEvents and anything else that needs to be bolt on over time.

I created an ITimeline interface that has a datetime and message method and implemented them on those models.

This is the simplest form of code I have started with:

public static List<Interfaces.ITimeline> GetAll(long personID)
{
 List<Interfaces.ITimeline> timelineEvents = new List<Interfaces.ITimeline>();
 timelineEvents.AddRange(Controllers.FollowUp.GetAll(personID));
 //add more to the list as the system grows
 return timelineEvents;
}

That is fine, as long as one of the controllers doesn't throw and error, then the whole Interface collapses. I had something similar happen on a another project, production environment, where a single provider went haywire due a bug on their system, amongst several other providers that were being interfaced with and worked fine. It wouldn't be so bad if it didn't crash all 600+ sites, just because of that mistake somebody else made.

I don't want to do try catches around every single addrange - I think that is just cluttering. Also, with integration like that, you build things to work, but exceptions happens here and there.

What is a proper way of taking data from various sources, controllers or integration, adding them to a collection of items for the interface? That way, if something fails in the middle, it won't bring down the entire page, or even worse, the whole framework.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Apr 15, 2014 at 18:10
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

The error should be caught where it occurs, namely in the controller. If possible try to implement something like this

TimelineEventCollection personTimelineEvents; // Change to the appropriate collection type.
if (Controllers.FollowUp.TryGetAll(personID, out personTimelineEvents)) {
 timelineEvents.AddRange(personTimelineEvents);
}

The advantage of this approach is that the error does not get swallowed up silently by some try-catch-statement. You get a Boolean return value from TryGetAll telling you if all went right. At the same time the basic error handling happens in a low level method and not in your list filling routine.

If you cannot alter the Controllers.FollowUp class, you can still write an extension method giving you the same functionality.

answered Apr 15, 2014 at 18:33
\$\endgroup\$
1
  • \$\begingroup\$ That is SOLID advice! Defiantly a great approach, I use TryGet almost daily and never thought of doing that on my own code! Thanks +1 \$\endgroup\$ Commented Apr 15, 2014 at 18:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.