Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4247936
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:10:46+00:00 2026-05-21T04:10:46+00:00

None of the examples I have looked at for Repository Patterns include any kind

  • 0

None of the examples I have looked at for Repository Patterns include any kind of error handling.
Why is this? Say for instance I have this:

public virtual TItem Insert<TItem>(TItem item) where TItem:class,new()
    {
        dbContext.Set<TItem>().Add(item);
        try
        {
            dbContext.SaveChanges();
        }
        catch (DbUpdateException)
        {

            return null;
        }

        return item;

    }

An instance where we violate a constraint. I catch the DbUpdateException… Where would this error handling live if not in the repository itself?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:10:47+00:00Added an answer on May 21, 2026 at 4:10 am

    For the most part, a repository doesn’t need to worry about handling exceptions. The classes that are consuming the repositories should handle that. In your example, why would want to return null if an insert error occurs? Isn’t that less clear than just throwing an exception?

    For example, let’s say we want to insert a record through the repository and then print out the new ID. Assume that the insert is going to fail for any reason.

    var myNewItem = myRepository.Insert(myItem);
    Console.WriteLine("MyItem added with ID: {0}", myNewItem.ID);
    

    Following the pattern in your question, you’d get a NullReference exception on the second line if the Insert fails. That’s a little strange. It’s more clear to see the DbUpdateException on the first line. It’s also better to be able to count on Insert always either returning a valid instance or throwing an exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have already looked at other related questions, and none of them helped this
I've looked through previous questions and none of them have really worked for me,
i have looked through other related questions but none of them giving me right
I have looked through these forums to find a solution to this problem, and
I have looked everywhere and found millions of python proxy servers but none do
I have looked at the php documentation, tutorials online and none of them how
I have looked around and have yet to find a solution to this problem.
I noticed that some enumerations have None as a enumeration member. For example what
I have a script which hides (display:none) certain divs in the list on page
I have looked at several variations on the Reachability example such as the Donoho

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.