public static string SerializeType(object obj, ICriteria filterCriteria)
        {
            if (obj.GetType().IsArray)
            {
                return(GetElements(obj, filterCriteria));
            }
            else
            {
                string objName = obj.GetType().Name;

                string    critName         = filterCriteria.GetType().Name;
                string    searchDictionary = objName + critName + suffix;
                ICriteria contained;
                if (serializedObject.TryGetValue(searchDictionary, out contained))
                {
                    return(contained.Execute(obj));
                }
                Jsonfier.criteria = filterCriteria;
                ICriteria newObjSerialized = EmmiterUtils.CodeEmmiter(obj, objName, critName, filterCriteria);//We pass the strings to avoid using reflection again

                serializedObject.Add(searchDictionary, newObjSerialized);

                return(newObjSerialized.Execute(obj));
            }
        }
Пример #2
0
        public static Task <TResult> RunQuery <TResult>(this IServiceProvider services, ICriteria <TResult> query)
        {
            Type queryType = typeof(IQuery <,>)
                             .MakeGenericType(query.GetType(), typeof(TResult));

            object   impl = services.GetRequiredService(queryType);
            IContext ctx  = services.GetRequiredService <IContext>();

            return((Task <TResult>)queryType
                   .GetMethod("Run")
                   .Invoke(impl, new object[] { query, ctx }));
        }
Пример #3
0
        public object Execute([FromBody] ICriteria query)
        {
            Check.NotNull(query, nameof(query));

            Type criteriaType = query.GetType();
            Type resultType   = ReflectionUtils
                                .GetGenericInterfaceImplementations(criteriaType, typeof(ICriteria <>))
                                .Single()
                                .GenericTypeArguments
                                .Single();

            return(((Task <object>)__runQueryMethod
                    .MakeGenericMethod(criteriaType, resultType)
                    .Invoke(this, new[] { query })).Result);
        }
        public static ICriteria CodeEmmiter(object obj, string objName, string critName, ICriteria criteria)
        {
            /*TESTE*/
            if (emmiterCriteria.Count == 0)
            {
                emmiterCriteria.Add(typeof(FilterByProperty), new EmmitProcessProperty());
                emmiterCriteria.Add(typeof(FilterByField), new EmitterProcessFields());
                emmiterCriteria.Add(typeof(FilterParametlessNonVoidMethod), new EmitterProcessParametlessNonVoidMethod());
            }
            /*END TESTE*/

            string          asmName   = objName + critName;
            string          className = objName + critName + suffix;
            AssemblyBuilder asmBuilder;

            TypeBuilder typeBuilder = CreateType(asmName, className, out asmBuilder);

            typeBuilder.AddInterfaceImplementation(typeof(ICriteria));
            MethodBuilder mb = typeBuilder.DefineMethod(
                "Execute",
                MethodAttributes.Virtual | MethodAttributes.Public | MethodAttributes.ReuseSlot,
                typeof(string),
                new Type[1] {
                typeof(object)
            }
                );
            ConstructorBuilder ctorBuilder = typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
            //FieldBuilder fieldBuilder = typeBuilder.DefineField("CriteriaTarget", typeof(Criteria), FieldAttributes.Public);
            //AddCtor(typeBuilder, fieldBuilder);

            IEmmiterCriteria ilEmmiter;

            if (!emmiterCriteria.TryGetValue(criteria.GetType(), out ilEmmiter))
            {
                Console.WriteLine("Criteria not accepted");
                return(null);
            }
            else
            {
                ilEmmiter.EmmiterCode(typeBuilder, mb, obj);
            }

            Type myClass = typeBuilder.CreateType();

            asmBuilder.Save(asmName + ".dll");

            return((ICriteria)Activator.CreateInstance(myClass));
        }
Пример #5
0
        private void MapValueToDBValue(SqlParameterCollection parameters, ICriteria criteria)
        {
            var       properties = criteria.GetType().GetProperties();
            SqlDbType dbType;

            foreach (var property in properties)
            {
                var realType = property.GetType();

                if (realType == typeof(Int64))
                {
                    dbType = SqlDbType.BigInt;
                }
                else if (realType == typeof(int))
                {
                    dbType = SqlDbType.Int;
                }
                else if (realType == typeof(decimal))
                {
                    dbType = SqlDbType.Decimal;
                }
                else if (realType == typeof(bool))
                {
                    dbType = SqlDbType.Bit;
                }
                else if (realType == typeof(char))
                {
                    dbType = SqlDbType.VarChar;
                }
                else if (realType == typeof(DateTime))
                {
                    dbType = SqlDbType.DateTime;
                }
                else
                {
                    dbType = SqlDbType.VarChar;
                }

                parameters.Add(property.Name, dbType).Value = property.GetValue(criteria);
            }
        }
        JObject Build(ICriteria criteria)
        {
            if (criteria == null)
                return null;

            if (criteria is RangeCriteria)
                return Build((RangeCriteria)criteria);

            if (criteria is RegexpCriteria)
                return Build((RegexpCriteria)criteria);

            if (criteria is PrefixCriteria)
                return Build((PrefixCriteria)criteria);

            if (criteria is TermCriteria)
                return Build((TermCriteria)criteria);

            if (criteria is TermsCriteria)
                return Build((TermsCriteria)criteria);

            if (criteria is NotCriteria)
                return Build((NotCriteria)criteria);

            if (criteria is QueryStringCriteria)
                return Build((QueryStringCriteria)criteria);

            if (criteria is MatchAllCriteria)
                return Build((MatchAllCriteria)criteria);

            if (criteria is BoolCriteria)
                return Build((BoolCriteria)criteria);

            // Base class formatters using name property

            if (criteria is SingleFieldCriteria)
                return Build((SingleFieldCriteria)criteria);

            if (criteria is CompoundCriteria)
                return Build((CompoundCriteria)criteria);

            throw new InvalidOperationException(string.Format("Unknown criteria type '{0}'", criteria.GetType()));
        }
