Hacker News new | ask | show | jobs
by piedar 2092 days ago
Nominally-typed interfaces are still clumsy in some circumstances. For example, if I consume a library with

    namespace ThirdParty {
      public sealed class Foo { public void Do() { } }
    }
and I want to hide it behind

    namespace MyCode {
      interface IFoo { void Do(); }
    }
the compiler does not accept ThirdParty.Foo as an implementation of MyCode.IFoo so I must define a wrapper

    namespace MyCode {
      sealed class ThirdPartyFooAdapter : IFoo {
        public ThirdPartyFooAdapter(ThirdParty.Foo instance) {
          _instance = instance;
        }
        readonly ThirdParty.Foo _instance;

        public void Do() => _instance.Do();
      }
    }
which only gets more tedious as the number of methods and implementations grows.
1 comments

While I agree, I feel that third-party library consumption is a bit of an edge case (i.e. the majority of your interface implementations won't be wrappers around third-party stuff).

Whenever I do work closely with a third-party library, the wrapping pays for itself quite quickly because it never takes long for you to find a 'quirkiness' or unsuitability in how the library implements something and you end up with code resembling this:

   public void Do() {
     _instance.SpecialOptionForTheBehaviourYouRequire = true;

     try
     {
       _instance.Do();
     }
     catch
     {
       // Workaround for bug that has not been fixed in ThirdParty Library yet. Remove when fixed!
       _instance.ResetBrokenStateCausedByBug();
       _instance.Do();
     }
   }