如何正确安排(My)Task类。 (上流-下流问题)

时间:2019-05-04 22:48:22

标签: c# asp.net-mvc entity-framework

我有课程 Course ,其中包含4个相似的类集合(VideoTask,QuizTask,TextTask,QuestionTask),并且它们都有基类Task.Each 任务属于主题(由TopicId定义),并且还具有 Order 属性(因为我需要在每个主题中保存任务的顺序)。

Course.cs

public class Course: INamedEntity
{
  public string Name { get; set; }
  public int Id { get; set; }


  public  ICollection<Topic> Topics { get; set; }


  public  ICollection<QuizTask> QuizTasks { get; set; }

  public  ICollection<QuestionTask> QuestionTasks { get; set; }

  public virtual ICollection<TextTask> TextTasks { get; set; }

  public ICollection<VideoTask> VideoTasks { get; set; }


}

任务示例

public class VideoTask : Task
{
  public int TopicId { get; set; }
  public int Order { get; set; }

  public string VideoUrl { get; set; }
}

我的问题是-如何以最佳方式安排这种结构?因为现在我应该做“糟糕的”事情,例如:

从EF单独获取此任务,然后将其上传到集合中

public IEnumerable<Task> GetTasksOfCourse(TaskFilter filter)
{
  var course = GetFullCourse(filter.CourseId);

  List<Task> tasks = new List<Task>();
  tasks.AddRange(course.QuestionTasks.Where(t=>t.TopicId == filter.TopicId));
  tasks.AddRange(course.QuizTasks.Where(t => t.TopicId == filter.TopicId));
  tasks.AddRange(course.TextTasks.Where(t => t.TopicId == filter.TopicId));
  tasks.AddRange(course.VideoTasks.Where(t => t.TopicId == filter.TopicId));

  return tasks;
}

然后,在视图中,我必须将它们向下投射:

@model IEnumerable<Task>

@foreach (var task in Model)
{
    if (task is QuestionTask)
    {
        @await Html.PartialAsync("Partial/_QuestionTask", (QuestionTask)task)
    }
    if (task is QuizTask)
    {
        @await Html.PartialAsync("Partial/_QuizTask", (QuizTask)task)
    }
    if (task is TextTask)
    {
        @await Html.PartialAsync("Partial/_TextTask", (TextTask)task)
    }
    if (task is VideoTask)
    {
        @await Html.PartialAsync("Partial/_VideoTask", (VideoTask)task)
    }
}

所有这些看起来都很丑陋,但我不知道该怎么办。

0 个答案:

没有答案