In the vocational training management system, I have an abstract CourseBase class, which I decided to use in favor of the ICourse interface, because I would rather avoid duplicating the implementation code for all classes derived from the hypothetical, basic Course object. Each course has a list, if subjects, with any subject defined by the abstract SubjectBase class. So, I have, for example.
public abstract class CourseBase : BaseObject { public IEnumerable<SubjectBase> Subjects { get { return new List<SubjectBase>(); } } } public abstract class SubjectBase { public string Name { get; set; } public string Description { get; set; } public int ValidityPeriod { get; set; } }
Now I want to add a specific LocalCourse class that contains a collection of LocalCourseSubject objects, but since I do not use the interface for CourseBase , I lose covariance and I need to hide the abstract base Subjects property with my new one:
public class LocalCourse: CourseBase { public IEnumerable<LocalCourseSubject> Subjects { get { throw new NotImplementedException(); } } }
I am sure that I am missing something very obvious here from the point of view of OO, but the only solutions that I see are:
- Completely omit objects from the abstract base and add only the specially typed property of the collection to derived classes.
- Implement an interface, such as
ISubjectCollectionOwner in an abstract database, as well as specific classes.
Please excuse my dullness here, it has been a while since I was happy to encounter a design problem like this.
source share