Ejemplo n.º 1
0
        protected MemberMapper GetComplexMapper(string mapName, string origName)
        {
            if (origName == null)
            {
                throw new ArgumentNullException("origName");
            }

            string name = origName.ToLower();
            int    idx  = origName.IndexOf('.');

            if (idx > 0)
            {
                name = name.Substring(0, idx);

                foreach (MemberAccessor ma in TypeAccessor)
                {
                    if (ma.Name.Length == name.Length && ma.Name.ToLower() == name)
                    {
                        ObjectMapper om = MappingSchema.GetObjectMapper(ma.Type);

                        if (om != null)
                        {
                            MemberMapper mm = om.GetComplexMapper(mapName, origName.Substring(idx + 1));

                            if (mm != null)
                            {
                                MapMemberInfo mi = new MapMemberInfo();

                                mi.MemberAccessor        = ma;
                                mi.ComplexMemberAccessor = mm.ComplexMemberAccessor;
                                mi.Type          = mm.Type;
                                mi.MappingSchema = MappingSchema;
                                mi.Name          = mapName;
                                mi.MemberName    = origName;

                                MemberMapper mapper = new MemberMapper.ComplexMapper(mm);

                                mapper.Init(mi);

                                return(mapper);
                            }
                        }

                        break;
                    }
                }
            }
            else
            {
                MemberMapper mm = (MemberMapper)_nameToComplexMapper[name];

                if (mm != null)
                {
                    return(mm);
                }

                if (_nameToNotFoundComplexMappers.ContainsKey(name))
                {
                    return(null);
                }

                foreach (MemberMapper m in _members)
                {
                    if (m.MemberAccessor.Name.Length == name.Length && m.MemberAccessor.Name.ToLower() == name)
                    {
                        _nameToComplexMapper[name] = m;
                        return(m);
                    }
                }

                // Under some conditions, this way lead to memory leaks.
                // In other hand, shaking mappers up every time lead to performance loss.
                // So we cache failed requests.
                // If this optimization is a memory leak for you, just comment out next line.
                //
                _nameToNotFoundComplexMappers.Add(name, null);
            }

            return(null);
        }
Ejemplo n.º 2
0
        protected MemberMapper GetComplexMapper(string mapName, string origName)
        {
            if (origName == null)
            {
                throw new ArgumentNullException("origName");
            }

            var name = origName.ToLower();
            var idx  = origName.IndexOf('.');

            lock (_nameToComplexMapper)
            {
                MemberMapper mm;

                if (_nameToComplexMapper.TryGetValue(name, out mm))
                {
                    return(mm);
                }

                if (idx > 0)
                {
                    name = name.Substring(0, idx);

                    foreach (MemberAccessor ma in TypeAccessor)
                    {
                        if (ma.Name.Length == name.Length && ma.Name.ToLower() == name)
                        {
                            var om = MappingSchema.GetObjectMapper(ma.Type);

                            if (om != null)
                            {
                                mm = om.GetComplexMapper(mapName, origName.Substring(idx + 1));

                                if (mm != null)
                                {
                                    var mi = new MapMemberInfo
                                    {
                                        MemberAccessor        = ma,
                                        ComplexMemberAccessor = mm.ComplexMemberAccessor,
                                        Type          = mm.Type,
                                        MappingSchema = MappingSchema,
                                        Name          = mapName,
                                        MemberName    = origName
                                    };

                                    var mapper = new MemberMapper.ComplexMapper(mm);
                                    var key    = origName.ToLower();

                                    mapper.Init(mi);

                                    if (_nameToComplexMapper.ContainsKey(key))
                                    {
                                        _nameToComplexMapper[key] = mapper;
                                    }
                                    else
                                    {
                                        _nameToComplexMapper.Add(key, mapper);
                                    }

                                    return(mapper);
                                }
                            }

                            break;
                        }
                    }
                }
                else
                {
                    foreach (var m in _members)
                    {
                        if (m.MemberAccessor.Name.Length == name.Length && m.MemberAccessor.Name.ToLower() == name)
                        {
                            if (_nameToComplexMapper.ContainsKey(name))
                            {
                                _nameToComplexMapper[name] = m;
                            }
                            else
                            {
                                _nameToComplexMapper.Add(name, m);
                            }

                            return(m);
                        }
                    }
                }

                // Under some conditions, this way lead to memory leaks.
                // In other hand, shaking mappers up every time lead to performance loss.
                // So we cache failed requests.
                // If this optimization is a memory leak for you, just comment out next line.
                //
                if (_nameToComplexMapper.ContainsKey(name))
                {
                    _nameToComplexMapper[name] = null;
                }
                else
                {
                    _nameToComplexMapper.Add(name, null);
                }

                return(null);
            }
        }