试图找出这些接口

本文关键字:接口 | 更新日期: 2023-09-27 18:29:19

我正在尝试创建一些接口。IReportSection对象将有一个字符串和一组项,根据我们使用的内容,这些项可能会有所不同。我需要使它通用吗?

IReport将具有一个字符串和一个IReportSection的集合。

以下是我现在试图定义它的方式。

public interface IReport
{
    string ReportName { get; set; }
    ICollection<IReportSection> ReportSections { get; }
}
public interface IReportSection
{
    string ReportSectionName { get; set; }
    ICollection ReportItems { get; }
}
public abstract class ReportSectionBase : IReportSection
{
    public string ReportSectionName { get; set; }
    public ICollection ReportItems { get; set; }
} 

我的型号:

pulic class ProjectSubmissionViewModel
{
    public int ProjectSubmissionId { get; set; }
    public string SubmissionTitle { get; set; }
}
pulic class AffiliateViewModel
{
    public int AffiliateId { get; set; }
    public string AffiliateName { get; set; }
}

这就是我试图在代码中使用它的方式:

public class ChapterAffiliates : ReportSectionBase
{
    public string ReportSectionName { get { return "Chapter Affiliates"; } }
    public ICollection<AffiliateViewModel> ReportItems { get; set; }
}
public class ChapterTitles : ReportSectionBase
{
    public string ReportSectionName { get { return "Chapter Titles"; } }
    public ICollection<ProjectSubmissionViewModel> ReportItems { get; set; }
}
public class SubmissionListViewModel : IReport
{
    public ICollection<ProjectSubmissionViewModel> Submissions { get; set; }
    public ICollection<AffiliateViewModel> Affiliates{ get; set; }
    public string ReportName { get; set; }
    public ICollection<IReportSection> ReportSections
    {
        get
        {
            var affiliateSection = new ChapterAffiliates
            {
                ReportItems = Affiliates
            };
            var titleSection = new ChapterTitles 
            {
                ReportItems = Submissions.Where(s => s.SubmissionTitle.Contains("SomePhrase")).ToList()
            };
            var sections = new List<IReportSection> { {subSection}, {titleSection} };
            return sections; 
        }
    }
}

我不知道如何最好地定义它。我很确定我以前做过,但我不会想到。

试图找出这些接口

某个报表中TRType的类型参数是否都相同?例如,您会有不同报告类型的报告部分吗?

如果报告中的所有类型都相同,则解决方案相对简单:

public interface IReport<T> { ... }

如果不是这样的话,你将不得不做一些不同的事情,例如:

public interface IReportSection
{
    string ReportSectionName { get; }
    ICollection ReportItems { get; }
}
public abstract class ReportSectionBase<TRType> : IReportSection {
   ...
} 

这允许您在与报表相关的ReportSections集合中放置不同的基础类型。您必须做更多的工作才能从每个报告部分获得所需的确切信息。