mensi
mensi

Reputation: 9826

Automatically pass Entity to Controller Action

When adding a controller for a model, the generated actions will look something like this

public ActionResult Edit(int id = 0)
{
    Entity entity = db.Entities.Find(id);
    if (entity == null)
    {
        return HttpNotFound();
    }
    return View(entity);
}

Now in my case I take a string id which can map to DB IDs in several ways, producing several lines of code for retrieval of the correct entity. Copy&pasting that code to every action which takes an id to retrieve an entity feels very inelegant.

Putting the retrieval code in a private function of the controller reduces the amount of duplicate code but I'm still left with this:

var entity = GetEntityById(id);
if (entity == null)
    return HttpNotFound();

Is there a way to perform the lookup in an attribute and pass the entity to the action? Coming from python, this could easily be achieved with a decorator. I managed to do something similar for WCF services by implementing an IOperationBehavior which still does not feel as straight-forward. Since retrieving an entity by id is something you frequently need to do I'd expect there to be a way other than copy&pasting code around.

Ideally it would look something like this:

[EntityLookup(id => db.Entities.Find(id))]
public ActionResult Edit(Entity entity)
{
    return View(entity);
}

where EntityLookup takes an arbitrary function mapping string id to Entity and either returns HttpNotFound or calls the action with the retrieved entity as the parameter.

Upvotes: 5

Views: 1595

Answers (2)

qujck
qujck

Reputation: 14578

If you are repeating similar code then you can use an extension method.

public static class ControllerExtensions
{
    public static ActionResult StandardEdit<TEntity>(
        this Controller controller, 
        DbContext db, 
        long id)
        where TEntity : class
    {
        TEntity entity = db.Set<TEntity>().Find(id);
        if (entity == null)
        {
            return controller.HttpNotFound();
        }
        return controller.View(entity);
    }
}

public ActionResult Edit(long id = 0)
{
    return this.StandardEdit<Client>(db, id);
}

If you are really repeating exactly the same code a number of times then this should be resolved with inheritance.

Create a common Controller that inherits from Controller

public class StandardController : Controller
{
    public ActionResult Edit<TEntity>(long id = 0)
        where TEntity : class
    {
        TEntity entity = db.Set<TEntity>().Find(id);
        if (entity == null)
        {
            return HttpNotFound();
        }
        return View(entity);
    }
}

and change your model controllers to inherit from this new controller:

public class ClientController : StandardController
{
    public ActionResult Edit(long id = 0)
    {
        return base.Edit<Client>(id);
    }
}

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039298

You could write a custom ActionFilter:

public class EntityLookupAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // retrieve the id parameter from the RouteData
        var id = filterContext.HttpContext.Request.RequestContext.RouteData.Values["id"] as string;
        if (id == null)
        {
            // There was no id present in the request, no need to execute the action
            filterContext.Result = new HttpNotFoundResult();
        }

        // we've got an id, let's query the database:
        var entity = db.Entities.Find(id);
        if (entity == null)
        {
            // The entity with the specified id was not found in the database
            filterContext.Result = new HttpNotFoundResult();
        }

        // We found the entity => we could associate it to the action parameter

        // let's first get the name of the action parameter whose type matches
        // the entity type we've just found. There should be one and exactly
        // one action argument that matches this query, otherwise you have a 
        // problem with your action signature and we'd better fail quickly here
        string paramName = filterContext
            .ActionDescriptor
            .GetParameters()
            .Single(x => x.ParameterType == entity.GetType())
            .ParameterName;

        // and now let's set its value to the entity
        filterContext.ActionParameters[paramName] = entity;
    }
}

and then:

[EntityLookup]
public ActionResult Edit(Entity entity)
{
    // if we got that far the entity was found
    return View(entity);
}

Upvotes: 3

Related Questions