如何使用c#建立类之间的关系?

时间:2016-07-10 18:49:27

标签: c# class

我正在开发一个允许我动态构建报告的项目。到目前为止,我只能使用2个类之间的直接关系来动态构建报告。 但是,为了使我能够使应用程序更加健壮,我希望能够通过递归或通过关系链找到类之间的关系。我将在下面详细解释

我有以下基类

public abstract class ReportTemplate
{
    public abstract List<IReportColumn> ReportColumns { get; }
    public abstract List<IReportRelationMapping> ReportRelationsMappings { get; }
    protected abstract IReportDataSource ReportDataSource { get; }
    private readonly IQueryExecutor queryExecutor;
    private readonly IQueryBuilder queryBuilder;

    protected ReportTemplate(IQueryBuilder queryBuilder, IQueryExecutor queryExecutor)
    {
        this.queryExecutor = queryExecutor;
        this.queryBuilder = queryBuilder;
    }

    public IReportModel CreateReport()
    {
        var model = new ReportModel(this.queryBuilder, this.queryExecutor);
        model.SetDataSource(ReportDataSource);
        foreach (var ReportRelationsMapping in this.ReportRelationsMappings)
        {
            model.AddReportRelationMapping(ReportRelationsMapping);
        }

        foreach (var repotColumn in this.ReportColumns)
        {
            model.AddReportColumn(repotColumn);
        }

        return model;
    }

}

然后我有多个扩展这个基类的类。以下是扩展我的基类ReportTemplate

的类的一些示例
public class CustomerTemplate : ReportTemplate
{
    private readonly IQueryExtractor QueryExtractor;
    private readonly TableMapper<Customer> customerMapper;
    private readonly TableMapper<Client> clientMapper;
    private readonly TableMapper<CustomerToTeam> customerToTeamMapper;
    private readonly TableMapper<Team> teamMapper;

    public CustomerTemplate(DbContext context)
        : base(new QueryBuilder(), new QueryExecutor(context.Database.Connection.ConnectionString))
    {
        this.QueryExtractor = new QueryExtractor(context);
        this.customerMapper = new TableMapper<Customer>(QueryExtractor, "Customer");
        this.clientMapper = new TableMapper<Client>(QueryExtractor, "CustomerTemplate_Client");
        this.customerToTeamMapper = new TableMapper<CustomerToTeam>(QueryExtractor, "CustomerTemplate_CustomerToTeam");
    }

    public override List<IReportColumn> ReportColumns
    {
        get
        {
            return new List<IReportColumn>
            {
                customerMapper.Column("Customer Id", x=> x.Id),
                customerMapper.Column("First Name", x => x.FirstName),
                customerMapper.Column("Last Name", x => x.LastName),
                customerMapper.Column("Phone", x => x.Phone),
                customerMapper.Column("E-Mail Adress", x => x.Email),
                customerMapper.Column("Client Id", x => x.ClientId),
                customerMapper.Column("Current Team Id", x => x.CurrentTeamId),

            };
        }
    }
    //This should contain direct relations only
    public override List<IReportRelationMapping> ReportRelationsMappings
    {
        get
        {
            return new List<IReportRelationMapping>
            { 
                //Relation to Client
                new ReportRelationMapping
                {
                    LocalRelation = this.customerMapper.GetForeignRelation(x => x.ClientId),
                    ForeignRelation = this.clientMapper.GetForeignRelation(x => x.Id),
                },

                //Relation to CustomerToClients
                new ReportRelationMapping
                {
                    LocalRelation = this.customerMapper.GetForeignRelation(x => x.Id),
                    ForeignRelation = this.customerToTeamMapper.GetForeignRelation(x => x.CustomerId),
                },

            };

        }
    }

    protected override IReportDataSource ReportDataSource
    {
        get 
        {
            return customerMapper.GetReportDataSource(); 
        }
    }

}

这是另一个,即CustomerToTeamTemplate

public class CustomerToTeamTemplate : ReportTemplate
{
    private readonly IQueryExtractor QueryExtractor;
    private readonly TableMapper<CustomerToTeam> customerToTeamMapper;
    private TableMapper<Customer> customerMapper;
    private TableMapper<Team> teamMapper;

