예제 #1
0
        /// <summary>
        /// Generates setter method using xml config or type metadata (attributes).
        /// </summary>
        /// <param name="targetClassType"></param>
        /// <param name="schemeId"></param>
        /// <param name="dtSource"></param>
        /// <returns></returns>
        public ExtractInfo GenerateSetterMethod(
            ExtractInfo extractInfo,
            DataTable dtSource,
            Type generatorSourceType,
            bool createNamespace)
        {
            lock (_SyncRoot)
            {
                CreateModule();
                _SetterMethodGenerator.GenerateSetterMethod(extractInfo, dtSource, generatorSourceType, createNamespace);

                var all = extractInfo.GetWholeSubTree();
                foreach (var item in all)
                {
                    GenerateKeys(item, dtSource, generatorSourceType, true);
                    GenerateKeys(item, dtSource, generatorSourceType, false);

                    if (item.ChildTypes.Count > 0)
                    {
                        _LinkMethodGenerator.GenerateLinkMethod(item, createNamespace);
                    }
                }

                return(extractInfo);
            }
        }
예제 #2
0
 /// <summary>
 /// Generates setter method using extractInfo.
 /// </summary>
 /// <param name="targetClassType"></param>
 /// <param name="schemeId"></param>
 /// <param name="dtSource"></param>
 /// <returns></returns>
 public ExtractInfo GenerateSetterMethod(
     ExtractInfo extractInfo,
     DataTable dtSource,
     Type generatorSourceType)
 {
     return(GenerateSetterMethod(extractInfo, dtSource, generatorSourceType, true));
 }
예제 #3
0
        public ExtractInfo CreateExtractInfoWithMethod(
            Type targetClassType,
            int schemeId,
            DataTable dtSource,
            Type generatorSourceType,
            bool createNamespace)
        {
            ExtractInfo result = CreateExtractInfo(targetClassType, schemeId);

            GenerateSetterMethod(result, dtSource, generatorSourceType, createNamespace);
            return(result);
        }
예제 #4
0
        public override bool Equals(object obj)
        {
            ExtractInfo ei = obj as ExtractInfo;

            if (ei == null)
            {
                return(false);
            }

            return
                (ei.TargetType == TargetType &&
                 ei.SchemeId == SchemeId);
        }
예제 #5
0
        protected void GenerateKeys(ExtractInfo extractInfo, DataTable dtSource, Type generatorSourceType, bool primary)
        {
            List <RelationExtractInfo> rels = primary ?
                                              extractInfo.ChildTypes:
                                              extractInfo.RelationsFromParent;

            foreach (var rei in rels)
            {
                if (rei.KeyInfo != null)
                {
                    GenerateKey(
                        rei.KeyInfo,
                        rei.Member.DeclaringType,
                        //extractInfo.TargetType,
                        extractInfo.SchemeId,
                        dtSource,
                        generatorSourceType);
                }
            }
        }
예제 #6
0
        protected void DumpExtractInfo(ExtractInfo extractInfo, int extractLevel)
        {
            Debug.WriteLine(string.Format(
                                new string('\t', extractLevel) + "Creating {0}",
                                extractInfo));

            foreach (var item in extractInfo.MemberColumns)
            {
                Debug.WriteLine(string.Format(
                                    new string('\t', extractLevel) + "\tMap {0} {1,-25} to column {2}",
                                    item.Member.MemberType,
                                    item.Member.Name,
                                    item.MapName
                                    ));
            }

            foreach (var item in extractInfo.ChildTypes)
            {
                Debug.WriteLine(string.Format(
                                    new string('\t', extractLevel) + "\tMap property {0,-25} to relation {1} (collection of {2})",
                                    item.Member.Name,
                                    item.MapName,
                                    item.RelatedExtractInfo.TargetType
                                    ));
            }

            foreach (var item in extractInfo.SubTypes)
            {
                Debug.WriteLine(string.Format(
                                    new string('\t', extractLevel) + "\tMap property {0,-25} to complex tpye {1}",
                                    item.Member.Name,
                                    item.RelatedExtractInfo.TargetType
                                    ));
            }

            Debug.WriteLine(string.Format(
                                new string('\t', extractLevel) + "Done with creating {0}",
                                extractInfo));
        }
