/// <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); }
/// <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); }