Пример #7
0
        protected JObject Build(ICriteria criteria)
        {
            if (criteria == null)
            {
                throw new ArgumentNullException("criteria", "Criteria must be referenced.");
            }

            throw new NotImplementedException(string.Format("The given criteria is not managed by this builder, type: {0}", criteria.GetType().FullName));
        }
Пример #8
0
        JObject Build(ICriteria criteria)
        {
            if (criteria == null)
            {
                return(null);
            }

            if (criteria is RangeCriteria)
            {
                return(Build((RangeCriteria)criteria));
            }

            if (criteria is RegexpCriteria)
            {
                return(Build((RegexpCriteria)criteria));
            }

            if (criteria is PrefixCriteria)
            {
                return(Build((PrefixCriteria)criteria));
            }

            if (criteria is TermCriteria)
            {
                return(Build((TermCriteria)criteria));
            }

            if (criteria is TermsCriteria)
            {
                return(Build((TermsCriteria)criteria));
            }

            if (criteria is NotCriteria)
            {
                return(Build((NotCriteria)criteria));
            }

            if (criteria is QueryStringCriteria)
            {
                return(Build((QueryStringCriteria)criteria));
            }

            if (criteria is MatchAllCriteria)
            {
                return(Build((MatchAllCriteria)criteria));
            }

            if (criteria is BoolCriteria)
            {
                return(Build((BoolCriteria)criteria));
            }

            // Base class formatters using name property

            if (criteria is SingleFieldCriteria)
            {
                return(Build((SingleFieldCriteria)criteria));
            }

            if (criteria is CompoundCriteria)
            {
                return(Build((CompoundCriteria)criteria));
            }

            throw new InvalidOperationException($"Unknown criteria type '{criteria.GetType()}'");
        }
Пример #9
0
        private JObject BuildCriteria(ICriteria criteria)
        {
            if (criteria is RangeCriteria)
            {
                return(Build((RangeCriteria)criteria));
            }

            if (criteria is RegexpCriteria)
            {
                return(Build((RegexpCriteria)criteria));
            }

            if (criteria is PrefixCriteria)
            {
                return(Build((PrefixCriteria)criteria));
            }

            if (criteria is TermCriteria)
            {
                return(Build((TermCriteria)criteria));
            }

            if (criteria is TermsCriteria)
            {
                return(Build((TermsCriteria)criteria));
            }

            if (criteria is NotCriteria)
            {
                return(Build((NotCriteria)criteria));
            }

            if (criteria is QueryStringCriteria)
            {
                return(Build((QueryStringCriteria)criteria));
            }

            // Base class formatters using name property

            if (criteria is SingleFieldCriteria)
            {
                return(Build((SingleFieldCriteria)criteria));
            }

            if (criteria is CompoundCriteria)
            {
                return(Build((CompoundCriteria)criteria));
            }

            throw new InvalidOperationException(String.Format("Unknown criteria type {0}", criteria.GetType()));
        }
        private void Build(ICriteria criteria)
        {
            if (criteria == null)
            {
                return;
            }

            if (criteria is ExistsCriteria)
            {
                SimpleBuild(criteria);

                return;
            }

            if (criteria is ComparisonCriteria)
            {
                SimpleBuild(criteria);

                return;
            }

            if (criteria is DistanceCriteria)
            {
                SimpleBuild(criteria);

                return;
            }

            /*
             * if (criteria is RegexpCriteria)
             * {
             *  Build((RegexpCriteria)criteria);
             *
             *  return;
             * }
             *
             * if (criteria is PrefixCriteria)
             * {
             *  Build((PrefixCriteria)criteria);
             *
             *  return;
             * }*/


            if (criteria is TermCriteria)
            {
                SimpleBuild(criteria);

                return;
            }

            if (criteria is TermsCriteria)
            {
                SimpleBuild(criteria);

                return;
            }

            if (criteria is QueryStringCriteria)
            {
                Build((QueryStringCriteria)criteria);

                return;
            }

            // Base class formatters using name property

            /*
             * if (criteria is SingleFieldCriteria)
             * {
             *  Build((SingleFieldCriteria)criteria);
             *
             *  return;
             * }*/

            if (criteria is CompoundCriteria)
            {
                Build((CompoundCriteria)criteria);

                return;
            }

            throw new InvalidOperationException($"Unknown criteria type '{criteria.GetType()}'");
        }