Beispiel #1
0
        /// <summary>
        /// Generates a (potentially multi-line) Xml doc comment for a Param element.
        /// </summary>
        /// <param name="paramName">The name of the parameter.</param>
        /// <param name="comment">The formatted string to embed in a Param element.</param>
        /// <param name="isCSharp">Whether or not the doc comment is for C#.</param>
        /// <returns>The collection of generated Xml doc comments.</returns>
        internal static CodeCommentStatementCollection GenerateParamCodeComment(string paramName, string comment, bool isCSharp)
        {
            System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(paramName), "paramName cannot be empty");
            System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(comment), "comment cannot be empty");

            return(CodeGenUtilities.GetDocComments("<param name=\"" + paramName + "\">" + comment + "</param>", isCSharp));
        }
Beispiel #2
0
        /// <summary>
        /// Adds a method invoke expression and a partial method definition based on the specified base method name
        /// to the internal method collection.
        /// </summary>
        /// <param name="baseMethodName">base method name w/o the On prefix (like Created for OnCreated)</param>
        /// <param name="parameters">if provided, the parameters for the method to be generated</param>
        /// <param name="comments">the comments for the partial property definition</param>
        public void AddMethodFor(string baseMethodName, CodeParameterDeclarationExpressionCollection parameters, string comments)
        {
            Debug.Assert(!string.IsNullOrEmpty(baseMethodName), "Unexpected null or empty base method name!");

            if (!string.IsNullOrEmpty(baseMethodName))
            {
                if (!this.methodInvokeExpressions.ContainsKey(baseMethodName))
                {
                    string methodName = string.Concat("On", baseMethodName);

                    List <CodeArgumentReferenceExpression> args = new List <CodeArgumentReferenceExpression>();

                    if (parameters != null && parameters.Count > 0)
                    {
                        foreach (CodeParameterDeclarationExpression paramDeclaration in parameters)
                        {
                            args.Add(new CodeArgumentReferenceExpression(paramDeclaration.Name));
                        }
                    }

                    // Create method call.
                    // OnMethod(arg1, arg2);
                    this.methodInvokeExpressions.Add(baseMethodName, new CodeMethodInvokeExpression(
                                                         new CodeThisReferenceExpression(),
                                                         methodName,
                                                         args.ToArray()));

                    // Create method declaration.
                    // partial void OnMethod(Type1 param1, Type2 param2);
                    CodeSnippetTypeMember codeSnippet = this.CreateNotificationPartialMethod(baseMethodName, parameters);

                    if (!string.IsNullOrEmpty(comments))
                    {
                        // Add comment on method declaration.
                        codeSnippet.Comments.AddRange(CodeGenUtilities.GetDocComments(comments, this.isCSharp));
                    }

                    this.partialMethodSnippets.Add(baseMethodName, codeSnippet);
                }
            }
        }
