正确的模式来设计亲子班 [英] Right Pattern to design Parent-Child class

查看:192
本文介绍了正确的模式来设计亲子班的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设计亲子班

请注意:这里父不是基类,而是显示一对多的关系。

Note:Here Parent is not base class rather it shows one to many relationship.

办法A

public class Company
{
   public int CompanyID;
   ...
   public IList<Branch> Branches;
}

public class Branch
{
}

Appraochb

public class Company
{   
   public int CompanyID;
   ...
}

public class Branch
{
   public int CompanyID; //which represents company object
}



分公司屏幕定稿UI设计:=

Finalized UI Design of Branch Screen:=

显示在树视图查看屏幕: -

Displaying in Tree-view for view screen:-

Company A
    Branch 1
    Branch 2
Company B
    Branch 3
    Branch 4
    Branch 5

添加/编辑画面(2个独立的屏幕): -

Add/Edit Screens(2 separate screen):-

Company - Company Drop-down
other attributes of Branch

1)如果我们看到查看屏幕,然后做法A看起来不错,如果我们看到添加/编辑屏幕的方法b看起来不错。

1) If we see View screen then Approach "A" looks fine and if we see Add/Edit screen Approach "B" looks fine.

2),而使用方法A,当我不得不返回分公司数据,那么我就返回公司类型,而不是分支类型:

2) while using Approach "A", when I have to return branch data then I have to actually return Company type instead of Branch Type:

Company GetBranch(int BranchID)

3)我们的数据库的设计是基于法b,其中分支表店CompanyID

3) Our DB design is based on Approach "B" where Branch Table stores CompanyID

有人可以提出什么好的方法去用?

Can somebody suggest what is the good approach to go with ?

推荐答案

IMO没有权利去做这个。这一切都取决于你打算如何使用和存储/检索数据。

IMO there is no "right" to do this. It all depends on how do you plan to use and store/retrieve the data.

保存根据公司的枝条我造成巨大的流量开销(正如你将百达通各分支机构,即使所有你关心的是公司或特定的分支),使用单独的ID我才能加载所有relevnat数据导致额外的查询。
我将与实施类似实体框架是如何工作去:

Storing the Branchs under the Companies my cause huge traffic overhead (as you will allways pass ALL the branches even when all you care about is the company or specific branch) , using ID alone my cause additional "queries" in order to load all the relevnat data. I would go with implementation similar to how the Entity framework works:

我会有分支公司ID信息库,并保持导航性能(Comany.Branches, Branch.Company),并有填充需要时,将只称为属性的功能。

I would have branch and company id repository and keep the navigational properties (Comany.Branches, Branch.Company) and have a function that fills the properties that would be called only when needed.

例如,你可以有 GetCompany(ID) 函数返回只有公司和 GetCompanyWithBranche(ID,INT?brandchid)将返回(如果充满了它的分支公司物体或单支ID指定)。这样,您就能够确定的多少数据以检索。

For example you can have GetCompany(id) function that returns only the company and GetCompanyWithBranche(id,int? brandchid) that will return the company object filled with its branches (or single branch if id is specified). This way you will be able to determinate how much data to retrive.

这篇关于正确的模式来设计亲子班的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