提供的示例'实体继承'具有以下实体模型:
- 客户
- 公司延伸客户
- 人员延伸客户
- 订单
OrderEdit屏幕显示如何处理与可能是公司或人员的客户相关联的字段的继承。这很清楚。
但是,公司和个人的编辑屏幕不考虑继承:它们只是复制“电子邮件”字段,该字段通常从客户继承。
考虑到我此时的所有输入,如果我必须设计这些屏幕,我会提出以下方法。
1)CustomerEditFrame:使用电子邮件字段,没有定义数据源
2)PersonEditScreen:
- 人员数据源
- 在Person数据源上映射lastName和firstName字段
- 嵌入CustomerEditFrame
- 在CustomerEditFrame中注入Person数据源
3)CompanyEditScreen:
- 公司数据源
- 将行业领域映射到公司数据源
- 嵌入CustomerEditFrame
- 在CustomerEditFrame中注入公司数据源
然后,CustomerEditFrame负责在引用两个子类中的任何一个的数据源中编辑它所知道的字段子集。这种设计会起作用吗?
为了完整的文档,我认为这应该由样本涵盖,因为它是常见的情况。此外,它将是帧操作的一个很好的样本。
答案 0 :(得分:1)
屏幕应该考虑实体继承以消除代码重复,这是绝对正确的。我已经分析了示例项目here,以演示如何使用框架完成。
customer-frame.xml
包含基本实体的字段及其数据源:
<window xmlns="http://schemas.haulmont.com/cuba/window.xsd"
caption="msg://editCaption"
class="com.company.entityinheritance.gui.customer.CustomerFrame"
focusComponent="fieldGroup"
messagesPack="com.company.entityinheritance.gui.customer">
<dsContext>
<datasource id="customerDs"
class="com.company.entityinheritance.entity.Customer"
view="_local"/>
</dsContext>
<layout spacing="true">
<fieldGroup id="fieldGroup"
datasource="customerDs">
<column width="250px">
<field id="name"/>
<field id="email"/>
</column>
</fieldGroup>
</layout>
</window>
在CustomerFrame
控制器中有一个公共方法可以将实例设置为数据源:
public class CustomerFrame extends AbstractFrame {
@Inject
private Datasource<Customer> customerDs;
public void setCustomer(Customer customer) {
customerDs.setItem(customer);
}
}
公司编辑器company-edit.xml
包含框架而非客户字段:
<window xmlns="http://schemas.haulmont.com/cuba/window.xsd"
caption="msg://editCaption"
class="com.company.entityinheritance.gui.company.CompanyEdit"
datasource="companyDs"
focusComponent="customerFrame"
messagesPack="com.company.entityinheritance.gui.company">
<dsContext>
<datasource id="companyDs"
class="com.company.entityinheritance.entity.Company"
view="_local"/>
</dsContext>
<layout expand="windowActions"
spacing="true">
<frame id="customerFrame"
screen="demo$Customer.frame"/>
<fieldGroup id="fieldGroup"
datasource="companyDs">
<column width="250px">
<field id="industry"/>
</column>
</fieldGroup>
<frame id="windowActions"
screen="editWindowActions"/>
</layout>
</window>
在公司编辑器控制器中,注入框架并将已编辑的实例传递给它:
public class CompanyEdit extends AbstractEditor<Company> {
@Inject
private CustomerFrame customerFrame;
@Override
protected void postInit() {
customerFrame.setCustomer(getItem());
}
}