Choosing between derived classes and adding an additional property in entity framework

by renakre   Last Updated October 10, 2019 06:05 AM

I have the following StudentGroup class:

public class StudentGroup
{
    [Key]
    public int Id { get; set; }

    //the set of peers who will review the work of the StudentGroup
    [ForeignKey("PeerReviewGroupId")]
    public PeerReviewGroup PeerReviewGroup { get; set; }
    public int? PeerReviewGroupId { get; set; }

    public IEnumerable<GroupMembership> GroupMemberships { get; set; }
}

Derived from this class is PeerReviewGroup, in which there are two new properties:

public class PeerReviewGroup: StudentGroup
{
    [ForeignKey("StudentToReview")]
    public ApplicationUser StudentToReview { get; set; }
    public string StudentToReviewId { get; set; }

    [ForeignKey("StudentGroupToReview")]
    public StudentGroup StudentGroupToReview { get; set; }
    public string StudentGroupToReviewId { get; set; }

}

PeerReviewGroup could be assigned to an individual (i.e., StudentToReview) or another group (i.e., StudentrGoup). So, either of these values has to be null.

Instead, I could drive two classes:

public class PeerReviewGroupForIndividuals: StudentGroup
{
    [ForeignKey("StudentToReview")]
    public ApplicationUser StudentToReview { get; set; }
    public string StudentToReviewId { get; set; }
}

public class PeerReviewGroupForGroups: StudentGroup
{
    [ForeignKey("StudentGroupToReview")]
    public StudentGroup StudentGroupToReview { get; set; }
    public string StudentGroupToReviewId { get; set; }

}

I wonder if this is preferable or not, or which approach is the best practice. I think deriving two classes complicates things. Any suggestions?



Related Questions


Updated December 29, 2017 04:05 AM

Updated January 26, 2019 11:05 AM

Updated June 10, 2015 11:02 AM

Updated June 26, 2015 16:02 PM

Updated January 26, 2017 14:02 PM