Beispiel #3
0
        public override void Generate()
        {
            // ----------------------------------------------------------------
            // namespace
            // ----------------------------------------------------------------
            CodeNamespace ns = this.ClientProxyGenerator.GetOrGenNamespace(this.ClientProxyGenerator.ClientProxyCodeGenerationOptions.ClientRootNamespace);

            // Missing namespace bails out of code-gen -- error has been logged
            if (ns == null)
            {
                return;
            }

            // Log an informational message to help users see progress
            this.ClientProxyGenerator.LogMessage(Resource.CodeGen_Generating_WebContext);

            // Find the AuthenticationServices and if there's just one, use it as the default.
            IEnumerable <DomainServiceDescription> authDescriptions =
                this.ClientProxyGenerator.DomainServiceDescriptions.Where(d => typeof(IAuthentication <>).DefinitionIsAssignableFrom(d.DomainServiceType));
            DomainServiceDescription defaultAuthDescription = null;

            if (authDescriptions.Count() > 1)
            {
                this.ClientProxyGenerator.LogMessage(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        Resource.WebContext_ManyAuthServices,
                        string.Join(",", authDescriptions.Select(d => d.DomainServiceType.Name).ToArray())));
            }
            else
            {
                defaultAuthDescription = authDescriptions.FirstOrDefault();
            }

            // ----------------------------------------------------------------
            // public partial sealed class WebContext : WebContextBase
            // ----------------------------------------------------------------
            CodeTypeDeclaration proxyClass = CodeGenUtilities.CreateTypeDeclaration("WebContext", ns.Name);

            proxyClass.IsPartial      = true;
            proxyClass.TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed;
            proxyClass.BaseTypes.Add(CodeGenUtilities.GetTypeReference(TypeConstants.WebContextBaseName, ns.Name, false));
            proxyClass.Comments.AddRange(CodeGenUtilities.GetDocComments(Resource.WebContext_CommentClass, this.ClientProxyGenerator.IsCSharp));

            ns.Types.Add(proxyClass);

            // ----------------------------------------------------------------
            // public WebContext()
            // {
            //     <!-- if there's a default authentication service
            //     this.Authentication = new WebUserService();
            //     -->
            //     this.OnCreated();
            // }
            // ----------------------------------------------------------------
            CodeConstructor constructor = new CodeConstructor();

            constructor.Attributes = MemberAttributes.Public;
            //if (defaultAuthDescription != null)
            //{
            //    // TODO: Choose between Forms and Windows when reading from web.config is available
            //    //constructor.Statements.Add(
            //    //    new CodeAssignStatement(
            //    //        new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "Authentication"),
            //    //        new CodeObjectCreateExpression(WebContextGenerator.FormsAuthenticationName)));
            //}
            NotificationMethodGenerator onCreatedMethodGenerator = new NotificationMethodGenerator(
                this.ClientProxyGenerator,
                this.ClientProxyGenerator.IsCSharp ? IndentationLevel.Namespace : IndentationLevel.GlobalNamespace);

            constructor.Statements.Add(onCreatedMethodGenerator.OnCreatedMethodInvokeExpression);
            constructor.Comments.AddRange(CodeGenUtilities.GetDocComments(Resource.WebContext_CommentConstructor, this.ClientProxyGenerator.IsCSharp));

            proxyClass.Members.Add(constructor);

            // ----------------------------------------------------------------
            // #region Extensibility Method Definitions
            // partial void OnCreated();
            // #endregion
            // ----------------------------------------------------------------
            proxyClass.Members.AddRange(onCreatedMethodGenerator.PartialMethodsSnippetBlock);

            // ----------------------------------------------------------------
            // public static new WebContext Current
            // {
            //     get { return (WebContext)WebContextBase.Current; }
            // }
            // ----------------------------------------------------------------
            CodeMemberProperty          currentProperty = new CodeMemberProperty();
            string                      typeFullName    = (string.IsNullOrEmpty(ns.Name) ? string.Empty : ns.Name + ".") + "WebContext";
            CodeTypeReference           targetTypeRef   = CodeGenUtilities.GetTypeReference(typeFullName, ns.Name, true);
            CodeTypeReference           baseTypeRef     = CodeGenUtilities.GetTypeReference(TypeConstants.WebContextBaseName, ns.Name, false);
            CodeTypeReferenceExpression baseTypeRefExp  = new CodeTypeReferenceExpression(baseTypeRef);

            currentProperty.Attributes = MemberAttributes.Public | MemberAttributes.Static | MemberAttributes.New;
            currentProperty.Type       = targetTypeRef;
            currentProperty.Name       = "Current";
            currentProperty.HasGet     = true;
            currentProperty.GetStatements.Add(
                new CodeMethodReturnStatement(
                    new CodeCastExpression(currentProperty.Type,
                                           new CodePropertyReferenceExpression(baseTypeRefExp, "Current"))));
            currentProperty.Comments.AddRange(CodeGenUtilities.GetDocComments(Resource.WebContext_CommentCurrent, this.ClientProxyGenerator.IsCSharp));

            proxyClass.Members.Add(currentProperty);

            // ----------------------------------------------------------------
            // <!-- if there's a default authentication service
            // public new MyUser User
            // {
            //     get { return (MyUser)base.User; }
            // }
            // -->
            // ----------------------------------------------------------------
            if (defaultAuthDescription != null)
            {
                Type genericType = null;
                typeof(IAuthentication <>).DefinitionIsAssignableFrom(defaultAuthDescription.DomainServiceType, out genericType);
                if ((genericType != null) && (genericType.GetGenericArguments().Count() == 1))
                {
                    CodeMemberProperty userProperty = new CodeMemberProperty();

                    userProperty.Attributes = MemberAttributes.Public | MemberAttributes.New | MemberAttributes.Final;
                    userProperty.Type       = CodeGenUtilities.GetTypeReference(
                        genericType.GetGenericArguments()[0], this.ClientProxyGenerator, proxyClass);
                    userProperty.Name   = "User";
                    userProperty.HasGet = true;
                    userProperty.GetStatements.Add(
                        new CodeMethodReturnStatement(
                            new CodeCastExpression(userProperty.Type,
                                                   new CodePropertyReferenceExpression(
                                                       new CodeBaseReferenceExpression(),
                                                       "User"))));
                    userProperty.Comments.AddRange(CodeGenUtilities.GetDocComments(Resource.WebContext_CommentUser, this.ClientProxyGenerator.IsCSharp));

                    proxyClass.Members.Add(userProperty);
                }
            }
        }
Beispiel #4
0
 /// <summary>
 /// Generates a (potentially multi-line) Xml doc comment for a Returns element.
 /// </summary>
 /// <param name="comment">The formatted string to embed in the Returns element.</param>
 /// <param name="isCSharp">Whether or not the doc comment is for C#.</param>
 /// <returns>The collection of generated Xml doc comments.</returns>
 internal static CodeCommentStatementCollection GenerateReturnsCodeComment(string comment, bool isCSharp)
 {
     System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(comment), "comment cannot be empty");
     return(CodeGenUtilities.GetDocComments("<returns>" + comment + "</returns>", isCSharp));
 }
Beispiel #5
0
 /// <summary>
 /// Generates a (potentially multi-line) Xml doc comment for a Summary element.
 /// </summary>
 /// <param name="comment">The formatted string to embed within a Summary element.  If it contains line breaks, it will become multiple comments.</param>
 /// <param name="isCSharp">Whether or not the doc comment is for C#.</param>
 /// <returns>The collection of generated Xml doc comments.</returns>
 internal static CodeCommentStatementCollection GenerateSummaryCodeComment(string comment, bool isCSharp)
 {
     System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(comment), "comment cannot be empty");
     return(CodeGenUtilities.GetDocComments("<summary>" + Environment.NewLine + comment + Environment.NewLine + "</summary>", isCSharp));
 }