views:

88

answers:

1

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.

A: 

Well....what I did above worked well, but it was totally unnecessary.

What WAS necessary was that I had my mappings in place (or at least stuck to types I had mapped). I realized moments ago while debugging something else that I had everything I needed without the dynamic casting magic. Just this:

public virtual ActionResult Details(int procedureID)
{
    Procedure procedure = _repository.GetById(procedureID, false);
    return View(procedure);
}

This works due to the magic of joined-subclasses in nhibernate. Ask for superclass and you'll get an instance of the appropriate subclass cast as the superclass (if it's all set up properly, that is)

Way cool, but now I'm wondering about performance. I profiled the SQL call and it uses outer joins across all the child tables. That smells of bottleneck when there's lots of tables (40-50) and many records. Will have to ponder and perhaps inquire.

enth