public static XPBaseObject GetXpObjectByKeyValue(XPObjectSpace oSpace, string value, Type type) { if (string.IsNullOrEmpty(value)) { return(null); } if (!type.IsSubclassOf(typeof(XPBaseObject))) { return(null); } var defaultMember = GetDefaultMember(type); object result; value.TryToChange(defaultMember.MemberType, out result); var criteriaOperator = GetDefaultCriteria(defaultMember, result); var item = (XPBaseObject)oSpace.FindObject(type, criteriaOperator, true); if (item != null) { return(item); } var nestedObjectSpace = oSpace.CreateNestedObjectSpace(); item = (XPBaseObject)nestedObjectSpace.CreateObject(type); defaultMember.SetValue(item, value); item.Save(); nestedObjectSpace.CommitChanges(); return(oSpace.GetObject(item)); }
public static XPBaseObject GetXpObjectByKeyValue(XPObjectSpace oSpace, string value, Type type) { if (string.IsNullOrEmpty(value)) return null; if (!type.IsSubclassOf(typeof(XPBaseObject))) return null; var defaultMember = GetDefaultMember(type); object result; value.TryToChange(defaultMember.MemberType, out result); var criteriaOperator = GetDefaultCriteria(defaultMember, result); var item = (XPBaseObject)oSpace.FindObject(type,criteriaOperator,true); if (item != null) return item; var nestedObjectSpace = oSpace.CreateNestedObjectSpace(); item = (XPBaseObject)nestedObjectSpace.CreateObject(type); defaultMember.SetValue(item, value); item.Save(); nestedObjectSpace.CommitChanges(); return oSpace.GetObject(item); }
public static XPBaseObject GetXpObjectByKeyValue(XPObjectSpace oSpace, string value, Type type) { if (string.IsNullOrEmpty(value)) { return(null); } if (!type.IsSubclassOf(typeof(XPBaseObject))) { return(null); } var keyPropertyName = oSpace.Session.GetClassInfo(type). PersistentProperties. OfType <XPMemberInfo>(). Where(p => p.HasAttribute(typeof(KeyPropertyAttribute))). Select(p => p.Name). FirstOrDefault() ?? oSpace.Session.GetClassInfo(type). PersistentProperties. OfType <XPMemberInfo>(). Where(p => p.Name == "Name" || p.Name == "Code") .Select(p => p.Name) .FirstOrDefault() ?? "Oid"; var item = (XPBaseObject)oSpace.FindObject( type, new BinaryOperator(keyPropertyName, value), true); if (item != null) { return(item); } var nestedObjectSpace = oSpace.CreateNestedObjectSpace(); item = (XPBaseObject)nestedObjectSpace.CreateObject(type); var firstOrDefault = item.ClassInfo .PersistentProperties .OfType <XPMemberInfo>() .FirstOrDefault(p => p.Name == keyPropertyName); if (firstOrDefault != null) { firstOrDefault.SetValue(item, value); } item.Save(); nestedObjectSpace.CommitChanges(); return(oSpace.GetObject(item)); }
public static XPBaseObject GetXpObjectByKeyValue(XPObjectSpace oSpace, string value, Type type) { if (string.IsNullOrEmpty(value)) return null; if (!type.IsSubclassOf(typeof(XPBaseObject))) return null; var keyPropertyName = oSpace.Session.GetClassInfo(type). PersistentProperties. OfType<XPMemberInfo>(). Where(p => p.HasAttribute(typeof(KeyPropertyAttribute))). Select(p => p.Name). FirstOrDefault() ?? oSpace.Session.GetClassInfo(type). PersistentProperties. OfType<XPMemberInfo>(). Where(p => p.Name == "Name" || p.Name == "Code") .Select(p => p.Name) .FirstOrDefault() ?? "Oid"; var item = (XPBaseObject)oSpace.FindObject( type, new BinaryOperator(keyPropertyName, value), true); if (item != null) return item; var nestedObjectSpace = oSpace.CreateNestedObjectSpace(); item = (XPBaseObject)nestedObjectSpace.CreateObject(type); var firstOrDefault = item.ClassInfo .PersistentProperties .OfType<XPMemberInfo>() .FirstOrDefault(p => p.Name == keyPropertyName); if (firstOrDefault != null) firstOrDefault.SetValue(item, value); item.Save(); nestedObjectSpace.CommitChanges(); return oSpace.GetObject(item); }