/// <summary>
        /// Generates the business logic class type.  We override this to control the base class and imports
        /// </summary>
        /// <param name="codeGenContext">The context to use to generate code.</param>
        /// <param name="codeNamespace">The namespace into which to generate code.</param>
        /// <param name="className">The name to use for the class.</param>
        /// <returns>The new type</returns>
        protected override CodeTypeDeclaration CreateBusinessLogicClass(CodeGenContext codeGenContext, CodeNamespace codeNamespace, string className)
        {
            // Add an import for our domain service
            foreach (string import in BusinessLogicClassConstants.LinqToEntitiesDbImports)
            {
                codeNamespace.Imports.Add(new CodeNamespaceImport(import));
            }

            // Add an import for the namespace of the DomainContext
            if (this.ContextType.Namespace != codeNamespace.Name)
            {
                codeNamespace.Imports.Add(new CodeNamespaceImport(BusinessLogicClassConstants.DbContextNamespace));
                codeNamespace.Imports.Add(new CodeNamespaceImport(this.ContextType.Namespace));
            }

            // Add to the set of known references
            codeGenContext.AddReference(typeof(EntityState).Assembly.FullName);

            // We used to add OpenRiaServices.DomainServices.EntityFramework, but due to
            // vstfdevdiv/DevDiv2 Bug 442272 - Domain Service Wizard failing when an EF DbContext is selected,
            // we need to avoid doing that.

            if (DbContextUtilities.DbContextTypeReference != null)
            {
                codeGenContext.AddReference(DbContextUtilities.DbContextTypeReference.Assembly.FullName);
            }

            CodeTypeDeclaration businessLogicClass = CodeGenUtilities.CreateTypeDeclaration(className, codeNamespace.Name);
            CodeTypeReference   baseClass          = new CodeTypeReference(BusinessLogicClassConstants.DbDomainServiceTypeName, new CodeTypeReference(this.ContextType.Name));

            businessLogicClass.BaseTypes.Add(baseClass);
            return(businessLogicClass);
        }
        /// <summary>
        /// Creates the business logic class.  Overridden to add imports, base class, etc.
        /// </summary>
        /// <param name="codeGenContext">The code gen context.></param>
        /// <param name="codeNamespace">The namespace name.</param>
        /// <param name="className">The class name.</param>
        /// <returns>the new type</returns>
        protected override CodeTypeDeclaration CreateBusinessLogicClass(CodeGenContext codeGenContext, CodeNamespace codeNamespace, string className)
        {
            Debug.Assert(LinqToSqlContext.linqToSqlDomainServiceAssemblyPath != null, "Unexpected method call when LinqToSqlDomainService assembly path has not been initialized!");

            if (LinqToSqlContext.linqToSqlDomainServiceAssemblyPath == null)
            {
                return(null);
            }

            // Add an import for our domain service
            foreach (string import in BusinessLogicClassConstants.LinqToSqlImports)
            {
                codeNamespace.Imports.Add(new CodeNamespaceImport(import));
            }

            // Add an import for the namespace of the DomainContext
            if (this.ContextType.Namespace != codeNamespace.Name)
            {
                codeNamespace.Imports.Add(new CodeNamespaceImport(this.ContextType.Namespace));
            }

            // Add to the set of known references
            codeGenContext.AddReference(typeof(DataContext).Assembly.FullName);
            codeGenContext.AddReference(LinqToSqlContext.linqToSqlDomainServiceAssemblyPath);

            CodeTypeDeclaration businessLogicClass = CodeGenUtilities.CreateTypeDeclaration(className, codeNamespace.Name);
            CodeTypeReference   baseClass          = new CodeTypeReference(BusinessLogicClassConstants.LinqToSqlDomainServiceTypeName, new CodeTypeReference(this.ContextType.Name));

            businessLogicClass.BaseTypes.Add(baseClass);
            return(businessLogicClass);
        }
