コード例 #1
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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity entity)
 {
 }
コード例 #2
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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity 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);
            }
        }
コード例 #3
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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity entity)
 {
     return(null);
 }
コード例 #4
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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity entity)
 {
     if (typeof(EntityObject).IsAssignableFrom(entity.ClrType))
     {
         return(false);
     }
     BusinessLogicContext.GenerateHelperMemberIfNecessary(codeGenContext, businessLogicClass, LinqToEntitiesContext.GetEntityStateHelperMethodName, () =>
     {
         return(LinqToEntitiesContext.GenerateGetEntityState(codeGenContext, businessLogicClass));
     });
     return(true);
 }
コード例 #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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity entity)
 {
 }
コード例 #6
0
        /// <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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity 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);
        }
コード例 #7
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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity 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.DbContext.$ObjectSetName$.AttachAsModified($current$, this.ChangeSet.GetOriginal(current), this.DbContext);
                CodeExpression contextRef = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DbContext");
                CodePropertyReferenceExpression objectSetRef = new CodePropertyReferenceExpression(contextRef, efEntity.DefaultObjectSetName);
                CodeMethodInvokeExpression      attachCall   = new CodeMethodInvokeExpression(objectSetRef, "AttachAsModified",
                                                                                              new CodeArgumentReferenceExpression(currentParameterName), changeSetGetOrig, new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DbContext"));
                CodeExpressionStatement attachStmt = new CodeExpressionStatement(attachCall);
                method.Statements.Add(attachStmt);
            }
            else
            {
                // this.DbContext.$ObjectSetName$.AttachAsModified($current$, this.DbContext);
                CodeExpression contextRef = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DbContext");
                CodePropertyReferenceExpression objectSetRef = new CodePropertyReferenceExpression(contextRef, efEntity.DefaultObjectSetName);
                CodeMethodInvokeExpression      attachCall   = new CodeMethodInvokeExpression(objectSetRef, "AttachAsModified",
                                                                                              new CodeArgumentReferenceExpression(currentParameterName), new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DbContext"));
                CodeExpressionStatement attachStmt = new CodeExpressionStatement(attachCall);
                method.Statements.Add(attachStmt);
            }
        }
コード例 #8
0
        /// <summary>
        /// Generates the select domain operation entry
        /// </summary>
        /// <param name="codeGenContext">The code generation 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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity entity)
        {
            CodeMemberMethod method = null;
            LinqToEntitiesEntity efDbEntity = entity as LinqToEntitiesEntity;

            if (efDbEntity != null && efDbEntity.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, efDbEntity.DefaultObjectSetName);
                method.Comments.Add(new CodeCommentStatement(queryComment, false));

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

                // return this.DbContext.$TablePropertyName$
                CodeExpression contextExpr = LinqToEntitiesDbContext.GetContextReferenceExpression();
                CodeExpression expr = new CodePropertyReferenceExpression(contextExpr, efDbEntity.DefaultObjectSetName);
                CodeMethodReturnStatement returnStmt = new CodeMethodReturnStatement(expr);
                method.Statements.Add(returnStmt);
            }
            return method;
        }
コード例 #9
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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity 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.DbContext.$ObjectSetName$.AttachAsModified($current$, this.ChangeSet.GetOriginal(current), this.DbContext);
                CodeExpression contextRef = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DbContext");
                CodePropertyReferenceExpression objectSetRef = new CodePropertyReferenceExpression(contextRef, efEntity.DefaultObjectSetName);
                CodeMethodInvokeExpression attachCall = new CodeMethodInvokeExpression(objectSetRef, "AttachAsModified", 
                    new CodeArgumentReferenceExpression(currentParameterName), changeSetGetOrig, new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DbContext"));
                CodeExpressionStatement attachStmt = new CodeExpressionStatement(attachCall);
                method.Statements.Add(attachStmt);
            }
            else
            {
                // this.DbContext.$ObjectSetName$.AttachAsModified($current$, this.DbContext);
                CodeExpression contextRef = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DbContext");
                CodePropertyReferenceExpression objectSetRef = new CodePropertyReferenceExpression(contextRef, efEntity.DefaultObjectSetName);
                CodeMethodInvokeExpression attachCall = new CodeMethodInvokeExpression(objectSetRef, "AttachAsModified",
                    new CodeArgumentReferenceExpression(currentParameterName), new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DbContext"));
                CodeExpressionStatement attachStmt = new CodeExpressionStatement(attachCall);
                method.Statements.Add(attachStmt);
            }
        }
コード例 #10
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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity 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);
        }        
