示例#1
0
        public void GenerateClassStub(CodeNamespace nspace, ClassInfo ci, bool miniStub)
        {
            if (!string.IsNullOrEmpty(ci.Schema.AssemblyName))
                return;
            if (!miniStub)
                GenerateClassValues(nspace, ci, miniStub);

            CodeDomClassStubGenerator gen = new CodeDomClassStubGenerator(ci, Project);
            CDILContext context = new CDILContext();
            context["ClassName"] = ci.Name;
            context["HasBaseClass"] = ci.InheritsFromClass != null;
            context["MiniStub"] = miniStub;
            context["HasKeyGen"] = gen.KeyGen != "none";
            if (ci.ExtBaseClassName != null && !miniStub)
            {
                context["BaseClassName"] = ci.ExtBaseClassName;
            }
            else if (ci.InheritFrom != null && !miniStub)
            {
                context["BaseClassName"] = ci.InheritFrom;
            }
            else if (Project.BaseClassName != null && !miniStub)
            {
                context["BaseClassName"] = Project.BaseClassName;
            }
            else
            {
                context["BaseClassName"] = "SoodaObject";
            }
            context["ArrayFieldValues"] = ci.GetDataSource().EnableDynamicFields;

            CodeTypeDeclaration ctd = CDILParser.ParseClass(CDILTemplate.Get("Stub.cdil"), context);
            if (ci.Description != null)
            {
                ctd.Comments.Add(new CodeCommentStatement("<summary>", true));
                ctd.Comments.Add(new CodeCommentStatement(ci.Description, true));
                ctd.Comments.Add(new CodeCommentStatement("</summary>", true));
            }
            nspace.Types.Add(ctd);

            if (miniStub)
                return;

            CodeTypeDeclaration ctdLoader = GetLoaderClass(ci);

            if (!Project.LoaderClass)
            {
                foreach (CodeTypeMember m in ctdLoader.Members)
                {
                    ctd.Members.Add(m);
                }
            }

            // class constructor

            if (gen.KeyGen != "none")
            {
                ctd.Members.Add(gen.Field_keyGenerator());
            }

            gen.GenerateFields(ctd, ci);
            gen.GenerateProperties(ctd, ci);


            // literals
            if (ci.Constants != null && ci.GetPrimaryKeyFields().Length == 1)
            {
                foreach (ConstantInfo constInfo in ci.Constants)
                {
                    object value;
                    switch (ci.GetFirstPrimaryKeyField().DataType)
                    {
                        case FieldDataType.Integer:
                            value = int.Parse(constInfo.Key);
                            break;
                        case FieldDataType.String:
                        case FieldDataType.AnsiString:
                            value = constInfo.Key;
                            break;
                        default:
                            throw new NotSupportedException("Primary key type " + ci.GetFirstPrimaryKeyField().DataType + " is not supported");
                    }
                    ctd.Members.Add(gen.Prop_LiteralValue(constInfo.Name, value));
                }
            }

            foreach (FieldInfo fi in ci.LocalFields)
            {
                if (fi.IsPrimaryKey)
                    continue;

                if (ci.Triggers || fi.ForceTrigger)
                {
                    ctd.Members.Add(gen.Method_TriggerFieldUpdate(fi, "BeforeFieldUpdate"));
                    ctd.Members.Add(gen.Method_TriggerFieldUpdate(fi, "AfterFieldUpdate"));
                }
            }
        }
示例#2
0
        public void GenerateClassValues(CodeNamespace nspace, ClassInfo ci, bool miniStub)
        {
            if (!string.IsNullOrEmpty(ci.Schema.AssemblyName) || ci.GetDataSource().EnableDynamicFields)
                return;
            CodeDomClassStubGenerator gen = new CodeDomClassStubGenerator(ci, Project);

            CodeTypeDeclaration ctd = new CodeTypeDeclaration(ci.Name + "_Values");
            if (ci.InheritFrom != null)
                ctd.BaseTypes.Add(ci.InheritFrom + "_Values");
            else
                ctd.BaseTypes.Add(typeof(SoodaObjectReflectionCachingFieldValues));
            ctd.Attributes = MemberAttributes.Assembly;

            foreach (FieldInfo fi in ci.LocalFields)
            {
                CodeTypeReference fieldType;
                if (fi.References != null)
                {
                    fieldType = gen.GetReturnType(PrimitiveRepresentation.SqlType, fi);
                }
                else if (fi.IsNullable)
                {
                    fieldType = gen.GetReturnType(Project.NullableRepresentation, fi);
                }
                else
                {
                    fieldType = gen.GetReturnType(Project.NotNullRepresentation, fi);
                }

                CodeMemberField field = new CodeMemberField(fieldType, fi.Name);
                field.Attributes = MemberAttributes.Public;
                ctd.Members.Add(field);
            }

            CodeConstructor constructor2 = new CodeConstructor();
            constructor2.Attributes = MemberAttributes.Public;
            constructor2.Parameters.Add(new CodeParameterDeclarationExpression(typeof(SoodaObjectReflectionCachingFieldValues), "other"));
            constructor2.BaseConstructorArgs.Add(new CodeArgumentReferenceExpression("other"));
            ctd.Members.Add(constructor2);

            CodeConstructor constructor3 = new CodeConstructor();
            constructor3.Attributes = MemberAttributes.Public;
            constructor3.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string[]), "fieldNames"));
            constructor3.BaseConstructorArgs.Add(new CodeArgumentReferenceExpression("fieldNames"));
            ctd.Members.Add(constructor3);

            CodeMemberMethod cloneMethod = new CodeMemberMethod();
            cloneMethod.Name = "Clone";
            cloneMethod.ReturnType = new CodeTypeReference(typeof(SoodaObjectFieldValues));
            cloneMethod.Attributes = MemberAttributes.Public | MemberAttributes.Override;
            cloneMethod.Statements.Add(
                    new CodeMethodReturnStatement(
                        new CodeObjectCreateExpression(ci.Name + "_Values",
                            new CodeThisReferenceExpression())));
            ctd.Members.Add(cloneMethod);

            nspace.Types.Add(ctd);
        }