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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:57:18+00:00 2026-06-08T20:57:18+00:00

Learning more about code contracts, they seem like something I want to use in

  • 0

Learning more about code contracts, they seem like something I want to use in my project.

Given the way that I’m structuring my web-service-layer (an abstraction of our service layer accessed from our MVC controllers) I find myself wondering why it’s not possible to specify different contracts for the various permutations of an interface implementation.

Specifically I’m interested in the reasons behind only being able to specify the contracts in a 1-1 fashion given a generic interface method.

Here’s an example of my code structure and I’ll aim to identify how I’d like to use code contracts. I’m sure someone with more experience will be able to nudge me in the right direction.

I’m using a CQRS style approach such that:

public interface IQuery<in TInput input,out TOutput output>
{
     TOutput Invoke(TInput request)
}

public interface IGetSomeUnicornsFromAMagicalLand : 
                                             IQuery<int, IEnumerable<Unicorn>>{}

 // Implementation
public class GetSomeUnicornsFromMagicLand : IGetSomeUnicornsFromAMagicalLand
{
    public IEnumerable<Unicorn> Invoke(int numberOfUnicornsToReturn)
    {
       // Here I'd like to specify some preconditions on the input, 
       // specific to type int

       return _wizardry
                 .GetMagicCreature<Unicorn>(numberOfUnicornsToReturn)
                 .DoMagicalConversionToEnumerable()
    }

}

Given this context it seems reasonable to want to specify contracts at the implementation level rather than in an abstract class designed to apply contracts on the interface (as a generalised mechanism).

  • What are some of the reasons for not being able to do this?
  • Are there other approaches that cater for this?
  • Is this just not a good structure if I want to use code contracts?
  • 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-08T20:57:19+00:00Added an answer on June 8, 2026 at 8:57 pm

    In light of the Liskov Substitution Principle, in general I’d say it does make more sense to specify these contracts at the interface level.

    The LSP basically says that each implementation of an abstract concept should be interchangeable without the user of the abstract concept having to know the difference. In other words, your code should not start to break because you switch to another implementation of a certain interface.

    By imposing implementation-specific additional requirements on the input arguments, you are effectively coupling the calling code (which is in your case most likely framework-level code dealing with the general concept of IQuery), to the specific implementation.

    For your specific situation that reasoning may or may not apply (that’s why it’s a principle, it’s not written in stone), but obviously a general framework like code contracts can only take general principles into account 🙂

    EDIT: After thinking this through some more, it seems to me that the derived interface is probably the ‘real’ one, not the generic IQuery<TInput, TOutput>. The generic base interface seems to be more of an ‘implementation detail’, you can indeed not really make any meaningful assumptions (contracts) on this generic base interface. Only at the concrete level can you make any meaningful reasonings about these input and output parameters.

    Maybe we need C++’s private inheritance (or implementation inheritance) in C# to ‘hide’ this interface from consumers. Read this excellent answer to a related question. It also talks about Liskov Substitutability being “a lie” in this case, which I’m inclined to agree with.

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

Sidebar

Related Questions

I'm trying to code a exe packer/protector as a way of learning more about
I want to start learning more about using SWIG and other methods to interface
I am learning JPA. I read about persistence.xml file. It can contain more that
I'm interested in learning more about pattern recognition. I know that's somewhat of a
I've been reading/learning more about Spring lately, and how one would use Spring in
I'm on my way to learning more about javascript and it could get really
I am on the Path of learning more about mysqli and all that exciting
asked a question on my project earlier but it seems im learning more about
I would like to learn more about osdev. So I thought about learning from
I am interested in learning more about manipulating network traffic to utilize LAN only

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.