Ed King
Ed King

Reputation: 1863

How to version a C# interface?

I have a C# interface, IFoo, which can be implemented by multiple parties in multiple projects. What I want to do is version the interface so that I can identify which version of the interface an implementation was developed against.

Is there a preferred/conventional way of doing this? I thought about putting a read-only InterfaceVersion property into the interface spec, but that can be defined by the implementer, not by me as the designer of the interface, and so isn't what I want.

Upvotes: 2

Views: 1600

Answers (3)

Sam Harwell
Sam Harwell

Reputation: 99869

This is a very complicated topic when you consider the full impact of various decisions. I have started the process of documenting this in the following topic:

Assembly Versioning in Extensible Applications

This document focuses on Visual Studio 2010 and newer, which is itself an extensible application with multiple versions released to date. The extension mechanism in place is MEF, but the rules and restrictions it imposes still apply to other extension mechanisms.

This document is a work-in-progress. Suggestions and/or problems can be reported on the issue tracker.

Upvotes: 3

Jon
Jon

Reputation: 437336

but that can be defined by the implementer, not by me as the designer of the interface, and so isn't what I want

This passage contains the answer to your own question. What you want is something that can only be defined by you (the designer), so it has to be something 100% contained in the assemblies you provide. Assemblies contain types, therefore this something must be a type.

Obviously you cannot change any interface after you have shipped it (that would break the code all your clients), so the only option remaining is to define a new interface. Implementers can then opt-in to the new version by choosing to implement it.

Upvotes: 0

Mike Cheel
Mike Cheel

Reputation: 13106

Typically one doesn't version an interface because it is considered a contract (and contracts should be enforced not broken). Typically a new interface is created with the new functionality (IFoo2 perhaps) and classes that need to participate in the new contract will then implement that.

public class Foo : IFoo, IFoo2 
{
    // Implementation goes here
}

Upvotes: 2

Related Questions