コード例 #11
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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity entity)
 {
     if (typeof(EntityObject).IsAssignableFrom(entity.ClrType))
     {
         return false;
     }
     BusinessLogicContext.GenerateHelperMemberIfNecessary(codeGenContext, businessLogicClass, LinqToEntitiesContext.GetEntityStateHelperMethodName, () =>
     {
         return LinqToEntitiesContext.GenerateGetEntityState(codeGenContext, businessLogicClass);
     });
     return true;
 }
コード例 #12
0
        /// <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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity 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);
        }
コード例 #13
0
        /// <summary>
        /// Generates the update 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 GenerateUpdateMethod(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity entity)
        {
            string currentParameterName = "current" + entity.ClrType.Name;

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

            businessLogicClass.Members.Add(method);

            LinqToSqlEntity ltsEntity = (LinqToSqlEntity)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));

            if (!ltsEntity.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.DataContext.$TablePropertyName$.Attach(current, this.ChangeSet.GetOriginal(current))
                CodeExpression             contextRef = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DataContext");
                CodeExpression             tableRef   = new CodePropertyReferenceExpression(contextRef, ltsEntity.TablePropertyName);
                CodeMethodInvokeExpression attachCall = new CodeMethodInvokeExpression(tableRef, "Attach", new CodeArgumentReferenceExpression(currentParameterName), changeSetGetOrig);
                method.Statements.Add(attachCall);
            }
            else
            {
                // this.DataContext.$TablePropertyName$.Attach(current, true)
                CodeExpression             contextRef = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DataContext");
                CodeExpression             tableRef   = new CodePropertyReferenceExpression(contextRef, ltsEntity.TablePropertyName);
                CodeMethodInvokeExpression attachCall = new CodeMethodInvokeExpression(tableRef, "Attach", new CodeArgumentReferenceExpression(currentParameterName), new CodePrimitiveExpression(true));
                method.Statements.Add(attachCall);
            }
        }
コード例 #14
0
        /// <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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity 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);
        }
コード例 #15
0
        /// <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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity 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;
        }
コード例 #16
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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity 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);
        }        
コード例 #17
0
        /// <summary>
        /// Generates the update 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 GenerateUpdateMethod(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity entity)
        {
            string currentParameterName = "current" + entity.ClrType.Name;

            // public void Update$EntityName$($entityType$ current)
            CodeMemberMethod method = new CodeMemberMethod();
            businessLogicClass.Members.Add(method);

            LinqToSqlEntity ltsEntity = (LinqToSqlEntity)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));

            if (!ltsEntity.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.DataContext.$TablePropertyName$.Attach(current, this.ChangeSet.GetOriginal(current))
                CodeExpression contextRef = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DataContext");
                CodeExpression tableRef = new CodePropertyReferenceExpression(contextRef, ltsEntity.TablePropertyName);
                CodeMethodInvokeExpression attachCall = new CodeMethodInvokeExpression(tableRef, "Attach", new CodeArgumentReferenceExpression(currentParameterName), changeSetGetOrig);
                method.Statements.Add(attachCall);
            }
            else
            {
                // this.DataContext.$TablePropertyName$.Attach(current, true)
                CodeExpression contextRef = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DataContext");
                CodeExpression tableRef = new CodePropertyReferenceExpression(contextRef, ltsEntity.TablePropertyName);
                CodeMethodInvokeExpression attachCall = new CodeMethodInvokeExpression(tableRef, "Attach", new CodeArgumentReferenceExpression(currentParameterName), new CodePrimitiveExpression(true));
                method.Statements.Add(attachCall);
            }
        }
コード例 #18
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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity 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);
            }
        }
コード例 #19
0
        /// <summary>
        /// Generates the select domain operation entry
        /// </summary>
        /// <param name="codeGenContext">The code generation 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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity entity)
        {
            CodeMemberMethod     method     = null;
            LinqToEntitiesEntity efDbEntity = entity as LinqToEntitiesEntity;

            if (efDbEntity != null && efDbEntity.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, efDbEntity.DefaultObjectSetName);
                method.Comments.Add(new CodeCommentStatement(queryComment, false));

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

                // return this.DbContext.$TablePropertyName$
                CodeExpression            contextExpr = LinqToEntitiesDbContext.GetContextReferenceExpression();
                CodeExpression            expr        = new CodePropertyReferenceExpression(contextExpr, efDbEntity.DefaultObjectSetName);
                CodeMethodReturnStatement returnStmt  = new CodeMethodReturnStatement(expr);
                method.Statements.Add(returnStmt);
            }
            return(method);
        }
コード例 #20
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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity entity)
 {
     return null;
 }
コード例 #21
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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity 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);
        }
コード例 #22
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(ICodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, IBusinessLogicEntity 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);
        }