Skip to content

Automatic Interfaces #54

Closed
Closed

Description

@MaJaHa95

This is a feature request. I don't know whether this is more of a language or IDE feature, but I can imagine it going either way.

Problem

Consider the process of setting up a dependency injection environment for a class HomeController that depends on another class, DataAccess.

This process would typically (for me, at least) looks something like this:

  1. Create DataAccess
  2. Build an interface IDataAccess with exactly the same items (Visual Studio does this through a menu-item)
  3. Add a field and constructor (easy with the new constructor syntax) for HomeController that accepts IDataAccess dataAccess

That's pretty straight-forward.

But once you start changing those contracts, it gets a little more frustrating.

  1. Starting in HomeController, go find DataAccess (note that F12 doesn't work, since we're only looking at the interface at best here. You can F12 to the interface, then look at references to find the class, but...)
  2. Add the desired method to DataAccess
  3. Copy the method signature, then scroll to the top of the file (often a ways) and F12 to IDataAccess
  4. Paste the signature into the interface
  5. Go find HomeController again and write the line I wanted.

In reality, I often do this backwards via Ctrl+. by referencing a non-existent method, creating the method stub, then implementing the interface.

Proposal

It seems like we could do away with many of these steps using an interfaceof(T) language feature, similar to typeof or nameof. I can also see it as HomeController.interface.

In essence, interfaceof(T) would build a compile-time interface out of T that can be used just as we used IDataAccess before. The compiler would build a logical interface identical to what the IDE would when I right-click a class name.

  1. Create DataAccess
  2. Add a field and constructor (easy with the new constructor syntax) for HomeController that accepts interfaceof(DataAccess) dataAccess

Simple still.

Then for the other steps,

  1. Starting in HomeController, go find DataAccess (note that F12 does now work, since we have the class referenced here)
  2. Add the desired method to DataAccess
  3. Go find HomeController again and write the line I wanted.

My personal preference of adding the unknown method reference first then creating it with ctrl+. is also supported, since it will automatically add onto DataAccess.

If I wanted to inject something else, I could use very similar syntax:

public class DataAccessMocks : interfaceof(DataAccess)

If I had those mocks set up and building with my main project when I added a method, they would raise a compiler error because it didn't implement the (whole) interface.

In essence, I see this kind of as a reverse version of an abstract class. In an abstract class, as we know, the class can decide (or force) "I want children to inherit from me." Using interfaceof, the recipient of that class can say `I don't care whether what I receive has a polymorphic relationship with this class, but I do need its contract maintained exactly."

Potential Concerns

Now, of course, this isn't an alternative to interfaces. Interfaces do a whole lot that isn't useful here, particularly in that to use this interface, one would have to maintain a reference to DataAccess. It's mostly useful in this specific (albeit common) case of dependency injection.

Does this approach, when not in the light of dependency injection, open itself to any bad practices? If overused, this could offer some interesting security holes in code. Imagine if I accepted an interfaceof(DateTime) in the interest of having as open a contract as possible, but then someone passed me something that implemented the DateTime members very poorly. Could that be problematic? This seems like it would be either my fault, or that of the caller.

Conclusion

This seems like it could clean up a lot of dependency injected code, and it might even have alternative uses that I haven't immediately thought of. The current procedure is painstaking, and I'd love some more efficient way of handling this very common scenario.

Activity

  1. added this to the milestone on Jan 22, 2015
  2. MaJaHa95 commented on Jan 23, 2015

    @MaJaHa95
    Author

    I've thought about this some more, and I wonder whether what I'm actually looking for is another modifier.

    Essentially, this interfaceof(T) call would build an abstract form of the class.

    My use-case would be resolved if we had a modifier that essentially meant "I have an implementation, but it's secret and polymorphic children can't touch it." It would be a mixture between abstract and virtual, so that the implementation is complete, but whenever anyone tries to inherit from it, they're forced to implement every piece of the contract.

    I don't know if that's better or worse than interfaceof(T), since it's placing more weight on the class than on the user of that class.

  3. matwilko commented on Jan 23, 2015

    @matwilko

    A couple of problems/questions I see with this:

    How do you control the accessibility of the resulting interface? There's no single place to specify the accessibility because the interface itself isn't explicitly written by the developer, and if you were allowed to specify the accessibility at the point of interfaceof(T), the compiler would have to enforce them all being the same, and if you needed to change it, now you have n places to update, rather than 1.

    How do you version the interface? If I'm trying to implement your interface from an outside assembly, using interfaceof(YourType) because there's no explicit interface to name, how can you ever add more public methods to the referenced class, and thus adding more to the implied contract, without breaking the outside implementers? Wouldn't your proposal effectively remove any opportunity for non-breaking API changes?

    I think the problem you're facing is more something that tooling should resolve, not language features. For instance, your workflow for adding new methods to an interface isn't that onerous when using Resharper - just write out directly the usage of the new method, Alt+Enter to automatically add the method to the interface, and Alt+Enter on the class to add the newly missing interface member. While it is still a few steps, the majority of it is automated, and it feels natural enough.

  4. modified the milestone: on Nov 21, 2015
  5. Pilchie commented on Dec 4, 2015

    @Pilchie
    Member

    This doesn't actually seem like an IDE issue.

  6. CyrusNajmabadi commented on Dec 4, 2015

    @CyrusNajmabadi
    Contributor

    Why do you need IDataAccess?

  7. gafter commented on Jan 21, 2016

    @gafter
    Member

    We have no expectation of doing something like this.

  8. added a commit that references this issue on Nov 21, 2025
  9. added a commit that references this issue on Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions