Beispiel #1
0
 public EnumProperty(ISettablePropertyDef pDef, string propName, Type propType, Func <object, object> getter, Action <object, object> setter)
     : base(pDef, propName, BuildDescription(propType, pDef.Description))
 {
     this.expectedType = propType;
     this.GetF         = MakeGetter(getter, this, this.Convert);
     this.SetF         = MakeSetter(setter, this, this.Convert);
 }
        internal static IPropDef BuildByMemberInfo(MemberInfo info, ISettablePropertyDef candidate)
        {
            string name = info.Name;
            Type   ty;
            Func <object, object>   getter;
            Action <object, object> setter;

            switch (info)
            {
            case PropertyInfo pi:
                ty     = pi.PropertyType;
                getter = pi.GetValue;
                setter = pi.SetValue;
                return(MakeTypedProperty(candidate, name, ty, getter, setter));

            case FieldInfo fi:
                ty     = fi.FieldType;
                getter = fi.GetValue;
                setter = fi.SetValue;
                return(MakeTypedProperty(candidate, name, ty, getter, setter));

            default:
                ThrowHelper.ThrowNotSupportException($"Trying to build property definitions but that only works with Properties and Fields. This is a {info.MemberType}.");
                return(null);
            }
        }
        public DataEntityPropertyDef(ISettablePropertyDef pDef, string fieldName, Type dataEntityType, Func <object, object> getter, Action <object, object> setter)
            : base(pDef, fieldName)
        {
            var attr = dataEntityType.GetCustomAttribute <ObjectDefinitionAttribute>();

            this.PropertyType   = string.IsNullOrWhiteSpace(attr.Name) ? dataEntityType.Name : attr.Name;
            this.DataEntityType = dataEntityType;
            this.getter         = getter;
            this.setter         = setter;
        }
Beispiel #4
0
        public SimpleProperty(ISettablePropertyDef pDef, string propName, Type propType, Func <object, object> getter, Action <object, object> setter)
            : base(pDef, propName)
        {
            if (propType != this.ExpectedType)
            {
                ThrowHelper.ThrowTypesDoNotMatch(propType, this.ExpectedType, pDef);
            }

            this.GetF = MakeGetter(getter, this, this.Convert);
            this.SetF = MakeSetter(setter, this, this.Convert);
        }
Beispiel #5
0
 private static Func <object, T> MakeGetter(Func <object, object> getValue, ISettablePropertyDef context, Func <object, T> convert)
 {
     return(target =>
     {
         if (target == null)
         {
             ThrowHelper.ThrowGetterTargetNull(context);
         }
         var val = getValue(target);
         return convert(val);
     });
 }
Beispiel #6
0
 public PropDefBase(ISettablePropertyDef pDef, string fieldName)
 {
     this.Name                  = string.IsNullOrWhiteSpace(pDef.Name) ? fieldName : pDef.Name.Trim();
     this.Description           = string.IsNullOrWhiteSpace(pDef.Description) ? string.Empty : pDef.Description.Trim();
     this.IsPrimaryKey          = pDef.IsPrimaryKey;
     this.RequiredInActionInput = pDef.RequiredInActionInput;
     this.UsedInActionInput     = pDef.UsedInActionInput;
     this.UsedInActionInput     = pDef.UsedInActionInput;
     this.UsedInActionOutput    = pDef.UsedInActionOutput;
     this.UsedInLookupCondition = pDef.UsedInLookupCondition;
     this.UsedInQueryConstraint = pDef.UsedInQueryConstraint;
     this.UsedInQuerySelect     = pDef.UsedInQuerySelect;
     this.UsedInQuerySequence   = pDef.UsedInQuerySequence;
 }
Beispiel #7
0
        private static Action <object, IEnumerable <DataEntity> > MakeSetter(Action <object, object> setValue, ObjDefs objDefHeader, ISettablePropertyDef context, Type targetType, string deType)
        {
            return((target, val) =>
            {
                if (target == null)
                {
                    ThrowHelper.ThrowSetterTargetNull(context);
                }
                if (val == null)
                {
                    setValue(target, null);
                    return;
                }
                var tVal = val.Select(x => ObjDefConverter.From <T>(x, objDefHeader, deType));

                if (targetType.IsArray)
                {
                    setValue(target, tVal.ToArray());
                    return;
                }

                if (targetType.GetGenericTypeDefinition() == typeof(List <>))
                {
                    setValue(target, tVal.ToList());
                    return;
                }

                setValue(target, tVal);
                return;
            });
        }
        private static IPropDef MakeTypedProperty(ISettablePropertyDef propAttr, string name, Type ty, Func <object, object> getter, Action <object, object> setter)
        {
            if (ty.IsEnum)
            {
                return(new EnumProperty(propAttr, name, ty, getter, setter));
            }

            // Boolean
            if (ty == typeof(Boolean))
            {
                return(new BooleanProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Boolean?))
            {
                return(new NBooleanProperty(propAttr, name, ty, getter, setter));
            }

            if (ty == typeof(List <Boolean>))
            {
                return(new BooleanListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Boolean>))
            {
                return(new BooleanListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Boolean[]))
            {
                return(new BooleanListProperty(propAttr, name, ty, getter, setter));
            }


            if (ty == typeof(List <Boolean?>))
            {
                return(new NBooleanListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Boolean?>))
            {
                return(new NBooleanListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Boolean?[]))
            {
                return(new NBooleanListProperty(propAttr, name, ty, getter, setter));
            }


            // DateTime
            if (ty == typeof(DateTime))
            {
                return(new DateTimeProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(DateTime?))
            {
                return(new NDateTimeProperty(propAttr, name, ty, getter, setter));
            }

            if (ty == typeof(List <DateTime>))
            {
                return(new DateTimeListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <DateTime>))
            {
                return(new DateTimeListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(DateTime[]))
            {
                return(new DateTimeListProperty(propAttr, name, ty, getter, setter));
            }


            if (ty == typeof(List <DateTime?>))
            {
                return(new NDateTimeListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <DateTime?>))
            {
                return(new NDateTimeListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(DateTime?[]))
            {
                return(new NDateTimeListProperty(propAttr, name, ty, getter, setter));
            }


            // Int32
            if (ty == typeof(Int32))
            {
                return(new Int32Property(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Int32?))
            {
                return(new NInt32Property(propAttr, name, ty, getter, setter));
            }

            if (ty == typeof(List <Int32>))
            {
                return(new Int32ListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Int32>))
            {
                return(new Int32ListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Int32[]))
            {
                return(new Int32ListProperty(propAttr, name, ty, getter, setter));
            }


            if (ty == typeof(List <Int32?>))
            {
                return(new NInt32ListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Int32?>))
            {
                return(new NInt32ListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Int32?[]))
            {
                return(new NInt32ListProperty(propAttr, name, ty, getter, setter));
            }


            // Int16
            if (ty == typeof(Int16))
            {
                return(new Int16Property(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Int16?))
            {
                return(new NInt16Property(propAttr, name, ty, getter, setter));
            }

            if (ty == typeof(List <Int16>))
            {
                return(new Int16ListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Int16>))
            {
                return(new Int16ListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Int16[]))
            {
                return(new Int16ListProperty(propAttr, name, ty, getter, setter));
            }


            if (ty == typeof(List <Int16?>))
            {
                return(new NInt16ListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Int16?>))
            {
                return(new NInt16ListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Int16?[]))
            {
                return(new NInt16ListProperty(propAttr, name, ty, getter, setter));
            }


            // Int64
            if (ty == typeof(Int64))
            {
                return(new Int64Property(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Int64?))
            {
                return(new NInt64Property(propAttr, name, ty, getter, setter));
            }

            if (ty == typeof(List <Int64>))
            {
                return(new Int64ListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Int64>))
            {
                return(new Int64ListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Int64[]))
            {
                return(new Int64ListProperty(propAttr, name, ty, getter, setter));
            }


            if (ty == typeof(List <Int64?>))
            {
                return(new NInt64ListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Int64?>))
            {
                return(new NInt64ListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Int64?[]))
            {
                return(new NInt64ListProperty(propAttr, name, ty, getter, setter));
            }


            // Decimal
            if (ty == typeof(Decimal))
            {
                return(new DecimalProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Decimal?))
            {
                return(new NDecimalProperty(propAttr, name, ty, getter, setter));
            }

            if (ty == typeof(List <Decimal>))
            {
                return(new DecimalListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Decimal>))
            {
                return(new DecimalListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Decimal[]))
            {
                return(new DecimalListProperty(propAttr, name, ty, getter, setter));
            }


            if (ty == typeof(List <Decimal?>))
            {
                return(new NDecimalListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Decimal?>))
            {
                return(new NDecimalListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Decimal?[]))
            {
                return(new NDecimalListProperty(propAttr, name, ty, getter, setter));
            }


            // Double
            if (ty == typeof(Double))
            {
                return(new DoubleProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Double?))
            {
                return(new NDoubleProperty(propAttr, name, ty, getter, setter));
            }

            if (ty == typeof(List <Double>))
            {
                return(new DoubleListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Double>))
            {
                return(new DoubleListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Double[]))
            {
                return(new DoubleListProperty(propAttr, name, ty, getter, setter));
            }


            if (ty == typeof(List <Double?>))
            {
                return(new NDoubleListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Double?>))
            {
                return(new NDoubleListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Double?[]))
            {
                return(new NDoubleListProperty(propAttr, name, ty, getter, setter));
            }


            // Single
            if (ty == typeof(Single))
            {
                return(new SingleProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Single?))
            {
                return(new NSingleProperty(propAttr, name, ty, getter, setter));
            }

            if (ty == typeof(List <Single>))
            {
                return(new SingleListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Single>))
            {
                return(new SingleListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Single[]))
            {
                return(new SingleListProperty(propAttr, name, ty, getter, setter));
            }


            if (ty == typeof(List <Single?>))
            {
                return(new NSingleListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Single?>))
            {
                return(new NSingleListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Single?[]))
            {
                return(new NSingleListProperty(propAttr, name, ty, getter, setter));
            }


            // Guid
            if (ty == typeof(Guid))
            {
                return(new GuidProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Guid?))
            {
                return(new NGuidProperty(propAttr, name, ty, getter, setter));
            }

            if (ty == typeof(List <Guid>))
            {
                return(new GuidListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Guid>))
            {
                return(new GuidListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Guid[]))
            {
                return(new GuidListProperty(propAttr, name, ty, getter, setter));
            }


            if (ty == typeof(List <Guid?>))
            {
                return(new NGuidListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Guid?>))
            {
                return(new NGuidListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Guid?[]))
            {
                return(new NGuidListProperty(propAttr, name, ty, getter, setter));
            }


            // String
            if (ty == typeof(String))
            {
                return(new StringProperty(propAttr, name, ty, getter, setter));
            }

            if (ty == typeof(List <String>))
            {
                return(new StringListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <String>))
            {
                return(new StringListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(String[]))
            {
                return(new StringListProperty(propAttr, name, ty, getter, setter));
            }

            // Char
            if (ty == typeof(Char))
            {
                return(new CharProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Char?))
            {
                return(new NCharProperty(propAttr, name, ty, getter, setter));
            }

            if (ty == typeof(List <Char>))
            {
                return(new CharListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Char>))
            {
                return(new CharListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Char[]))
            {
                return(new CharListProperty(propAttr, name, ty, getter, setter));
            }


            if (ty == typeof(List <Char?>))
            {
                return(new NCharListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Char?>))
            {
                return(new NCharListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Char?[]))
            {
                return(new NCharListProperty(propAttr, name, ty, getter, setter));
            }


            // Byte
            if (ty == typeof(Byte[]))
            {
                return(new ByteArrayProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Byte))
            {
                return(new ByteProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Byte?))
            {
                return(new NByteProperty(propAttr, name, ty, getter, setter));
            }

            if (ty == typeof(List <Byte>))
            {
                return(new ByteListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Byte>))
            {
                return(new ByteListProperty(propAttr, name, ty, getter, setter));
            }
            //if (ty == typeof(Byte[])) return new ByteListProperty(propAttr, name, ty, getter, setter);

            if (ty == typeof(List <Byte?>))
            {
                return(new NByteListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(IEnumerable <Byte?>))
            {
                return(new NByteListProperty(propAttr, name, ty, getter, setter));
            }
            if (ty == typeof(Byte?[]))
            {
                return(new NByteListProperty(propAttr, name, ty, getter, setter));
            }


            // Check if it is an Object Defintion or some type of list of object definition

            if (ty.GetCustomAttributes <ObjectDefinitionAttribute>().Any())
            {
                return(new DataEntityPropertyDef(propAttr, name, ty, getter, setter));
            }

            if (ty.IsArray)
            {
                var underTy = ty.GetElementType();
                return(new DataEntityListPropertyDef(propAttr, name, underTy, getter, setter, ty));
            }

            if (ty.GetGenericTypeDefinition() == typeof(List <>))
            {
                var underlying = ty.GetGenericArguments()[0];
                return(new DataEntityListPropertyDef(propAttr, name, underlying, getter, setter, ty));
            }

            if (ty.GetGenericTypeDefinition() == typeof(IEnumerable <>))
            {
                var underlying = ty.GetGenericArguments()[0];
                return(new DataEntityListPropertyDef(propAttr, name, underlying, getter, setter, ty));
            }



            throw new NotImplementedException($"We do not yet have support for lists of hierarchical properties. Property: {name},  Type: {ty.FullName}");
        }
 public NDateTimeProperty(ISettablePropertyDef pDef, string propName, Type propType, Func <object, object> getter, Action <object, object> setter) : base(pDef, propName, propType, getter, setter)
 {
 }
Beispiel #10
0
 private static Func <object, QueryEntity, DataEntity> MakeQGetter(Func <object, object> getValue, ObjDefs objDefHeader, ISettablePropertyDef context)
 {
     return((target, qe) =>
     {
         if (target == null)
         {
             ThrowHelper.ThrowGetterTargetNull(context);
         }
         var val = getValue(target);
         if (val == null)
         {
             return null;
         }
         var de = ObjDefConverter.To((T)val, objDefHeader, qe);
         return de;
     });
 }
Beispiel #11
0
 protected SimpleNullableListProperty(ISettablePropertyDef pDef, string propName, Type propType, Func <object, object> getter, Action <object, object> setter)
     : base(pDef, propName, propType, getter, setter)
 {
 }
 protected SimpleListProperty(ISettablePropertyDef pDef, string propName, Type propType, Func <object, object> getter, Action <object, object> setter)
     : base(pDef, propName)
 {
     this.GetF = MakeGetter(getter, this, this.Convert);
     this.SetF = MakeSetter(setter, this, this.Convert, propType.IsArray);
 }
Beispiel #13
0
 private static Action <object, DataEntity> MakeSetter(Action <object, object> setValue, ObjDefs objDefHeaders, ISettablePropertyDef context, string objDefName)
 {
     return((target, val) =>
     {
         if (target == null)
         {
             ThrowHelper.ThrowSetterTargetNull(context);
         }
         var tVal = ObjDefConverter.From <T>(val, objDefHeaders, objDefName);
         setValue(target, tVal);
     });
 }
Beispiel #14
0
 private static Action <object, object> MakeSetter(Action <object, object> setValue, ISettablePropertyDef context, Func <object, T> convert)
 {
     return((target, val) =>
     {
         var tVal = convert(val);
         if (target == null)
         {
             ThrowHelper.ThrowSetterTargetNull(context);
         }
         setValue(target, tVal);
     });
 }
Beispiel #15
0
 public static void ThrowGetterTargetNull(ISettablePropertyDef context)
 {
     throw new InvalidOperationException(
               $"Attempting to get the '{context.Name}' property, but the target is null.'");
 }
Beispiel #16
0
 public ListOfDataEntity(ISettablePropertyDef pDef, IObjDefHeader header, string propName, Type underlyingType)
     : base(pDef, propName)
 {
     this.PropertyType = string.IsNullOrWhiteSpace(header.Name) ? underlyingType.Name : header.Name.Trim();
 }
Beispiel #17
0
 public static void ThrowTypesDoNotMatch(Type actual, Type expectedType, ISettablePropertyDef context)
 {
     throw new InvalidOperationException(
               $"The actual type of the field '{context.Name}' was '{actual.FullName}',  but it was expected to be '{expectedType.FullName}'.'");
 }
Beispiel #18
0
 public SimplePropDefBase(ISettablePropertyDef pDef, string fieldName) : base(pDef, fieldName)
 {
 }
Beispiel #19
0
 private static Func <object, QueryEntity, List <DataEntity> > MakeQGetter(Func <object, object> getValue, ObjDefs objDefHeader, ISettablePropertyDef context)
 {
     return((target, qe) =>
     {
         if (target == null)
         {
             ThrowHelper.ThrowGetterTargetNull(context);
         }
         var val = getValue(target);
         if (val == null)
         {
             return null;
         }
         var ie = val as IEnumerable <T>;
         var des = ie.Select(x => ObjDefConverter.To(x, objDefHeader, qe));
         return des.ToList();
     });
 }
 private static Action <object, object> MakeSetter(Action <object, object> setValue, ISettablePropertyDef context, Func <object, List <T> > convert, bool isArray)
 {
     return((target, val) =>
     {
         if (target == null)
         {
             ThrowHelper.ThrowGetterTargetNull(context);
         }
         var tVal = convert(val);
         if (target == null)
         {
             ThrowHelper.ThrowSetterTargetNull(context);
         }
         setValue(target, isArray ? (object)tVal?.ToArray() : tVal);
     });
 }