Previously I thought that if there is background code for the same form, it should correspond to a set of services and can only serve this function. However, my requirements later overturned my understanding.
A line I made previously implements the following functions:
This is a line function. Combined with a workflow, four activity points correspond to four forms.
Later, when we made the fourth feature line, we found that part of it was the same as feature 1. However, the classes and objects used are different. But the page is slightly changed. You can still reuse pages.
The main functional requirements are the same. The class design is different from the subclass design. Then you need to modify the original code implementation.
View Design Drawing
Previously, all backend generics were the first, and the second generic class was required for subsequent page multiplexing, but they all inherited the same base class. How to save different generic classes based on different features. Make a form compatible with two different types, which need to be considered during design.
The solution is to add a selftype attribute to the base class and mark a user type. In this way, the base class is passed during data preparation, but it carries a certain type.
When a specific form page is displayed, the type is determined based on its own selftype, and different subclasses are converted to save the data.
Let's look at an instance.
Design ideas:
- First load the base class, and then determine whether it is null. If it is null, create the subclass type.
- If it is not null, determine the type of the Child class based on the parent class carrying type, load the Child class according to the ID, and point the parent class to the Child class.
- The data type of a form is strongly converted when it is saved Based on the Type carried by the parent class.
Control prepare data
//以下为MVC中control中数据准备 String resourceID = process.ResourceID;//父类加载数据,后续判断是否为空,指向不同自来EnsureKeyedCapabilityIndicatorFormBase data = EnsureKeyedCapabilityIndicatorFormBaseAdapter.Instance.Load(resourceID);int operateYear = WebUtility.GetRequestQueryValue("operateYear", DateTime.Now.SimulateTime().Year);if (data == null){//父类为空,应用指向子类,创建子类对象,//方便后续转换类型data = new EnsureGroupKeyedCapabilityIndicatorForm(){SelfTypeName = typeof(EnsureGroupKeyedCapabilityIndicatorForm).Name,ID = resourceID,CreatorID = user.ID,CreatorName = user.DisplayName,CreateTime = DateTime.Now.SimulateTime(),OperateYear = operateYear,PlanStartYear = operateYear + 1,Subject = operateYear + "确认历史能力",ProcState = WfProcessStatus.NotRunning};var companyCapabilitySummary = data.InitDefaultCompanyCapability();CompanyCapabilitySummaryTransAdapter.Instance.Update(companyCapabilitySummary);EnsureGroupKeyedCapabilityIndicatorFormAdapter.Instance.Update((EnsureGroupKeyedCapabilityIndicatorForm)data);}else{//已经是有数据了,根据父类携带类型,判断是子类的哪个类型,进行类型加载if (data.SelfTypeName == typeof(EnsureGroupKeyedCapabilityIndicatorForm).Name){data = EnsureGroupKeyedCapabilityIndicatorFormAdapter.Instance.Load(resourceID);}else{data = EnsureBusinessModelKeyedCapabilityIndicatorFormAdapter.Instance.Load(resourceID);var comanys = CompanyCapabilitySummaryTransAdapter.Instance.LoadByFormCode(resourceID);var enBusines = (EnsureBusinessModelKeyedCapabilityIndicatorForm)data;//子类还需要进行辨别是新增还是已经存在的商业模式,if (enBusines.BusinessModelExsitsType == BusinessModelExsitsTypeEnum.Exsits){//已经在初始化自身if (comanys.Count == 0){var companyCapabilitySummary = data.InitDefaultCompanyCapability();CompanyCapabilitySummaryTransAdapter.Instance.Update(companyCapabilitySummary);}}else{//新增,使用公共代替自身if (comanys.Count == 0){var companyCapabilitySummary = data.InitDefaultCompanyCapability(CompanyCapabilitySummaryTrans.IndustryAvgCodeConst);CompanyCapabilitySummaryTransAdapter.Instance.Update(companyCapabilitySummary);}}}}//返回基类数据,但是指向的却是之类。return new FormCommandStateGenericBase<EnsureKeyedCapabilityIndicatorFormBase>(){Data = data};
Save Form Background
The inheritance type of the form is base class.
public partial class EnsureCapabilityIndicatorHistoryView : ExtendViewBase<FormCommandStateGenericBase<EnsureKeyedCapabilityIndicatorFormBase>, EnsureKeyedCapabilityIndicatorFormBase>{//ViewData中DatA 为control中传递过来的数据(data)protected override void SaveApplicationData(WfExecutorDataContext datacontext){//还是根据携带类型辨别是哪个继承类,if (this.ViewData.Data.SelfTypeName == typeof(EnsureGroupKeyedCapabilityIndicatorForm).Name){//保存时进行数据类型强转(若没有control中的父类指向子类引用,这里的转换是报错的)EnsureGroupKeyedCapabilityIndicatorFormAdapter.Instance.Update((EnsureGroupKeyedCapabilityIndicatorForm)this.ViewData.Data);}else{EnsureBusinessModelKeyedCapabilityIndicatorFormAdapter.Instance.Update((EnsureBusinessModelKeyedCapabilityIndicatorForm)this.ViewData.Data);}}}
Summary:
The essence used here is the inheritance feature in the object-oriented model. The parent class reference points to the subclass, which is familiar to everyone,For exampleIlist <string> names = new list <string> (),An ilist parent class is instantiated and can only use the attributes of the parent class. If you want to use the list attributes, such as the foreach attribute of the list, such instantiation cannot be used, names must be converted to list. It is a good example, but if you can thoroughly understand and use it in design, you still need to thoroughly understand and think more.
Form compatibility Type Design