Esempio n. 1
0
        public CodeTypeDeclaration CreateClass()
        {
            var codeTypeDeclaration = new CodeTypeDeclaration(_entityBaseClassName)
            {
                IsClass        = true,
                TypeAttributes = TypeAttributes.Public | TypeAttributes.Abstract
            };

            codeTypeDeclaration.BaseTypes.Add(typeof(INotifyPropertyChanged));
            codeTypeDeclaration.BaseTypes.Add(typeof(INotifyPropertyChanging));
            codeTypeDeclaration.BaseTypes.Add(typeof(IEntity));
            codeTypeDeclaration.BaseTypes.Add(_dataTypeDescriptor.GetFullInterfaceName());

            codeTypeDeclaration.Members.Add(new CodeMemberField(new CodeTypeReference(typeof(DataSourceId).FullName), EntityClassesFieldNames.DataSourceIdFieldName));
            codeTypeDeclaration.Members.Add(new CodeMemberField(new CodeTypeReference(typeof(DataScopeIdentifier).FullName), EntityClassesFieldNames.DataSourceIdScopeFieldName)
            {
                Attributes = MemberAttributes.Family
            });
            codeTypeDeclaration.Members.Add(new CodeMemberField(new CodeTypeReference(typeof(CultureInfo).FullName), EntityClassesFieldNames.DataSourceIdCultureFieldName)
            {
                Attributes = MemberAttributes.Family
            });

            AddConstructor(codeTypeDeclaration);
            AddIEntityImplementation(codeTypeDeclaration);
            AddIDataSourceProperty(codeTypeDeclaration);

            AddProperties(codeTypeDeclaration);

            EntityCodeGeneratorHelper.AddPropertyChanging(codeTypeDeclaration);
            EntityCodeGeneratorHelper.AddPropertyChanged(codeTypeDeclaration);

            return(codeTypeDeclaration);
        }
Esempio n. 2
0
        private void AddPropertiesAddProperty(CodeTypeDeclaration declaration, string propertyName, Type propertyType, string fieldName, string nullableFieldName)
        {
            SqlColumnInformation columnInformation = _dataTypeDescriptor.CreateSqlColumnInformation(propertyName);

            string name       = propertyName;
            Type   type       = propertyType;
            string dbName     = propertyName;
            string dbType     = EntityCodeGeneratorHelper.GetDbType(columnInformation.SqlDbType, columnInformation.IsNullable);
            bool   isNullable = columnInformation.IsNullable;
            bool   isId       = columnInformation.IsPrimaryKey;
            bool   isAutoGen  = columnInformation.IsIdentity;


            CodeMemberProperty propertyMember = new CodeMemberProperty();

            propertyMember.Name       = name;
            propertyMember.Type       = new CodeTypeReference(type);
            propertyMember.Attributes = MemberAttributes.Public;
            propertyMember.HasSet     = true;
            propertyMember.HasGet     = true;


            propertyMember.GetStatements.Add(
                new CodeConditionStatement(
                    new CodeBinaryOperatorExpression(
                        new CodeFieldReferenceExpression(
                            new CodeThisReferenceExpression(),
                            nullableFieldName
                            ),
                        CodeBinaryOperatorType.IdentityInequality,
                        new CodePrimitiveExpression(null)
                        ),
                    new CodeStatement[] {
                new CodeMethodReturnStatement(
                    new CodePropertyReferenceExpression(
                        new CodeFieldReferenceExpression(
                            new CodeThisReferenceExpression(),
                            nullableFieldName
                            ),
                        "Value"
                        )
                    )
            },
                    new CodeStatement[] {
                new CodeMethodReturnStatement(
                    new CodeFieldReferenceExpression(
                        new CodeThisReferenceExpression(),
                        fieldName
                        )
                    )
            }
                    )
                );



            propertyMember.SetStatements.Add(
                new CodeAssignStatement(
                    new CodePropertyReferenceExpression(
                        new CodeBaseReferenceExpression(),
                        name
                        ),
                    new CodeArgumentReferenceExpression("value")
                    )
                );



            propertyMember.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(DebuggerNonUserCodeAttribute))));

            var codeAttributeArguments = new List <CodeAttributeArgument>
            {
                new CodeAttributeArgument("Name", new CodePrimitiveExpression(dbName)),
                new CodeAttributeArgument("Storage", new CodePrimitiveExpression(fieldName)),
                new CodeAttributeArgument("DbType", new CodePrimitiveExpression(dbType)),
                new CodeAttributeArgument("CanBeNull", new CodePrimitiveExpression(isNullable)),
                new CodeAttributeArgument("IsPrimaryKey", new CodePrimitiveExpression(isId)),
                new CodeAttributeArgument("IsDbGenerated", new CodePrimitiveExpression(isAutoGen)),
                new CodeAttributeArgument("UpdateCheck",
                                          new CodeFieldReferenceExpression(
                                              new CodeTypeReferenceExpression(typeof(UpdateCheck)), nameof(UpdateCheck.Never))
                                          )
            };



            propertyMember.CustomAttributes.Add(new CodeAttributeDeclaration(
                                                    new CodeTypeReference(typeof(ColumnAttribute)),
                                                    codeAttributeArguments.ToArray()
                                                    ));


            declaration.Members.Add(propertyMember);
        }