Would creating a controller's interface
a bad idea? I have a controller that derived from ApiController
, so knowing that you can only derived one class to a class but able to derived one or more interface to a class.
public class FooController : ApiController
My problem is I have code that is in another controller that can be used in FooController
. I don't want to repeat the code so I'm thinking to create a base class that will hold common codes and create an interface for the base class. Would this be considered a bad idea or any suggestion on this?
ApiController
class BaseController : ApiController
, which implements the common methods. Then inherit the two controllers using the common code fromBaseController
:class FooController : BaseController
andclass BarController : BaseController
ApiController
only inController
? my problem is I have two controllers that use common codes. First controller is derived inController
; Second controller is derived inApiController
. If I'll be makingBaseController
derived fromApiController
then I would be having problem using it with the controller that does not derived fromApiController
butController
interface ICommonController
, you still have to implement the interface in all controllers:class FooController : ApiController, ICommonController
andclass BarController : Controller, ICommonController
. Depending on what the commonly used code does, you could however move it into another class (and possibly abstract the behaviour using an interface), which is then injected as dependency in the controllers.