Exemplo n.º 1
0
        /// <summary>
        /// Check if the dynamic object have all the members needed to map a new T object, except those setted as IEnumerable,
        /// which should be provided in others dynamic.
        /// </summary>
        /// <param name="membersDict"></param>
        /// <returns></returns>
        public bool CheckIfDynamicHasAllTypeMembersByName(IDictionary <string, object> membersDict)
        {
            IEnumerable <string> dynList = membersDict.Select(kvp => kvp.Key);
            PrePostFixesParser   parser  = new PrePostFixesParser(this);

            return(dynList.SequenceEqual(parser.GetCleanNamesList(this.NamesList)));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Remove duplicated results due to JOINs.
        /// </summary>
        /// <param name="origDapperResult"></param>
        /// <param name="cleanResult"></param>
        /// <returns></returns>
        public IEnumerable <dynamic> GetDistinctDapperResult(IEnumerable <dynamic> origDapperResult, bool cleanResult)
        {
            PrePostFixesParser   parser = new PrePostFixesParser(this);
            IEnumerable <string> names  = this.NamesList;
            List <dynamic>       result = new List <dynamic>();

            foreach (dynamic dyn in origDapperResult)
            {
                IDictionary <string, object> dict =
                    (!cleanResult ? parser.GetTypeMembersWithoutPrePostFixes(dyn, names) : dyn)
                    as IDictionary <string, object>;

                bool distinct = true;
                foreach (dynamic resultDyn in result)
                {
                    IDictionary <string, object> resDict = resultDyn as IDictionary <string, object>;

                    if (dict.Keys.SequenceEqual(resDict.Keys) && dict.Values.SequenceEqual(resDict.Values))
                    {
                        distinct = false;
                        break;
                    }
                }

                if (distinct)
                {
                    result.Add(dyn);
                }
            }
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Check if the dynamic object have all the members needed to map a new T object, except those setted as IEnumerable,
        /// which should be provided in others dynamic.
        /// </summary>
        /// <param name="dyn"></param>
        /// <returns></returns>
        public bool CheckIfDynamicHasAllTypeMembersByName(dynamic dyn)
        {
            IDictionary <string, object> membersDict = dyn as IDictionary <string, object>;
            IEnumerable <string>         dynList     = membersDict.Select(kvp => kvp.Key);
            PrePostFixesParser           parser      = new PrePostFixesParser(this);
            IEnumerable <string>         list        = parser.GetCleanNamesList(this.NamesList);

            return(!dynList.Except(list).Any() && !list.Except(dynList).Any());
        }
Exemplo n.º 4
0
        protected dynamic GetCleanResult(IEnumerable <dynamic> result, iDapperMapper mapper, bool clean)
        {
            PrePostFixesParser parser = new PrePostFixesParser(mapper);

            if (!clean)
            {
                return(result.Select(dyn => parser.GetTypeMembersWithoutPrePostFixes(dyn, mapper.NamesList)));
            }
            else
            {
                return(result.Select(dyn => parser.RemovePrePostFixesFromDictionary(dyn)));
            }
        }
Exemplo n.º 5
0
        protected dynamic GetCleanResult(dynamic dyn, iDapperMapper keysMapper, string valuesName, bool clean)
        {
            var KeysParser  = new PrePostFixesParser(keysMapper);
            var typeMembers = new ExpandoObject() as IDictionary <string, object>;

            if (!clean)
            {
                typeMembers = KeysParser.GetTypeMembersWithoutPrePostFixes(dyn, keysMapper.NamesList);
                typeMembers.Add(valuesName, ((IDictionary <string, object>)dyn)[valuesName]);
            }
            else
            {
                typeMembers = KeysParser.RemovePrePostFixesFromDictionary(dyn);
                typeMembers.Add(valuesName, ((IDictionary <string, object>)dyn)[valuesName]);
            }

            return(typeMembers);
        }
Exemplo n.º 6
0
        protected dynamic GetCleanResult(dynamic dyn, iDapperMapper keysMapper, iDapperMapper valuesMapper, bool clean)
        {
            var KeysParser   = new PrePostFixesParser(keysMapper);
            var ValuesParser = new PrePostFixesParser(valuesMapper);
            var typeMembers  = new ExpandoObject() as IDictionary <string, object>;

            if (!clean)
            {
                typeMembers = KeysParser.GetTypeMembersWithoutPrePostFixes(dyn, keysMapper.NamesList);
                typeMembers = typeMembers
                              .Concat((IDictionary <string, object>)ValuesParser.GetTypeMembersWithoutPrePostFixes(dyn, valuesMapper.NamesList))
                              .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
            }
            else
            {
                typeMembers = KeysParser.RemovePrePostFixesFromDictionary(dyn);
                typeMembers = typeMembers
                              .Concat((IDictionary <string, object>)ValuesParser.RemovePrePostFixesFromDictionary(dyn))
                              .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
            }

            return(typeMembers);
        }
Exemplo n.º 7
0
        public DictionaryMapper(
            IEnumerable <dynamic> dResult,
            string memberName,
            bool nested,
            bool isAnInterface,
            bool cleanResult,
            Type memberType,
            iDapperMapper masterMapper)
        {
            Type masterType = masterMapper.TType;

            if (!cleanResult && !nested)
            {
                IEnumerable <dynamic> clean;
                var parser = new PrePostFixesParser(masterMapper);
                clean = dResult.Select(dyn => parser.GetTypeMembersWithoutPrePostFixes(dyn, masterMapper.NamesList));

                GetDummyDictionary(dResult, memberName, false, isAnInterface, /*allowDuplicates, */ true, memberType, masterType);
            }
            else
            {
                GetDummyDictionary(dResult, memberName, nested, isAnInterface, /*allowDuplicates, */ cleanResult, memberType, masterType);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Generic Map.
        /// </summary>
        /// <param name="dapperResult"></param>
        /// <returns></returns>
        public T Map(IEnumerable <dynamic> dapperResult, bool cleanResult = false)
        {
            var parser = new PrePostFixesParser(this);
            T   mapped = this.NewObject(dapperResult.First());

            if (_OnlyConstructor.Contains(this.TType))
            {
                return(mapped);
            }

            //TODO: divide el siguiente foreach en dos con dos nuevos diccionarios estáticos, uno para pInfos y otro para fInfos,
            //aunque se repita código: hacer métodos para cada parte del código del tipo:
            //private T PreMapCreator(KeyValuePair<PropertyInfo, MemberTypeInfo> kvp, IEnumerable<dynamic> dapperResult, bool cleanResult = false)
            //private T PreMapIEnumerable(KeyValuePair<PropertyInfo, MemberTypeInfo> kvp, IEnumerable<dynamic> dapperResult, bool cleanResult = false)
            //...

            //Loop through all members
            foreach (KeyValuePair <MemberInfo, MemberTypeInfo> kvp in mtInfos)
            {
                if (kvp.Value == MemberTypeInfo.Ignore)
                {
                    continue;
                }
                //Member have a creator
                else if ((kvp.Value & MemberTypeInfo.Creator) == MemberTypeInfo.Creator)
                {
                    //MemberDelegate mDel = (MemberDelegate)_MembersCreators[this.TType][kvp.Key.Name];
                    Func <dynamic, object> mDel = (Func <dynamic, object>)_MembersCreators[this.TType][kvp.Key.Name];

                    if (kvp.Key.MemberType == MemberTypes.Property)
                    {
                        ((PropertyInfo)kvp.Key).SetValue(mapped, mDel(dapperResult));
                    }
                    else
                    {
                        ((FieldInfo)kvp.Key).SetValue(mapped, mDel(dapperResult));
                    }
                }
                //Member is IDictionary or IEnumerable
                else if ((kvp.Value & MemberTypeInfo.IEnumerable) == MemberTypeInfo.IEnumerable)
                {
                    Type t = GetMemberType(kvp.Key);
                    //if ((kvp.Value & MemberTypeInfo.Interface) == MemberTypeInfo.Interface) t = ResolveInterface(kvp.Key, dapperResult);
                    //else t = GetMemberType(kvp.Key);

                    /*
                     * {
                     *  //Type of property or field
                     *  if (kvp.Key.MemberType == MemberTypes.Property) t = ((PropertyInfo)kvp.Key).PropertyType;
                     *  else t = ((FieldInfo)kvp.Key).FieldType;
                     * }*/
                    bool isAnInterface = (kvp.Value & MemberTypeInfo.Interface) == MemberTypeInfo.Interface;
                    bool isNested      = (kvp.Value & MemberTypeInfo.Nested) == MemberTypeInfo.Nested;

                    //If member is a dictionary
                    if (typeof(IDictionary).IsAssignableFrom(t))
                    {
                        //Create a dummy dictionary with the dapper's dynamic result which should be equal to the final one
                        DictionaryMapper dictMapper = new DictionaryMapper(dapperResult, kvp.Key.Name, isNested, isAnInterface, cleanResult, t, this);

                        try
                        {
                            if (kvp.Key.MemberType == MemberTypes.Property)
                            {
                                ((PropertyInfo)kvp.Key).SetValue(mapped, dictMapper.DummyDictionary);
                            }
                            else
                            {
                                ((FieldInfo)kvp.Key).SetValue(mapped, dictMapper.DummyDictionary);
                            }
                        }
                        catch (Exception err)
                        {
                            throw new CustomException_DapperMapper(
                                      $@"DapperMapper.Map: Couldn't map IDictionary member {kvp.Key.Name} with value contained by dynamic object.
Incorrect type of value?: {kvp.Value.ToString()}",
                                      err);
                        }
                    }
                    //Rest of enumerables
                    else
                    {
                        IEnumerable <dynamic> iEnumDapperResult;
                        //Select current member's values from dynamic
                        if (isNested && !cleanResult)
                        {
                            //Type mType = t; // GetMemberType(kvp.Key);//IEnumerable<T>
                            Type genericType = t.GenericTypeArguments[0];//mType.GenericTypeArguments[0];//T
                            if ((kvp.Value & MemberTypeInfo.Interface) == MemberTypeInfo.Interface)
                            {
                                bool genericIsInterfaceNotIEnumerable =
                                    genericType.IsInterface &&
                                    !typeof(IDictionary).IsAssignableFrom(genericType) &&
                                    !(typeof(IEnumerable).IsAssignableFrom(genericType) && !typeof(string).IsAssignableFrom(genericType));

                                if (genericIsInterfaceNotIEnumerable)
                                {
                                    genericType = ResolveInterface(genericType, dapperResult);
                                }
                            }

                            IDapperMapper nestedMapper = MappersStore.GetMapper(genericType);
                            var           nestedParser = new PrePostFixesParser(nestedMapper);

                            iEnumDapperResult = dapperResult
                                                .Select(dyn => nestedParser.GetTypeMembersWithoutPrePostFixes(dyn, nestedMapper.NamesList));
                        }
                        else if (!cleanResult)
                        {
                            iEnumDapperResult = dapperResult.Select(dyn => parser.RemovePrePostFixesFromDictionary(dyn));
                        }
                        else
                        {
                            iEnumDapperResult = dapperResult;
                        }

                        //Create dummy IEnumerable
                        EnumerableMapper enumMapper = new EnumerableMapper(iEnumDapperResult, kvp.Key.Name, isNested, t, this.TType);;
                        var dummy = Activator.CreateInstance(t, enumMapper.DummyEnumerable);

                        try
                        {
                            if (kvp.Key.MemberType == MemberTypes.Property)
                            {
                                ((PropertyInfo)kvp.Key).SetValue(mapped, dummy);
                            }
                            else
                            {
                                ((FieldInfo)kvp.Key).SetValue(mapped, dummy);
                            }
                        }
                        catch (Exception err)
                        {
                            throw new CustomException_DapperMapper(
                                      $@"DapperMapper.Map: Couldn't map IEnumerable member {kvp.Key.Name} with value contained by dynamic object.
Incorrect type of value?: {kvp.Value.ToString()}",
                                      err);
                        }
                    }
                }//End IDictionary/IEnumerable
                //If Built-in
                else if ((kvp.Value & MemberTypeInfo.BuiltIn) == MemberTypeInfo.BuiltIn)
                {
                    string name = parser.RemoveFieldsUnderscore(kvp.Key.Name);
                    IDictionary <string, object> dapperDict;
                    if (!cleanResult)
                    {
                        dapperDict = parser.GetTypeMembersWithoutPrePostFixes(dapperResult.First(), NamesList) as IDictionary <string, object>;
                    }
                    else
                    {
                        dapperDict = dapperResult.First() as IDictionary <string, object>;
                    }

                    if (!dapperDict.ContainsKey(name))
                    {
                        throw new CustomException_DapperMapper(
                                  $@"DapperMapper.Map: There's no member in dynamic dapper result with name {kvp.Key.Name}. Cannot Map object.");
                    }

                    try
                    {
                        if (kvp.Key.MemberType == MemberTypes.Property)
                        {
                            ((PropertyInfo)kvp.Key).SetValue(mapped, dapperDict[name]);
                        }
                        else
                        {
                            ((FieldInfo)kvp.Key).SetValue(mapped, dapperDict[name]);
                        }
                    }
                    catch (Exception err)
                    {
                        throw new CustomException_DapperMapper(
                                  $@"DapperMapper.Map: Couldn't map BuiltIn-type member {kvp.Key.Name} with value contained by dynamic object.
Incorrect type of value?: {kvp.Value.ToString()}",
                                  err);
                    }
                }
                //if nested
                else if ((kvp.Value & MemberTypeInfo.Nested) == MemberTypeInfo.Nested)
                {
                    Type mType = GetMemberType(kvp.Key);

                    if ((kvp.Value & MemberTypeInfo.Interface) == MemberTypeInfo.Interface)
                    {
                        mType = ResolveInterface(mType, dapperResult);
                    }

                    //access generic Map method through nongeneric interface method
                    IDapperMapper nestedMapper = MappersStore.GetMapper(mType);

                    if (nestedMapper == null)
                    {
                        throw new CustomException_DapperMapper(
                                  $@"DapperMapper.Map: No Mapper found at store for property {kvp.Key.Name} of type {mType.ToString()}.
If you want to map a nested property you have to create a mapper for that property type.");
                    }

                    if (kvp.Key.MemberType == MemberTypes.Property)
                    {
                        ((PropertyInfo)kvp.Key).SetValue(mapped, nestedMapper.NoGenericMap(dapperResult, cleanResult));
                    }
                    else
                    {
                        ((FieldInfo)kvp.Key).SetValue(mapped, nestedMapper.NoGenericMap(dapperResult, cleanResult));
                    }
                }
            }

            return(mapped);
        }