If I want to rename an interface without breaking existing code, would this work?
The old interface:
namespace RenameInterfaceNicely
{
/// <summary>
/// The old name of the interface.
/// </summary>
[Obsolete("IFoo is replaced by IFooNew.")]
public interface IFoo
{
void Test();
}
}
The new, renamed, interface:
namespace RenameInterfaceNicely
{
#pragma warning disable 0618
/// <summary>
/// IFooNew new name for the interface IFoo.
/// </summary>
public interface IFooNew : IFoo
{
}
#pragma warning restore 0618
}
So if I have a class Foo that used the old interface, that I now change to use the new interface. Will users of Foo get in trouble?
Foo before the change:
public class Foo : IFoo
{
public void Test()
{
// Do something
return;
}
}
Foo after the change:
public class Foo : IFooNew
{
public void Test()
{
// Do something
return;
}
}
Example of existing code:
// Old code
IFoo oldFoo = new Foo();
oldFoo.Test();
IFoo oldFoo2 = new Bar().GetFoo();
...
Bar bar = new Bar();
bar.DoSomethingWithFoo(oldFoo);
Example of new code:
// New code
IFooNew newFoo = new Foo();
newFoo.Test();
IFooNew newFoo2 = new Bar().GetFoo();
...
Bar bar = new Bar();
bar.DoSomethingWithFoo(newFoo);
My question was inspired by this one: A definitive guide to API-breaking changes in .NET and the fact that someone made a breaking change by renaming some interfaces that I was using.
In the case of a genuine rename: everyone will need to recompile to pick up that change. It is a dangerous change, frankly. Interfaces are contracts, and should not be renamed. If the rename is unavoidable, however, this does at least let them know what to change it to.
--
However, actually: what you have demonstrated isn't a rename; it is a semi-compulsory extension; the IFoo still exists and still has the old features. Presumably you want people to start providing IFooNew implementations. That's reasonable, but you should aim to ensure that if a consumer only implements IFoo, then it continues to work.
Renaming a public type or member is a breaking change.
Deriving a new interface from the old one is still breaking:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With