예제 #7
0
        protected void GenerateKey(KeyInfo keyInfo, Type targetType, int schemeId, DataTable dtSource, Type generatorSourceType)
        {
            if (keyInfo.ParentKeyExtractInfo != null)
            {
                return;
            }

            string keyClass      = targetType + "." + keyInfo.Name;
            int    childSchemeId = schemeId == 0 ? int.MinValue : -schemeId;

            Type keyType = _KeyGenerator.GenerateKeyType(
                keyClass,
                dtSource,
                keyInfo.ParentColumns,
                keyInfo.ChildColumns,
                schemeId,
                childSchemeId
                );

            ExtractInfo primaryExtractInfo = CreateExtractInfoWithMethod(
                keyType,
                schemeId,
                dtSource,
                generatorSourceType,
                false
                );

            ExtractInfo foreignExtractInfo = primaryExtractInfo.Copy();

            foreignExtractInfo.SchemeId      = childSchemeId;
            foreignExtractInfo.MemberColumns = CreateExtractInfo(keyType, childSchemeId).MemberColumns;

            keyInfo.GeneratorSourceType  = generatorSourceType;
            keyInfo.ParentKeyExtractInfo = primaryExtractInfo;
            keyInfo.ChildKeyExtractInfo  = foreignExtractInfo;

            return;
        }
예제 #8
0
 public RelationExtractInfo(string mapName, MemberInfo member, ExtractInfo relatedExtractInfo, KeyInfo primaryKey)
     : base(mapName, member)
 {
     _RelatedExtractInfo = relatedExtractInfo;
     _PrimaryKeyInfo     = primaryKey;
 }
예제 #9
0
        protected ExtractInfo CreateExtractInfo(Type targetClassType, int schemeId, int extractLevel)
        {
            //Check cache
            ExtractInfo result;

            if (_ExtractInfoCache.TryGetExtractInfo(targetClassType, schemeId, out result))
            {
                return(result);
            }

            result = new ExtractInfo(targetClassType, schemeId);
            _ExtractInfoCache.Add(targetClassType, schemeId, result);
            _MappingProvider.GetExtractInfo(result);

            //resolving nested types
            foreach (var item in result.SubTypes)
            {
                if (item.RelatedExtractInfo.TargetType == null)
                {
                    item.RelatedExtractInfo.TargetType = ReflectionHelper.GetReturnType(item.Member);
                }
            }

            foreach (var item in result.ChildTypes)
            {
                if (item.RelatedExtractInfo.TargetType == null)
                {
                    item.RelatedExtractInfo.TargetType = ReflectionHelper.GetListItemType(
                        ReflectionHelper.GetReturnType(item.Member));

                    if (item.KeyInfo != null)
                    {
                        item.KeyInfo.ChildType = item.RelatedExtractInfo.TargetType;
                    }
                }

                if (item.RelatedExtractInfo.TargetType == null)
                {
                    throw new DataMapperException("Cannot resolve type of items in collection(" + item.Member.Name + "). " +
                                                  "Try to set it via ItemType property of DataRelationMapAttribute.");
                }
            }

            //distinct same keys
            foreach (var item1 in result.ChildTypes)
            {
                foreach (var item2 in result.ChildTypes)
                {
                    var key1 = item1.KeyInfo;
                    if (key1 != null && key1.Equals(item2.KeyInfo))
                    {
                        item2.KeyInfo = key1;
                    }
                }
            }

            DumpExtractInfo(result, extractLevel);

            //fill child types (recursive)
            foreach (var item in result.SubTypes)
            {
                item.RelatedExtractInfo = CreateExtractInfo(
                    item.RelatedExtractInfo.TargetType,
                    item.RelatedExtractInfo.SchemeId,
                    extractLevel + 1);
            }

            foreach (var item in result.ChildTypes)
            {
                item.RelatedExtractInfo = CreateExtractInfo(
                    item.RelatedExtractInfo.TargetType,
                    item.RelatedExtractInfo.SchemeId,
                    extractLevel + 1);
            }

            //fill foreign keys
            if (extractLevel == 0)
            {
                result.ResolveForeign();
            }

            return(result);
        }