Exemple #3
0
        /// <summary>
        /// Generates the business logic class type.  We override this to control the base class and imports
        /// </summary>
        /// <param name="codeGenContext">The context to use to generate code.</param>
        /// <param name="codeNamespace">The namespace into which to generate code.</param>
        /// <param name="className">The name to use for the class.</param>
        /// <returns>The new type</returns>
        protected override CodeTypeDeclaration CreateBusinessLogicClass(CodeGenContext codeGenContext, CodeNamespace codeNamespace, string className)
        {
            // Add an import for our domain service
            foreach (string import in BusinessLogicClassConstants.LinqToEntitiesImports)
            {
                codeNamespace.Imports.Add(new CodeNamespaceImport(import));
            }

            // Add an import for the namespace of the DomainContext
            if (this.ContextType.Namespace != codeNamespace.Name)
            {
                codeNamespace.Imports.Add(new CodeNamespaceImport(this.ContextType.Namespace));
            }

            // Add to the set of known references
            codeGenContext.AddReference(typeof(ObjectContext).Assembly.FullName);
            codeGenContext.AddReference(typeof(LinqToEntitiesDomainService <>).Assembly.FullName);

            CodeTypeDeclaration businessLogicClass = CodeGenUtilities.CreateTypeDeclaration(className, codeNamespace.Name);
            CodeTypeReference   baseClass          = new CodeTypeReference(BusinessLogicClassConstants.LinqToEntitiesDomainServiceTypeName, new CodeTypeReference(this.ContextType.Name));

            businessLogicClass.BaseTypes.Add(baseClass);
            return(businessLogicClass);
        }
