Question

So, I have a inheritance hierarchy in my Entity Model as bellow:

class Member
{
    public virtual Information Information { get; set; }
    public long InformationId { get; set; }

    //Other Props
}
class Staff : Member
{
}
class Guest : Member
{
}

class Information
{
    public string Name { get; set; }
}

class StaffInformation : Information
{
    public DateTime BirthDate { get; set; }
}

class GuestInformation : Information
{
    public DateTime Expiry { get; set; }
}

In view, I've trouble to cast a Member Information into correct child. For example, I want to:

TextBoxFor(model => model.Staff.StaffInformation.BirthDate)

but what I can do is:

TextBoxFor(model => (Entities.StaffInformation)(model.Staff.Information).BirthDate)

Can I specify the type of Information in a Child? some thing like following pseudo:

class Staff : Member
{
    public StaffInformation Information { get; set; }
}
class Guest : Member
{
    public GuestInformation Information { get; set; }
}

Any suggestion?

Was it helpful?

Solution

This is a prime candidate for ViewModels.

Your entity model and your view models do not have to be one-to-one. That is the job of a controller (service class, controller, whatever) to do. Convert from one to the other.

It seems silly to upcast a BirthDate on one view.. then upcast an Expiry date on another. This is exactly what a ViewModel is for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top