    public CustomerToTeamTemplate(DbContext context)
        : base(new QueryBuilder(), new QueryExecutor(context.Database.Connection.ConnectionString))
    {
        this.QueryExtractor = new QueryExtractor(context);
        this.customerToTeamMapper = new TableMapper<CustomerToTeam>(QueryExtractor, "CustomerToTeam");
        var customerMapper = new TableMapper<Customer>(QueryExtractor, "CustomerToTeam_Customer");
        var teamMapper = new TableMapper<Team>(QueryExtractor, "CustomerToTeam_Team");
    }

    public override List<IReportColumn> ReportColumns
    {
        get
        {
            return new List<IReportColumn>
            {
                customerToTeamMapper.Column("Customer To Team Relation Id", x=> x.Id),
                customerToTeamMapper.Column("Related Customer Id", x => x.CustomerId),
                customerToTeamMapper.Column("Related Team Id", x => x.TeamId),                    
            };
        }
    }

    public override List<IReportRelationMapping> ReportRelationsMappings
    {
        get
        {
            return new List<IReportRelationMapping>
            { 
                //Relation to Team
                new ReportRelationMapping
                {
                    LocalRelation = customerToTeamMapper.GetForeignRelation(x => x.TeamId),
                    ForeignRelation = teamMapper.GetForeignRelation(x => x.Id),
                },

                //Relation to Customer
                new ReportRelationMapping
                {
                    LocalRelation = customerToTeamMapper.GetForeignRelation(x => x.CustomerId),
                    ForeignRelation = customerMapper.GetForeignRelation(x => x.Id),
                },

            };
        }
    }

    protected override IReportDataSource ReportDataSource
    {
        get 
        {
            return customerToTeamMapper.GetReportDataSource(); 
        }
    }
}

这是另一个课程

public class TeamTemplate : ReportTemplate
{
    private readonly IQueryExtractor QueryExtractor;
    private readonly TableMapper<Team> teamMapper;

    public TeamTemplate(DbContext context)
        : base(new QueryBuilder(), new QueryExecutor(context.Database.Connection.ConnectionString))
    {
        QueryExtractor = new QueryExtractor(context);
        teamMapper = new TableMapper<Team>(QueryExtractor, "Team");
    }

    public override List<IReportColumn> ReportColumns
    {
        get
        {
            return new List<IReportColumn>
            {
                teamMapper.Column("Team Id", x => x.Id),
                teamMapper.Column("Team Name", x => x.Name)
            };
        }
    }

    //There are no direct relations here!
    public override List<IReportRelationMapping> ReportRelationsMappings
    {
        get
        {
            return new List<IReportRelationMapping>();
        }
    }

    protected override IReportDataSource ReportDataSource
    {
        get
        {
            return teamMapper.GetReportDataSource();
        }
    }
}

正如您所看到的,每个派生类中的方法public override List<IReportRelationMapping> ReportRelationsMappings将定义当前类与其他类之间的直接关系。

例如,在我的班级CustomerToTeamTemplate中,第一个关系将在CustomerToTeamTemplate班级和TeamTemplate班级之间建立直接关系。同一个类还将包含CustomerClass的另一个关系。由于CustomerToTeamTemplate具有直接关系,我可以搜索relationMappings列表并找出所有关系或我可以采取的所有路由。 所以我可以采取的关系/方向将是以下之一

CustomerToTeamTemplate > CustomerTemplate
CustomerToTeamTemplate > TeamTemplate

现在,如果您查看CustomerClass课程,您会看到CustomerTemplateCustomerToTeamTemplate之间的关系。这将是这样的

CustomerTemplate > CustomerToTeamTemplate

但由于CustomerToTeamTemplate具有以下关系CustomerToTeamTemplate > TeamTemplate,因此我应该能够通过{CustomerTemplateTeamTemplate之间找出间接关系。 1}} class。

CustomerToTeamTemplate

我认为我可以做这种关系的一种方法是在CustomerTemplate > CustomerToTeamTemplate > TeamTemplate 类中创建类似关系时传递外来类的关系我会将关系改为这样的

CustomerTemplate

有更好的方法吗?

如何在班级之间建立这样的关系?

0 个答案:

没有答案