⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 element.cs

📁 smark.data是基于C#开发的轻量级数据访问组件。 提供以下功能封: 1)提供了跨数据库类型操作能力 2)基于程线存储的数据访问上下文对象
💻 CS
📖 第 1 页 / 共 5 页
字号:
                  TypeReferenceFactory.Region(), "region"));
            list.Parameters.Add(
             new CodeParameterDeclarationExpression(
                 TypeReferenceFactory.StringType(), "OrderBy"));

            mEntity.Members.Add(list);
            list.Statements.Add(context);
            list.Statements.Add(setcontext);
            trycatch = new CodeTryCatchFinallyStatement();
            list.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            list.Statements.Add(trycatch);
            trycatch.TryStatements.Add(
                new CodeMethodReturnStatement(
                new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(
                        new CodeTypeReferenceExpression(Name), "List"),
                        new CodeVariableReferenceExpression("_context"),
                          new CodeArgumentReferenceExpression("expression"),
                        new CodeArgumentReferenceExpression("region"),
                        new CodeArgumentReferenceExpression("OrderBy"))));
            _catch = new CodeCatchClause("e_", new CodeTypeReference(typeof(Exception)));
            _catch.Statements.Add(new CodeThrowExceptionStatement(new CodeVariableReferenceExpression("e_")));
            trycatch.CatchClauses.Add(_catch);
            trycatch.FinallyStatements.Add(
                new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(new CodeVariableReferenceExpression("_context"),
                        "Dispose")));
            #endregion

            #region expression
            list = new CodeMemberMethod();
            list.Name = "List";
            list.ReturnType = TypeReferenceFactory.IList_1(Name);
            list.Parameters.Add(
               new CodeParameterDeclarationExpression(
                    TypeReferenceFactory.WhereBody(), "expression"));
            mEntity.Members.Add(list);
            list.Statements.Add(context);
            list.Statements.Add(setcontext);
            trycatch = new CodeTryCatchFinallyStatement();
            list.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            list.Statements.Add(trycatch);
            trycatch.TryStatements.Add(
                new CodeMethodReturnStatement(
                new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(
                        new CodeTypeReferenceExpression(Name), "List"),
                        new CodeVariableReferenceExpression("_context"),
                          new CodeArgumentReferenceExpression("expression"),
                        new CodePrimitiveExpression(null),
                        new CodePrimitiveExpression(null))));
            _catch = new CodeCatchClause("e_",new CodeTypeReference(typeof(Exception)));
            _catch.Statements.Add(new CodeThrowExceptionStatement(new CodeVariableReferenceExpression("e_")));
            trycatch.CatchClauses.Add(_catch);
            trycatch.FinallyStatements.Add(
                new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(new CodeVariableReferenceExpression("_context"),
                        "Dispose")));
            #endregion
        }
        private void BuilderLoad()
        {
            CodeMemberMethod load = new CodeMemberMethod();
            load.Name = "Load";
            load.ReturnType = new CodeTypeReference(Name);
            load.Parameters.Add(new CodeParameterDeclarationExpression(
               TypeReferenceFactory.IConnectinContext(), "context"));
            load.Parameters.Add(new CodeParameterDeclarationExpression(
                new CodeTypeReference(ID.Type), "id"));
            load.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            mEntity.Members.Add(load);
            load.Statements.Add(new CodeMethodReturnStatement(
                new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(
                        new CodeTypeReferenceExpression("EntityBase"), "OnLoad",
                        new CodeTypeReference(Name)),
                        new CodeArgumentReferenceExpression("context"),
                         new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(Name), "mSelectString"),
                        new CodePrimitiveExpression(ID.Column),
                        new CodeArgumentReferenceExpression("id"))));


            load = new CodeMemberMethod();
            load.Name = "Load";
            load.ReturnType = new CodeTypeReference(Name);
            load.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            load.Parameters.Add(new CodeParameterDeclarationExpression(
                new CodeTypeReference(ID.Type), "id"));
            mEntity.Members.Add(load);
            CodeVariableDeclarationStatement context = new CodeVariableDeclarationStatement(TypeReferenceFactory.IConnectinContext(), "_context");
            CodeAssignStatement setcontext = GetContext();
            CodeTryCatchFinallyStatement trycatch = new CodeTryCatchFinallyStatement();
            CodeCatchClause _catch;
            load.Statements.Add(context);
            load.Statements.Add(setcontext);
            load.Statements.Add(trycatch);
            trycatch.TryStatements.Add(new CodeMethodReturnStatement(
                new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(Name), "Load"),
                        new CodeVariableReferenceExpression("_context"),
                        new CodeArgumentReferenceExpression("id"))));
            _catch = new CodeCatchClause("e_", new CodeTypeReference(typeof(Exception)));
            _catch.Statements.Add(new CodeThrowExceptionStatement(new CodeVariableReferenceExpression("e_")));
            trycatch.CatchClauses.Add(_catch);
            trycatch.FinallyStatements.Add(
                new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(new CodeVariableReferenceExpression("_context"),
                        "Dispose")));
        }

        private CodeAssignStatement GetContext()
        {
            switch (DBContext)
            {
                case "context":
                    return new CodeAssignStatement(new CodeVariableReferenceExpression("_context"),
                        new CodePropertyReferenceExpression(new CodeTypeReferenceExpression("DBContext"), "Context"));
                    break;
                case "context1":
                    return new CodeAssignStatement(new CodeVariableReferenceExpression("_context"),
                     new CodePropertyReferenceExpression(new CodeTypeReferenceExpression("DBContext"), "Context1"));
                    break;
                case "context2":
                    return new CodeAssignStatement(new CodeVariableReferenceExpression("_context"),
                     new CodePropertyReferenceExpression(new CodeTypeReferenceExpression("DBContext"), "Context2"));
                    break;
                case "context3":
                    return new CodeAssignStatement(new CodeVariableReferenceExpression("_context"),
                     new CodePropertyReferenceExpression(new CodeTypeReferenceExpression("DBContext"), "Context3"));
                    break;
                case "context4":
                    return new CodeAssignStatement(new CodeVariableReferenceExpression("_context"),
                     new CodePropertyReferenceExpression(new CodeTypeReferenceExpression("DBContext"), "Context4"));
                    break;
                case "context5":
                    return new CodeAssignStatement(new CodeVariableReferenceExpression("_context"),
                     new CodePropertyReferenceExpression(new CodeTypeReferenceExpression("DBContext"), "Context5"));
                    break;
                default:
                    return new CodeAssignStatement(new CodeVariableReferenceExpression("_context"),
                     new CodePropertyReferenceExpression(new CodeTypeReferenceExpression("DBContext"), "Context"));
                    break;
            }

        }
        private void BuilderSave() {
            CodeMemberMethod save = new CodeMemberMethod();
            save.Name = "Save";
            save.Attributes = MemberAttributes.Public;
            save.Parameters.Add(
               new CodeParameterDeclarationExpression(
                   TypeReferenceFactory.IConnectinContext(), "context"));
            mEntity.Members.Add(save);
            save.Statements.Add(new CodeVariableDeclarationStatement(TypeReferenceFactory.IList_1("Field") , "_fields"));
            save.Statements.Add(new CodeVariableDeclarationStatement(
                new CodeTypeReference("Field"), "_id"));
            save.Statements.Add(new CodeVariableDeclarationStatement(
                new CodeTypeReference("Field"), "item"));
            save.Statements.Add(
                new CodeAssignStatement(new CodeVariableReferenceExpression("_fields"),
                    new CodeObjectCreateExpression(TypeReferenceFactory.List_1("Field"))));
            save.Statements.Add(new CodeAssignStatement(
                new CodeVariableReferenceExpression("_id"),
                new CodeObjectCreateExpression(new CodeTypeReference("Field"), new CodeExpression[0])));
            save.Statements.Add(
                new CodeAssignStatement(
                    new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("_id"), "Name"),
                    new CodePrimitiveExpression(ID.Column)));
            save.Statements.Add(
               new CodeAssignStatement(
                   new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("_id"), "Value"),
                   new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), ID.Name)));
            //GetValueBy GetValueAfterInsert;
            save.Statements.Add(
               new CodeAssignStatement(
                   new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("_id"), "GetValueBy"),
                   new CodePrimitiveExpression(ID.GetValueBy)));

            save.Statements.Add(
               new CodeAssignStatement(
                   new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("_id"), "GetValueAfterInsert"),
                   new CodePrimitiveExpression(ID.GetValueAfterInsert)));
            foreach (Property p in Properties) {
                save.Statements.Add(new CodeAssignStatement(
               new CodeVariableReferenceExpression("item"),
               new CodeObjectCreateExpression(new CodeTypeReference("Field"), new CodeExpression[0])));
                save.Statements.Add(
               new CodeAssignStatement(
                   new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("item"), "Name"),
                   new CodePrimitiveExpression(p.Column)));
                save.Statements.Add(
                   new CodeAssignStatement(
                       new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("item"), "Value"),
                       new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), p.Name)));
                save.Statements.Add(
                    new CodeMethodInvokeExpression(
                        new CodeMethodReferenceExpression(new CodeVariableReferenceExpression("_fields"), "Add"),
                        new CodeVariableReferenceExpression("item")));

            }
            save.Statements.Add(
                new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(
                        new CodeThisReferenceExpression(), "OnSave"),
                        new CodeArgumentReferenceExpression("context"),
                        new CodePrimitiveExpression(Table),
                        new CodeVariableReferenceExpression("_fields"),
                        new CodeVariableReferenceExpression("_id")));
            save.Statements.Add(
                new CodeAssignStatement(
                    new CodePropertyReferenceExpression(
                        new CodeThisReferenceExpression(), ID.Name),
                        new CodeCastExpression(new CodeTypeReference(ID.Type),
                            new CodeMethodInvokeExpression(
                            new CodeThisReferenceExpression(), "Convert",
                            new CodeTypeOfExpression(new CodeTypeReference(ID.Type)),
                        new CodePropertyReferenceExpression(
                            new CodeVariableReferenceExpression("_id"),"Value")))));


            save = new CodeMemberMethod();
            save.Name = "Save";
            save.Attributes = MemberAttributes.Public;
            mEntity.Members.Add(save);
            CodeVariableDeclarationStatement context = new CodeVariableDeclarationStatement(TypeReferenceFactory.IConnectinContext(), "_context");
            CodeAssignStatement setcontext = GetContext();
            CodeTryCatchFinallyStatement trycatch = new CodeTryCatchFinallyStatement();
            CodeCatchClause _catch;
            save.Statements.Add(context);
            save.Statements.Add(setcontext);
            save.Statements.Add(trycatch);
            
            _catch = new CodeCatchClause("e_", new CodeTypeReference(typeof(Exception)));
            _catch.Statements.Add(new CodeThrowExceptionStatement(new CodeVariableReferenceExpression("e_")));
            trycatch.CatchClauses.Add(_catch);
            trycatch.TryStatements.Add(
                new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(
                        new CodeThisReferenceExpression(), "Save"),
                        new CodeVariableReferenceExpression("_context")));
            trycatch.FinallyStatements.Add(
                new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(new CodeVariableReferenceExpression("_context"),
                        "Dispose")));


        }

        private void BuilderCount()
        {
                    
            CodeMemberMethod count = new CodeMemberMethod();
            count.Name = "Count";
            count.ReturnType = new CodeTypeReference(typeof(int));
            count.Parameters.Add(
               new CodeParameterDeclarationExpression(
                   TypeReferenceFactory.IConnectinContext(), "context"));
            count.Parameters.Add(

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -