Ejemplo n.º 1
0
 /// <summary>
 /// Tests whether we need to generate the GetEntityState helper method.  We do for POCO types.
 /// If we determine we need to generate that helper, this method generates it and adds it to
 /// the list of helper methods that will be appended to the generated code.
 /// </summary>
 /// <param name="codeGenContext">The context in which we are generating code.</param>
 /// <param name="businessLogicClass">The class containing the generated code.</param>
 /// <param name="entity">The entity that we need to test to determine whether the helper is needed.</param>
 /// <returns><c>true</c> means the helper should be used.</returns>
 private static bool GenerateGetEntityStateIfNecessary(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
 {
     if (typeof(EntityObject).IsAssignableFrom(entity.ClrType))
     {
         return(false);
     }
     BusinessLogicContext.GenerateHelperMemberIfNecessary(codeGenContext, businessLogicClass, LinqToEntitiesContext.GetEntityStateHelperMethodName, () =>
     {
         return(LinqToEntitiesContext.GenerateGetEntityState(codeGenContext, businessLogicClass));
     });
     return(true);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates the select domain operation entry
        /// </summary>
        /// <param name="codeGenContext">The code gen context.</param>
        /// <param name="businessLogicClass">Contains the business logic.</param>
        /// <param name="entity">The entity.</param>
        /// <returns>The newly created method.</returns>
        protected override CodeMemberMethod GenerateSelectMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
        {
            CodeMemberMethod     method   = null;
            LinqToEntitiesEntity efEntity = entity as LinqToEntitiesEntity;

            if (efEntity != null && efEntity.DefaultObjectSetName != null)
            {
                // public IQueryable<$entityType$> GetEntities()
                method = new CodeMemberMethod();
                businessLogicClass.Members.Add(method);

                // Add developer comment explaining they can add additional parameters
                method.Comments.Add(new CodeCommentStatement(Resources.BusinessLogicClass_Query_Method_Remarks, false));

                // And for EF, we add an additional comment warning they need to add ordering if they want paging
                string queryComment = String.Format(CultureInfo.CurrentCulture, Resources.BusinessLogicClass_Query_Method_EF_Remarks, efEntity.DefaultObjectSetName);
                method.Comments.Add(new CodeCommentStatement(queryComment, false));

                method.Name       = "Get" + CodeGenUtilities.MakeLegalEntityName(efEntity.DefaultObjectSetName);
                method.ReturnType = new CodeTypeReference("IQueryable", new CodeTypeReference(entity.Name));
                method.Attributes = MemberAttributes.Public | MemberAttributes.Final;   // final needed to prevent virtual

                // return this.ObjectContext.$TablePropertyName$
                CodeExpression            contextExpr = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "ObjectContext");
                CodeExpression            expr        = new CodePropertyReferenceExpression(contextExpr, efEntity.DefaultObjectSetName);
                CodeMethodReturnStatement returnStmt  = new CodeMethodReturnStatement(expr);
                method.Statements.Add(returnStmt);
            }
            return(method);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Generates the update domain operation entry
        /// </summary>
        /// <param name="codeGenContext">The context to use</param>
        /// <param name="businessLogicClass">The business logic class into which to generate it</param>
        /// <param name="entity">The entity for which to generate the method</param>
        protected override void GenerateUpdateMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
        {
            string currentParameterName = "current" + entity.ClrType.Name;

            // public void Update$EntityName$($entityType$ current)
            CodeMemberMethod method = new CodeMemberMethod();

            businessLogicClass.Members.Add(method);

            //LinqToEntitiesEntity efEntity = (LinqToEntitiesEntity)entity;
            method.Name       = "Update" + CodeGenUtilities.MakeLegalEntityName(entity.Name);
            method.Attributes = MemberAttributes.Public | MemberAttributes.Final;   // final needed to prevent virtual

            // parameter declaration
            method.Parameters.Add(new CodeParameterDeclarationExpression(entity.ClrType.Name, currentParameterName));

            LinqToEntitiesEntity efEntity = (LinqToEntitiesEntity)entity;

            if (!efEntity.HasTimestampMember)
            {
                // this.ChangeSet.GetOriginal(current)
                CodeExpression changeSetRef = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "ChangeSet");
                CodeMethodReferenceExpression getOrigMethodRef = new CodeMethodReferenceExpression(changeSetRef, "GetOriginal");
                CodeMethodInvokeExpression    changeSetGetOrig = new CodeMethodInvokeExpression(getOrigMethodRef, new CodeArgumentReferenceExpression(currentParameterName));

                // this.ObjectContext.$ObjectSetName$.AttachAsModified($current$, this.ChangeSet.GetOriginal(current));
                CodeExpression contextRef = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "ObjectContext");
                CodePropertyReferenceExpression objectSetRef = new CodePropertyReferenceExpression(contextRef, efEntity.DefaultObjectSetName);
                CodeMethodInvokeExpression      attachCall   = new CodeMethodInvokeExpression(objectSetRef, "AttachAsModified", new CodeArgumentReferenceExpression(currentParameterName), changeSetGetOrig);
                CodeExpressionStatement         attachStmt   = new CodeExpressionStatement(attachCall);
                method.Statements.Add(attachStmt);
            }
            else
            {
                // this.ObjectContext.$ObjectSetName$.AttachAsModified($current$);
                CodeExpression contextRef = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "ObjectContext");
                CodePropertyReferenceExpression objectSetRef = new CodePropertyReferenceExpression(contextRef, efEntity.DefaultObjectSetName);
                CodeMethodInvokeExpression      attachCall   = new CodeMethodInvokeExpression(objectSetRef, "AttachAsModified", new CodeArgumentReferenceExpression(currentParameterName));
                CodeExpressionStatement         attachStmt   = new CodeExpressionStatement(attachCall);
                method.Statements.Add(attachStmt);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Generates the delete domain operation entry
        /// </summary>
        /// <param name="codeGenContext">The context to use</param>
        /// <param name="businessLogicClass">The business logic class into which to generate it</param>
        /// <param name="entity">The entity for which to generate the method</param>
        protected override void GenerateDeleteMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
        {
            string parameterName = CodeGenUtilities.MakeLegalParameterName(entity.Name);

            // public void Delete$EntityName$($entityType$ $entityName$)
            CodeMemberMethod method = new CodeMemberMethod();

            businessLogicClass.Members.Add(method);

            //LinqToEntitiesEntity efEntity = (LinqToEntitiesEntity)entity;
            method.Name       = "Delete" + CodeGenUtilities.MakeLegalEntityName(entity.Name);
            method.Attributes = MemberAttributes.Public | MemberAttributes.Final;   // final needed to prevent virtual

            // parameter declaration
            method.Parameters.Add(new CodeParameterDeclarationExpression(entity.ClrType.Name, parameterName));

            // if ($entity$.EntityState != EntityState.Detached)
            // {
            //      ObjectContext.ObjectStateManager.ChangeObjectState($entity$, EntityState.Deleted);
            // }
            // else
            // {
            //      ObjectContext.Products.Attach($entity$);
            //      ObjectContext.Products.DeleteObject($entity$);
            // }
            //
            // In the case of POCO objects, we use "this.GetEntityState(entity)"
            // rather than referring to the entity's EntityState property

            LinqToEntitiesEntity            efEntity   = (LinqToEntitiesEntity)entity;
            CodeArgumentReferenceExpression entityExpr = new CodeArgumentReferenceExpression(parameterName);

            // If this is a POCO class, we need to generate a call to a helper method to get
            // the EntityState, otherwise we can directly de-reference it on the entity
            // If this entity does not have an EntityState member, we need to use a helper method instead.
            // This call tells us whether we need this helper and, if so, generates it.
            bool useGetEntityStateHelper = LinqToEntitiesContext.GenerateGetEntityStateIfNecessary(codeGenContext, businessLogicClass, entity);

            CodeExpression getEntityStateExpr;

            if (useGetEntityStateHelper)
            {
                // this.GetEntityState($entity$)...
                getEntityStateExpr = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), LinqToEntitiesContext.GetEntityStateHelperMethodName, entityExpr);
            }
            else
            {
                // $entity$.EntityState...
                getEntityStateExpr = new CodePropertyReferenceExpression(entityExpr, LinqToEntitiesContext.EntityStatePropertyName);
            }

            CodeExpression contextRef = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "ObjectContext");
            CodePropertyReferenceExpression objectSetRef     = new CodePropertyReferenceExpression(contextRef, efEntity.DefaultObjectSetName);
            CodeFieldReferenceExpression    detachedStateRef = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(EntityState).Name), Enum.GetName(typeof(EntityState), EntityState.Detached));
            CodeExpression equalTest = CodeGenUtilities.MakeNotEqual(typeof(EntityState), getEntityStateExpr, detachedStateRef, codeGenContext.IsCSharp);

            CodeFieldReferenceExpression    deletedStateRef   = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(EntityState).Name), Enum.GetName(typeof(EntityState), EntityState.Deleted));
            CodePropertyReferenceExpression objectStateMgrRef = new CodePropertyReferenceExpression(contextRef, "ObjectStateManager");
            CodeMethodInvokeExpression      changeStateExpr   = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(objectStateMgrRef, "ChangeObjectState"), entityExpr, deletedStateRef);

            CodeStatement[] trueStatements = new CodeStatement[] { new CodeExpressionStatement(changeStateExpr) };

            CodeMethodInvokeExpression attachCall = new CodeMethodInvokeExpression(objectSetRef, "Attach", entityExpr);
            CodeMethodInvokeExpression deleteCall = new CodeMethodInvokeExpression(objectSetRef, "DeleteObject", entityExpr);

            CodeStatement[] falseStatements = new CodeStatement[] { new CodeExpressionStatement(attachCall), new CodeExpressionStatement(deleteCall) };

            CodeConditionStatement ifStmt = new CodeConditionStatement(equalTest, trueStatements, falseStatements);

            method.Statements.Add(ifStmt);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Creates the "delete" method for the entity
 /// </summary>
 /// <param name="codeGenContext">The context into which to generate code.  It cannot be null.</param>
 /// <param name="businessLogicClass">The class into which to generate the method</param>
 /// <param name="entity">The entity which will be affected by this method</param>
 protected virtual void GenerateDeleteMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
 {
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Generates additional metadata classes for the given entity if necessary.
 /// </summary>
 /// <param name="codeGenContext">The context to use to generate code.</param>
 /// <param name="optionalSuffix">If not null, optional suffix to class name and namespace</param>
 /// <param name="entity">The entity for which to generate the additional metadata.</param>
 /// <returns><c>true</c> means at least some code was generated.</returns>
 /// <remarks>
 /// This default implementation of the virtual method does not generate any additional classes. It needs to be overridden in the derived
 /// classes to generate additional metadata classes if necessary.
 /// </remarks>
 protected virtual bool GenerateAdditionalMetadataClasses(ICodeGenContext codeGenContext, string optionalSuffix, BusinessLogicEntity entity)
 {
     return(false);
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Creates all the domain operation entries for the given entity.
        /// </summary>
        /// <param name="codeGenContext">The context into which to generate code.  It cannot be null.</param>
        /// <param name="businessLogicClass">The class into which to generate the method</param>
        /// <param name="entity">The entity which will be affected by this method</param>
        public void GenerateEntityDomainOperationEntries(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
        {
            CodeMemberMethod selectMethod = this.GenerateSelectMethod(codeGenContext, businessLogicClass, entity);

            if (selectMethod != null)
            {
                // If OData endpoint is requested, generate [Query(IsDefault=true)].
                if (this.IsODataEndpointEnabled)
                {
                    CodeAttributeDeclaration attributeDeclaration = new CodeAttributeDeclaration(
                        new CodeTypeReference("Query"),
                        new CodeAttributeArgument[] { new CodeAttributeArgument("IsDefault", new CodePrimitiveExpression(true)) });
                    selectMethod.CustomAttributes.Add(attributeDeclaration);
                }
            }

            if (entity.IsEditable)
            {
                this.GenerateInsertMethod(codeGenContext, businessLogicClass, entity);
                this.GenerateUpdateMethod(codeGenContext, businessLogicClass, entity);
                this.GenerateDeleteMethod(codeGenContext, businessLogicClass, entity);
            }
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Creates the "select" domain operation entry for the entity
 /// </summary>
 /// <remarks>This base class unconditionally returns <c>null</c> and should not be called by derived classes.
 /// This allows the base class to be used for the blank <see cref="DomainService"/>.</remarks>
 /// <param name="codeGenContext">The context into which to generate code.  It cannot be null.</param>
 /// <param name="businessLogicClass">The class into which to generate the method</param>
 /// <param name="entity">The entity which will be affected by this method</param>
 /// <returns>The newly created method</returns>
 protected virtual CodeMemberMethod GenerateSelectMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
 {
     return(null);
 }
 /// <summary>
 /// Generates additional metadata classes for the given entity if necessary.
 /// </summary>
 /// <param name="codeGenContext">The context to use to generate code.</param>
 /// <param name="optionalSuffix">If not null, optional suffix to class name and namespace</param>
 /// <param name="entity">The entity for which to generate the additional metadata.</param>
 /// <returns><c>true</c> means at least some code was generated.</returns>
 /// <remarks>
 /// This default implementation of the virtual method does not generate any additional classes. It needs to be overridden in the derived
 /// classes to generate additional metadata classes if necessary.
 /// </remarks>
 protected virtual bool GenerateAdditionalMetadataClasses(ICodeGenContext codeGenContext, string optionalSuffix, BusinessLogicEntity entity)
 {
     return false;
 }
Ejemplo n.º 10
0
        /// <summary>
        /// If the entity contains properties of complex types, then this class generates metadata classes for those complex types.
        /// </summary>
        /// <param name="codeGenContext">The context to use to generate code.</param>
        /// <param name="optionalSuffix">If not null, optional suffix to class name and namespace</param>
        /// <param name="entity">The entity for which to generate the additional metadata.</param>
        /// <returns><c>true</c> means at least some code was generated.</returns>
        protected override bool GenerateAdditionalMetadataClasses(ICodeGenContext codeGenContext, string optionalSuffix, BusinessLogicEntity entity)
        {
            LinqToEntitiesEntity linqToEntitiesEntity = (LinqToEntitiesEntity)entity;
            bool generatedCode = this.GenerateMetadataClassesForComplexTypes(codeGenContext, optionalSuffix, linqToEntitiesEntity.EntityType.Properties);

            return(generatedCode);
        }
        /// <summary>
        /// Generates the delete domain operation entry
        /// </summary>
        /// <param name="codeGenContext">The context to use</param>
        /// <param name="businessLogicClass">The business logic class into which to generate it</param>
        /// <param name="entity">The entity for which to generate the method</param>
        protected override void GenerateDeleteMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
        {
            string parameterName = CodeGenUtilities.MakeLegalParameterName(entity.Name);

            // public void Delete$EntityName$($entityType$ $entityName$)
            CodeMemberMethod method = new CodeMemberMethod();

            businessLogicClass.Members.Add(method);

            LinqToEntitiesEntity efDbEntity = (LinqToEntitiesEntity)entity;

            method.Name       = "Delete" + CodeGenUtilities.MakeLegalEntityName(entity.Name);
            method.Attributes = MemberAttributes.Public | MemberAttributes.Final;   // final needed to prevent virtual

            // parameter declaration
            method.Parameters.Add(new CodeParameterDeclarationExpression(entity.ClrType.Name, parameterName));

            // Below we're generating the following method body

            // DbEntityEntry<$entityType$> entityEntry = this.DbContext.Entry($entity$);
            // if (entityEntry.State != EntityState.Detached)
            // {
            //     entityEntry.State = EntityState.Deleted;
            // }
            // else
            // {
            //     this.DbContext.$TablePropertyName$.Attach($entity$);
            //     this.DbContext.$TablePropertyName$.Remove($entity$);
            // }

            CodeArgumentReferenceExpression entityArgRef = new CodeArgumentReferenceExpression(parameterName);
            CodeExpression contextRef = LinqToEntitiesDbContext.GetContextReferenceExpression();

            CodeVariableDeclarationStatement entityEntryDeclaration = new CodeVariableDeclarationStatement(
                new CodeTypeReference("DbEntityEntry", new CodeTypeReference(entity.ClrType.Name)),
                "entityEntry",
                new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(contextRef, "Entry"), entityArgRef));

            method.Statements.Add(entityEntryDeclaration);

            CodeVariableReferenceExpression entityEntryRef   = new CodeVariableReferenceExpression("entityEntry");
            CodePropertyReferenceExpression entityStateRef   = new CodePropertyReferenceExpression(entityEntryRef, "State");
            CodeFieldReferenceExpression    detachedStateRef = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(EntityState).Name), Enum.GetName(typeof(EntityState), EntityState.Deleted));
            CodeExpression detachedStateTestExpr             = CodeGenUtilities.MakeNotEqual(typeof(EntityState), entityStateRef, detachedStateRef, codeGenContext.IsCSharp);

            CodeFieldReferenceExpression deletedStateRef          = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(EntityState).Name), Enum.GetName(typeof(EntityState), EntityState.Deleted));
            CodeAssignStatement          deletedStateExpr         = new CodeAssignStatement(entityStateRef, deletedStateRef);
            CodeMethodInvokeExpression   attachedEntityMethodCall = new CodeMethodInvokeExpression(LinqToEntitiesDbContext.GetDbSetReferenceExpression(efDbEntity), "Attach", entityArgRef);
            CodeMethodInvokeExpression   removedEntityMethodCall  = new CodeMethodInvokeExpression(LinqToEntitiesDbContext.GetDbSetReferenceExpression(efDbEntity), "Remove", entityArgRef);

            CodeConditionStatement changeStateOrAddStmt =
                new CodeConditionStatement(detachedStateTestExpr,
                                           new CodeStatement[] { deletedStateExpr },
                                           new CodeStatement[] { new CodeExpressionStatement(attachedEntityMethodCall), new CodeExpressionStatement(removedEntityMethodCall) });

            method.Statements.Add(changeStateOrAddStmt);
        }
        /// <summary>
        /// Generates the delete domain operation entry
        /// </summary>
        /// <param name="codeGenContext">The code gen context.></param>
        /// <param name="businessLogicClass">The business logic class.</param>
        /// <param name="entity">The entity.</param>
        protected override void GenerateDeleteMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
        {
            string parameterName = CodeGenUtilities.MakeLegalParameterName(entity.Name);

            // public void Delete$EntityName$($entityType$ $entityName$)
            CodeMemberMethod method = new CodeMemberMethod();

            businessLogicClass.Members.Add(method);

            LinqToSqlEntity ltsEntity = (LinqToSqlEntity)entity;

            method.Name       = "Delete" + CodeGenUtilities.MakeLegalEntityName(entity.Name);
            method.Attributes = MemberAttributes.Public | MemberAttributes.Final;   // final needed to prevent virtual

            // parameter declaration
            method.Parameters.Add(new CodeParameterDeclarationExpression(entity.ClrType.Name, parameterName));

            // this.DataContext.$TablePropertyName$.Attach(current)
            CodeExpression             contextRef = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DataContext");
            CodeExpression             tableRef   = new CodePropertyReferenceExpression(contextRef, ltsEntity.TablePropertyName);
            CodeMethodInvokeExpression attachCall = new CodeMethodInvokeExpression(tableRef, "Attach", new CodeArgumentReferenceExpression(parameterName));

            method.Statements.Add(attachCall);

            // this.DataContext.$TablePropertyName$.DeleteOnSubmit(current)
            CodeMethodInvokeExpression deleteCall = new CodeMethodInvokeExpression(tableRef, "DeleteOnSubmit", new CodeArgumentReferenceExpression(parameterName));

            method.Statements.Add(deleteCall);
        }
        /// <summary>
        /// Generates the select domain operation entry
        /// </summary>
        /// <param name="codeGenContext">The code gen context.></param>
        /// <param name="businessLogicClass">The business logic class.</param>
        /// <param name="entity">The entity.</param>
        /// <returns>The newly created method</returns>
        protected override CodeMemberMethod GenerateSelectMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
        {
            CodeMemberMethod method    = null;
            LinqToSqlEntity  ltsEntity = entity as LinqToSqlEntity;

            if (ltsEntity != null && ltsEntity.TablePropertyName != null)
            {
                // public IQueryable<$entityType$> GetEntities()
                method = new CodeMemberMethod();
                businessLogicClass.Members.Add(method);

                // Add developer comment explaining they can add additional parameters
                method.Comments.Add(new CodeCommentStatement(Resources.BusinessLogicClass_Query_Method_Remarks, false));

                method.Name       = "Get" + CodeGenUtilities.MakeLegalEntityName(ltsEntity.TablePropertyName);
                method.ReturnType = new CodeTypeReference("IQueryable", new CodeTypeReference(entity.Name));
                method.Attributes = MemberAttributes.Public | MemberAttributes.Final;   // final needed to prevent virtual

                // return this.DataContext.$TablePropertyName$
                CodeExpression            contextExpr = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DataContext");
                CodeExpression            expr        = new CodePropertyReferenceExpression(contextExpr, ltsEntity.TablePropertyName);
                CodeMethodReturnStatement returnStmt  = new CodeMethodReturnStatement(expr);
                method.Statements.Add(returnStmt);
            }
            return(method);
        }