1

I have a base controller:

public abstract class EntityController<T> : BaseController
{
    public EntityController(ILogService logService) : base(logService) { }

    [HttpGet]
    public abstract IEnumerable<T> Get();

    [HttpGet]
    public abstract T Get(int id);

    [HttpPost]
    [ValidateModel]
    public abstract IHttpActionResult Create(T dto);

    [HttpPut]
    [ValidateModel]
    public abstract IHttpActionResult Update(T dto);

    [HttpDelete]
    public abstract IHttpActionResult Delete(int id);
}

Everything works fine on most controllers inheriting this class. However, I have a few controllers where I would like to the 'hide' the Get() action.

Is it possible to do so at the action level, or should I throw a MethodNotFound exception?

Also, is it best practice for all of the above actions to return IHttpActionResult instead of IEnumerable and T ?

Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263
  • I don't have the knowledge to answer your question, but I found it interesting, so I made a little research and come up with [this](http://stackoverflow.com/questions/106383/c-sharp-can-publicly-inherited-methods-be-hidden-e-g-made-private-to-derived). Accepted answer makes a good explanation so I liked it too :) Hope it helps. – Tolga Evcimen Oct 09 '14 at 12:12

1 Answers1

1

This base class without any implementation at all makes not sense. Why do you want it? If you need to abstract some behaviour, you'd better use an interface. And using an interface gives much more flexibility. You can even define and implement different interfaces.

Using a base class of this kind makes sense if there is a common implementation, shared by all inheriting classes. In this particular case you have the T type parameter, which could allow you to implement and share a generic implementation. That would make this class more useful. If you did so, you should declare the methods as virtual, so that you have the chance to change the implementation for special cases, and otherwise use the base implementation.

As to returning IHttpActionResult or a particular type or collection, it's perfectly natural, and more informative, when you're implementing Web API. So you should change the return type to a particular type (for example T or a collection of T) where it makes sense.

You can even combine the idea of interface, and base clase (with some implementation). If you do so you can have a base class with all the functionality, and a set of interfaces, which would allow you to expose only the desired functionality.

JotaBe
  • 38,030
  • 8
  • 98
  • 117