views:

23

answers:

1

For simplicity's sake, lets say I have the following Abstract Base Controller Class:

public abstract class RESTController : Controller
{      
    public abstract JsonResult List();
    public abstract JsonResult Get(Guid id);
    public abstract JsonResult Create(object obj);
    public abstract JsonResult Update(object obj);
    public abstract JsonResult Delete(Guid Id);
}

For the Create & Update methods, I not only want to override the Method, but also the type of the parameter.

Typically I would use generics like so:

public abstract JsonResult Create<T>(T obj);

However this is an MVC action, and there is no way to specify type parameters.

What are my options? If I leave it as (object obj) will the MVC model binder work correctly?

var model = obj as MyViewModel;

This isn't very clean in any case. Any help would be appreciated.

A: 

How about something among the lines:

public abstract class RESTController<T> : Controller
{      
    public abstract JsonResult List();
    public abstract JsonResult Get(Guid id);
    public abstract JsonResult Create(T obj);
    public abstract JsonResult Update(T obj);
    public abstract JsonResult Delete(Guid Id);
}

and when overriding:

public FooController: RESTController<Foo>
{
    ...
    public override JsonResult Create(Foo obj)
    {
        throw new NotImplementedException();
    }
    ...
}
Darin Dimitrov
Wow, I feel stupid. Thanks...
Climber104