Beispiel #1
0
        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));
        }
Beispiel #2
0
        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);

        }
Beispiel #3
0
        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));
        }
Beispiel #4
0
        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);

        }