Handling Model Inheritance in ASP.NET MVC2

Posted by enth on Stack Overflow See other posts from Stack Overflow or by enth
Published on 2010-05-14T03:09:52Z Indexed on 2010/05/14 3:14 UTC
Read the original article Hit count: 156

Filed under:
|

I've gotten myself stuck on how to handle inheritance in my model when it comes to my controllers/views.

Basic Model:

public class Procedure : Entity
{
    public Procedure() { }

    public int Id { get; set; }
    public DateTime ProcedureDate { get; set; }
    public ProcedureType Type { get; set; }
}

public ProcedureA : Procedure
{
    public double VariableA { get; set; }
    public int VariableB { get; set; }
    public int Total { get; set; }
}

public ProcedureB : Procedure
{
    public int Score { get; set; }
}

etc... many of different procedures eventually.

So, I do things like list all the procedures:

public class ProcedureController : Controller
{
    public virtual ActionResult List()
    {
        IEnumerable<Procedure> procedures = _repository.GetAll();
        return View(procedures);
    }
}

but now I'm kinda stuck. Basically, from the list page, I need to link to pages where the specific subclass details can be viewed/edited and I'm not sure what the best strategy is.

I thought I could add an action on the ProcedureController that would conjure up the right subclass by dynamically figuring out what repository to use and loading the subclass to pass to the view. I had to store the class in the ProcedureType object. I had to create/implement a non-generic IRepository since I can't dynamically cast to a generic one.

public virtual ActionResult Details(int procedureID)
{
    Procedure procedure = _repository.GetById(procedureID, false);
    string className = procedure.Type.Class;
    Type type = Type.GetType(className, true);
    Type repositoryType = typeof (IRepository<>).MakeGenericType(type);
    var repository = (IRepository)DependencyRegistrar.Resolve(repositoryType);
    Entity procedure = repository.GetById(procedureID, false);
    return View(procedure);
}

I haven't even started sorting out how the view is going to determine which partial to load to display the subclass details.

I'm wondering if this is a good approach? This makes determining the URL easy. It makes reusing the Procedure display code easy.

Another approach is specific controllers for each subclass. It simplifies the controller code, but also means many simple controllers for the many procedure subclasses. Can work out the shared Procedure details with a partial view. How to get to construct the URL to get to the controller/action in the first place?

Time to not think about it. Hopefully someone can show me the light. Thanks in advance.

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc-2