There is a @Mark Seemann's cite from a conversation which states that an abstraction must never implement IDisposable:

I like how @nblumhardt put it almost six years ago:

an interface [...] generally shouldn't be disposable. There's no way for the one defining an interface to foresee all possible implementations of it - you can always come up with a disposable implementation of practically any interface.

Is this applicable for the ISerializable interface as well (meaning that an abstraction must never implement it)?

  • 2
    You would implement ISerializable when you want the abstraction to have a custom serialization implementation, if I understand your question correctly. Inheriting from ISerializable compels the implementer to create an implementation, so you would inherit from it when you want to require it. – Robert Harvey Mar 9 '17 at 15:59
  • In the discussions I mentioned the point was that it's not possible to foresee if all the implementers will need IDisposable which is the reason not to make the abstraction as IDisposable. It moves this decision 'down' to the concrete implementers. You're right that inheriting ISerializable compels the implementer, but how do I know if all the future implementations will need to be serializable? – Serhii Shushliapin Mar 9 '17 at 16:19
  • 1
    You have to decide for yourself if that's the right thing to do. The IDisposable example only really applies to those things that dispose unmanaged resources, so if you don't have to do that, you don't need to force people to implement IDisposable, unless you want to co-opt it for closing database connections, closing HTML tags (yes, it's been abused to do that), and such things. – Robert Harvey Mar 9 '17 at 16:35
  • 1
    You would have to come up with a good use case to extend ISerializable from your interface. Off the top of my head, I can't think of one. – Jon Raynor Mar 9 '17 at 16:57
  • @Jon Raynor, if I understand you correctly, your point is that an abstraction should not be ISerializable. I tend to agree with that, but would like to check if somebody could share an example where it might be necessary. – Serhii Shushliapin Mar 9 '17 at 17:25

"An abstraction" and "an interface" are not the same thing. Classes, structures, methods, and other things are all abstractions. The discussion you quote is specifically about interfaces.

Interfaces in .net are like mixins without any implementation (a poor man's mixin). They specify what the class or structure must ultimately implement. For that reason, you should minimize the number of members in the interface, so that is more easily implemented. This means you should also minimize inheritance in interface design.

An interface inheriting from ISerializable means that all interface implementations must implement ISerializable. If the methods using your interface require ISerializable, in addition to other things, of course it makes sense to inherit from it. But this particular interface is rarely used.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.