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 8763905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:53:36+00:00 2026-06-13T15:53:36+00:00

Say I’ve got the repository: interface IRepo { Foo Get(int id); void Add(Foo f);

  • 0

Say I’ve got the repository:

interface IRepo
{
    Foo Get(int id);
    void Add(Foo f);
}

Now, there is requirement that I can only have one Foo with given property, say…Id. Obviously, if I implement IRepo with some SQL backend and will map Foo.Id to Primary Key, I get something like this for free – I will get it probably as some kind of PKViolationException. But this is becoming implementation-specific, so wherever I use this IRepo implementation and start catching those exceptions, I’m losing benefits of loose coupling.

Now, how would I fix this? Should I add some kind of service layer that would first check whether the object exist, and then throw some exception that would be repository independent?

class Service
{
    IRepo repo;

    public void AddFoo(Foo foo)
    {
        if(repo.Get(foo.Id) != null)
            repo.Add(foo);
        else
            throw new FooAlreadyExistsException(foo.Id);
    }
}

This solution seems bad, because the repo.Add(foo) can still throw some exception, especially when object with given Id has been added right after the check (by some other activity), so I just added one additional call to my infrastructure for little or no benefits.

It seems like I should just be careful and implement the IRepo with such exception in mind (could catch PKViolationException and turn it into FooAlreadyExistsException in example SQL implementation), but what to do to be sure that every IRepo implementation follows such specification?

How do you tackle those issues generaly?

  • 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-06-13T15:53:37+00:00Added an answer on June 13, 2026 at 3:53 pm

    “It seems like I should just be careful and implement the IRepo with such exception in mind (could catch PKViolationException and turn it into FooAlreadyExistsException in example SQL implementation)”

    You’re right on the money with this. The exceptions being thrown become part of the interface contract, and implementations must adhere to this contract. The compiler will not enforce this for you so you have to be absolutely clear about the expectation.

    “but what to do to be sure that every IRepo implementation follows such specification?”

    As the interface writer you cannot be held responsible for the classes that implement it. If other classes are defective and expose leaky abstractions, that is their defect. All you can do is be absolutely clear about the contract you’re defining.

    The purpose of the repository is to abstract away the implementation details of the database, that includes its exceptions. So, you should also abstract away implementation specific exceptions…

    interface IRepo
    {
        /// <exception cref="FooAlreadyExistsException">Thrown if the specified Foo object already exists in the database.</exception>
        void Add(Foo f);
    }
    
    class ConcreteRepo
    {
        public void Add(Foo f)
        {
            try
            {
                // Database stuff...
            }
            catch (PKViolationException e)
            {
                throw new FooAlreadyExistsException(e);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a function that expects a block: void foo(Foo (^block)(Bar)); And say
Say I have a higher kinded type SuperMap[Key[_],Value[_]]`. Suppose now that I had something
Say I've got this array: MyArray(0)=aaa MyArray(1)=bbb MyArray(2)=aaa Is there a .net function which
Say I have a Student table, it's got an int ID. I have a
Say Suppose you have a class public class Person { public int PesronId{get;set;} public
Say I have 2 scripts test1.sh #!/bin/sh . ./test2.sh foo test2.sh #!/bin/sh foo(){ echo
Say I have a SSI script that uses exec, or a PHP script that
Say I have this: private list<myClass> myCollection; Is there a programming idiom to shorten
Say our client is sending the packets at a constant rate. Now, if server
Say I have the following, is there a way to chain them together, or

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.