/// <summary> /// 增加关联子项到容器中。 /// </summary> /// <param name="val"></param> public void AddItem(DataRelationValue <TBaseModel> val) { //验证是否允许增加 checkAllowAdd(val); Dictionary <Type, List <TBaseModel> > childsData = null; if (_Childs.ContainsKey(val.Parent)) { childsData = _Childs[val.Parent]; } else { childsData = new Dictionary <Type, List <TBaseModel> >(); _Childs[val.Parent] = childsData; } Type childType = val.Child.GetType(); List <TBaseModel> datas = null; if (childsData.ContainsKey(childType)) { datas = childsData[childType]; } else { datas = new List <TBaseModel>(); childsData.Add(childType, datas); } datas.Add(val.Child); }
//检查是否允许增加 private int checkAllowAdd(DataRelationValue <TBaseModel> val) { if (val.Parent == null) { throw new MB.Util.APPException("父对象输入不能为空"); } if (val.Child == null) { throw new MB.Util.APPException("添加的子对象不能为空"); } var parentType = val.Parent.GetType(); if (!RELATION_DATA_TYPE.ContainsKey(parentType)) { throw new MB.Util.APPException(string.Format("父对象类型 {0} 还没有进行注册", parentType.FullName)); } var childs = RELATION_DATA_TYPE[parentType]; var childType = val.Child.GetType(); var map = childs.FirstOrDefault(o => o.EntityType == childType); if (map == null) { throw new MB.Util.APPException(string.Format("父对象类型{0} 下的子类型{1} 还没有进行注册", parentType.FullName, childType.FullName)); } return(1); }