private void BindToModelValue(EAV.Model.IModelAttribute attribute, EAV.Model.IModelInstance instance, EAV.Model.IModelValue value, ViewModelAttributeValue viewValue) { if (value == null && viewValue == null) { return; } if (value != null && viewValue == null) { if (value.ObjectState == ObjectState.New) { instance.Values.Remove(value); } else { value.MarkDeleted(); } return; } else if (value == null && viewValue != null) { value = new ModelValue() { Attribute = attribute, Instance = instance, Unit = attribute.VariableUnits.HasValue ? new ModelUnit() { UnitID = NextUnitID } : null }; } if (value.RawValue != viewValue.Value) { value.RawValue = viewValue.Value; } if (attribute.VariableUnits.HasValue) { if (attribute.VariableUnits.Value) { if (value.Unit.ObjectState == ObjectState.New && !String.Equals(value.Unit.DisplayText, viewValue.UnitText, StringComparison.InvariantCulture)) { value.Unit.DisplayText = viewValue.UnitText; } } else { if (value.UnitID != viewValue.UnitID) { value.Unit = attribute.Units.SingleOrDefault(it => it.UnitID == viewValue.UnitID); } } } }
private void BindToModelInstance(EAV.Model.IModelContainer container, EAV.Model.IModelSubject subject, EAV.Model.IModelInstance parentInstance, EAV.Model.IModelInstance instance, ViewModelInstance viewParentInstance, ViewModelInstance viewInstance) { if (instance == null && viewInstance == null) { return; } if (instance != null && viewInstance == null) { if (instance.ObjectState == ObjectState.New) { if (parentInstance != null) { parentInstance.ChildInstances.Remove(instance as IModelChildInstance); } else { subject.Instances.Remove(instance as IModelRootInstance); } } else { instance.MarkDeleted(); } return; } else if (instance == null && viewInstance != null) { if (parentInstance != null) { instance = new ModelChildInstance() { Container = container, InstanceID = viewInstance.InstanceID, ParentInstance = parentInstance }; } else { instance = new ModelRootInstance() { Container = container, InstanceID = viewInstance.InstanceID, Subject = subject }; } } foreach (ModelAttribute attribute in container.Attributes) { BindToModelValue(attribute, instance, instance.Values.SingleOrDefault(it => it.AttributeID == attribute.AttributeID), viewInstance.Values.SingleOrDefault(it => it.AttributeID == attribute.AttributeID)); } foreach (IModelContainer childContainer in container.ChildContainers) { var dataInstanceSubset = childContainer.Instances.Where(it => it.ParentInstanceID == instance.InstanceID); var viewInstanceSubset = viewInstance.ChildContainers.Where(it => it.ContainerID == childContainer.ContainerID).SelectMany(it => it.Instances).Where(it => it.ParentInstanceID == viewInstance.InstanceID); var dataInstances = dataInstanceSubset.GroupJoin(viewInstanceSubset, left => left.InstanceID, right => right.InstanceID, (left, right) => new { ModelInstance = left, ViewInstance = right.FirstOrDefault() }).ToArray(); var viewInstances = viewInstanceSubset.GroupJoin(dataInstanceSubset, left => left.InstanceID, right => right.InstanceID, (left, right) => new { ModelInstance = right.FirstOrDefault(), ViewInstance = left }).ToArray(); foreach (var item in dataInstances.Union(viewInstances)) { BindToModelInstance(childContainer, subject, instance, item.ModelInstance, viewInstance, item.ViewInstance); } } }