dimanche 10 juin 2018

Hide implementation details from developers in single assembly

I want to decouple Foo from FooHandler. Also they should be registered by DI via assembly scanning. Therefore I have 3 types:

interface IFooHandler { // no type arguments, so it can be used by reflection
   void DoStuff(object foo); 
}

interface IFooHandler<TFoo>: IFooHandler { // TFoo tells us, what foo is it for
   void DoStuff(TFoo foo); // developers are happy with strongly typed foos
}

abstract class FooHandler<TFoo>: IFooHandler<TFoo> {
   void IFooHandler.DoStuff(object foo){
       DoStuff((TFoo)foo); // adapter of interface 2 to 1
   }
   abstract void DoStuff(TFoo foo); // for nice implementation
}

The problem with such design is that I cannot easly forbid other people in my assembly from using IFooHandler or IFooHandler<TFoo> directly. Using first is bad idea, because it will not be registered in DI and it will fail. Using second is bad idea, because you write useless boilerplate, already written in base class.

Is it reasonable, to add Obsolete attribute with error=True to prevent people from using these interfaces? Is there is better way to hide them (I cannot move them to another assembly and mark as internal).

Aucun commentaire:

Enregistrer un commentaire