Exemple #4
0
        /// <summary>
        /// Creates the entire business logic class within the specified namespace name
        /// </summary>
        /// <param name="codeGenContext">The context into which to generate code.  It cannot be null.</param>
        /// <param name="className">The name of the class to generate.  It cannot be null or empty.</param>
        /// <param name="namespaceName">The namespace to use for the generated code.  It cannot be empty.</param>
        protected void GenerateBusinessLogicClass(CodeGenContext codeGenContext, string className, string namespaceName)
        {
            if (codeGenContext == null)
            {
                throw new ArgumentNullException("codeGenContext");
            }
            if (string.IsNullOrEmpty(className))
            {
                throw new ArgumentNullException("className");
            }
            if (string.IsNullOrEmpty(namespaceName))
            {
                throw new ArgumentNullException("namespaceName");
            }

            // namespace XXX { }
            CodeNamespace ns = codeGenContext.GetOrGenNamespace(namespaceName);

            // public class $classname$ { }
            CodeTypeDeclaration businessLogicClass = this.CreateBusinessLogicClass(codeGenContext, ns, className);

            ns.Types.Add(businessLogicClass);

            // Class-level Xml comments
            // Empty class gets its own comment because it has no ContextType to describe
            string remarksComment;

            if (this.ContextType == null)
            {
                remarksComment = Resources.BusinessLogicClass_Class_Remarks_Empty;
            }
            else
            {
                remarksComment = String.Format(CultureInfo.CurrentCulture, Resources.BusinessLogicClass_Class_Remarks, this.ContextType.Name);
            }

            // Add developer comment explaining what this class does
            businessLogicClass.Comments.Add(new CodeCommentStatement(remarksComment, false));

            // Add [RequiresAuthentication] as a comment
            if (this.ContextType != null)
            {
                remarksComment = codeGenContext.IsCSharp
                                    ? Resources.BusinessLogicClass_RequiresAuthentication_CSharp
                                    : Resources.BusinessLogicClass_RequiresAuthentication_VB;
                businessLogicClass.Comments.Add(new CodeCommentStatement(remarksComment, false));
            }

            if (this.IsClientAccessEnabled)
            {
                // [EnableClientAccess]
                CodeAttributeDeclaration attr = CodeGenUtilities.CreateAttributeDeclaration(BusinessLogicClassConstants.EnableClientAccessAttributeTypeName);
                businessLogicClass.CustomAttributes.Add(attr);
            }
            else
            {
                // if not enabled, add a comment explaining how to enable it
                businessLogicClass.Comments.Add(new CodeCommentStatement(Resources.BusinessLogicClass_EnableClientAccess_Comment));
            }

            // Gen all domain operation entries
            // Sort by name for baseline predictability
            foreach (BusinessLogicEntity entity in this.Entities.OrderBy(e => e.Name))
            {
                if (entity.IsIncluded)
                {
                    // Add an import for this entity's namespace if needed
                    // This is necessary only when entities exist in a different namespace from the context
                    CodeGenUtilities.AddImportIfNeeded(ns, entity.ClrType.Namespace);

                    this.GenerateEntityDomainOperationEntries(codeGenContext, businessLogicClass, entity);
                }
            }

            // If any private helper methods were generated, append them now.
            // We sort by their keys to give baseline predictability.
            Dictionary <string, CodeTypeMember> helpers = BusinessLogicContext.GetHelperMemberDictionary(businessLogicClass);

            foreach (string key in helpers.Keys.OrderBy(s => s))
            {
                businessLogicClass.Members.Add(helpers[key]);
            }

            // If we exposed an OData endpoint, add a reference to the OData assembly
            // so it appears in the server project, allowing the user to chose
            // CopyLocal=true for bin deploy scenarios
            if (this.IsODataEndpointEnabled)
            {
                codeGenContext.AddReference(typeof(ODataEndpointFactory).Assembly.FullName);
            }
        }
        /// <summary>
        /// Creates the entire business logic class within the specified namespace name
        /// </summary>
        /// <param name="codeGenContext">The context into which to generate code.  It cannot be null.</param>
        /// <param name="className">The name of the class to generate.  It cannot be null or empty.</param>
        /// <param name="namespaceName">The namespace to use for the generated code.  It cannot be empty.</param>
        protected void GenerateBusinessLogicClass(CodeGenContext codeGenContext, string className, string namespaceName)
        {
            if (codeGenContext == null)
            {
                throw new ArgumentNullException("codeGenContext");
            }
            if (string.IsNullOrEmpty(className))
            {
                throw new ArgumentNullException("className");
            }
            if (string.IsNullOrEmpty(namespaceName))
            {
                throw new ArgumentNullException("namespaceName");
            }

            // namespace XXX { }
            CodeNamespace ns = codeGenContext.GetOrGenNamespace(namespaceName);

            // public class $classname$ { }
            CodeTypeDeclaration businessLogicClass = this.CreateBusinessLogicClass(codeGenContext, ns, className);
            ns.Types.Add(businessLogicClass);

            // Class-level Xml comments
            // Empty class gets its own comment because it has no ContextType to describe
            string remarksComment;
            if (this.ContextType == null)
            {
                remarksComment = Resources.BusinessLogicClass_Class_Remarks_Empty;
            }
            else
            {
                remarksComment = String.Format(CultureInfo.CurrentCulture, Resources.BusinessLogicClass_Class_Remarks, this.ContextType.Name);
            }

            // Add developer comment explaining what this class does
            businessLogicClass.Comments.Add(new CodeCommentStatement(remarksComment, false));

            // Add [RequiresAuthentication] as a comment
            if (this.ContextType != null)
            {
                remarksComment = codeGenContext.IsCSharp
                                    ? Resources.BusinessLogicClass_RequiresAuthentication_CSharp
                                    : Resources.BusinessLogicClass_RequiresAuthentication_VB;
                businessLogicClass.Comments.Add(new CodeCommentStatement(remarksComment, false));
            }

            if (this.IsClientAccessEnabled)
            {
                // [EnableClientAccess]
                CodeAttributeDeclaration attr = CodeGenUtilities.CreateAttributeDeclaration(BusinessLogicClassConstants.EnableClientAccessAttributeTypeName);
                businessLogicClass.CustomAttributes.Add(attr);
            }
            else
            {
                // if not enabled, add a comment explaining how to enable it
                businessLogicClass.Comments.Add(new CodeCommentStatement(Resources.BusinessLogicClass_EnableClientAccess_Comment));
            }

            // Gen all domain operation entries
            // Sort by name for baseline predictability
            foreach (BusinessLogicEntity entity in this.Entities.OrderBy(e => e.Name))
            {
                if (entity.IsIncluded)
                {
                    // Add an import for this entity's namespace if needed
                    // This is necessary only when entities exist in a different namespace from the context
                    CodeGenUtilities.AddImportIfNeeded(ns, entity.ClrType.Namespace);

                    this.GenerateEntityDomainOperationEntries(codeGenContext, businessLogicClass, entity);
                }
            }

            // If any private helper methods were generated, append them now.
            // We sort by their keys to give baseline predictability.
            Dictionary<string, CodeTypeMember> helpers = BusinessLogicContext.GetHelperMemberDictionary(businessLogicClass);
            foreach (string key in helpers.Keys.OrderBy(s => s))
            {
                businessLogicClass.Members.Add(helpers[key]);
            }

            // If we exposed an OData endpoint, add a reference to the OData assembly
            // so it appears in the server project, allowing the user to chose 
            // CopyLocal=true for bin deploy scenarios
            if (this.IsODataEndpointEnabled)
            {
                codeGenContext.AddReference(typeof(ODataEndpointFactory).Assembly.FullName);
            }
        }