/// <summary>
 /// Creates a plain ol' property representing any column that is not a foreign key or primary key.
 /// </summary>
 /// <param name="classDeclaration">The class to which the property is added.</param>
 /// <param name="column">The column the property represents.</param>
 protected override void CreateNonKeyProperty(ClassDeclaration classDeclaration, ColumnSchema column)
 {
     string propertyName = this.NameProvider.GetPropertyName(column);
     string fieldName = this.NameProvider.GetFieldName(column);
     classDeclaration.AddProperty(propertyName, fieldName, column.DataType, column.Nullable)
             .AddAttribute("Castle.ActiveRecord.PropertyAttribute")
             .AddQuotedArgument(column.Name);
 }
        /// <summary>
        /// Gets a value that can be assigned to the target column.
        /// </summary>
        /// <param name="column">The column for which the value is generated.</param>
        /// <returns>A value that can be assigned to a property representing the column.</returns>
        public string GetValue(ColumnSchema column)
        {
            if (column == null)
                throw new ArgumentNullException("column");

            string result = null;
            switch (column.DataType.FullName)
            {
                case "System.String":
                    int length = column.Length > 1000 || column.Length < 0 ? 1000 : column.Length;
                    result = String.Format("\"{0}\"", new String('Z', length));
                    break;
                case "System.DateTime":
                    result = "new DateTime(1970, 6, 18)";
                    break;
                case "System.Boolean":
                    result = "true";
                    break;
                case "System.Decimal":
                    result = "666M";
                    break;
                case "System.Int32":
                case "System.Int64":
                    result = "123";
                    break;
                case "System.Byte[]":
                    result = "System.Text.ASCIIEncoding.ASCII.GetBytes(\"GodHatesUsAll\")";
                    break;
                case "System.Guid":
                    result = "System.Guid.NewGuid()";
                    break;
                default:
                    throw new InvalidOperationException(String.Format("Unable to generate a value for type of '{0}'", column.DataType.FullName));
            }
            return result;
        }
 /// <summary>
 /// Creates a plain ol' property representing any column that is not a foreign key or primary key.
 /// </summary>
 /// <param name="classDeclaration">The class to which the property is added.</param>
 /// <param name="column">The column the property represents.</param>
 protected override void CreateNonKeyProperty(ClassDeclaration classDeclaration, ColumnSchema column)
 {
     string propertyName = this.NameProvider.GetPropertyName(column);
     string fieldName = this.NameProvider.GetFieldName(column);
     AttributeDeclaration attrib = classDeclaration.AddProperty(propertyName, fieldName, column.DataType, column.Nullable)
             .AddAttribute("Castle.ActiveRecord.PropertyAttribute")
             .AddQuotedArgument(column.Name);
     if (column.SqlType == System.Data.SqlDbType.Image)
         attrib.AddArgument("ColumnType=\"BinaryBlob\"");
     if (column.SqlType == System.Data.SqlDbType.Xml)
         attrib.AddArgument("ColumnType=\"StringClob\"");
 }
 /// <summary>
 /// Creates a plain ol' property representing any column that is not a foreign key or primary key.
 /// </summary>
 /// <param name="classDeclaration">The class to which the property is added.</param>
 /// <param name="column">The column the property represents.</param>
 protected abstract void CreateNonKeyProperty(ClassDeclaration classDeclaration, ColumnSchema column);
        protected override void CreateNonKeyProperty(ClassDeclaration classDeclaration, ColumnSchema column)
        {
            string fieldName = this.NameProvider.GetFieldName(column.Name);
            var columnDataType = new CodeTypeReference(column.DataType);
            if (column.Nullable && column.DataType.IsValueType)
            {
                columnDataType = new CodeTypeReference(typeof(System.Nullable<>));
                columnDataType.TypeArguments.Add(new CodeTypeReference(column.DataType));
            }

            var field = new CodeMemberField(columnDataType, fieldName);

            string propertyName = this.NameProvider.GetPropertyName(column.Name);
            var prop = new CodeMemberProperty();
            prop.Name = propertyName;
            prop.Attributes = MemberAttributes.Final | MemberAttributes.Public;
            prop.Type = columnDataType;

            prop.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fieldName)));
            prop.SetStatements.Add(
                new CodeConditionStatement(
                    new CodeBinaryOperatorExpression(
                        new CodeSnippetExpression("value"),
                        CodeBinaryOperatorType.IdentityInequality,
                        new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fieldName)),
                    new CodeSnippetStatement(String.Format("{1}((EightyProofSolutions.BlogEngine.Core.Models.ITrackPropertyChanges)this).OnPropertyChanged(\"{0}\", this.{2}, value);", propertyName, new String('\t', 5), fieldName)),
                    new CodeAssignStatement(
                        new CodeFieldReferenceExpression(
                            new CodeThisReferenceExpression(), fieldName),
                            new CodeSnippetExpression("value"))));

            classDeclaration.Members.Add(field);
            classDeclaration.Members.Add(prop);
            //classDeclaration.AddProperty(propertyName, fieldName, column.DataType, column.Nullable);
        }
        /// <summary>
        /// Creates a plain ol' property representing any column that is not a foreign key or primary key.
        /// </summary>
        /// <param name="classDeclaration">The class to which the property is added.</param>
        /// <param name="column">The column the property represents.</param>
        protected override void CreateNonKeyProperty(ClassDeclaration classDeclaration, ColumnSchema column)
        {
            string propertyName = this.NameProvider.GetPropertyName(column);
            string fieldName = this.NameProvider.GetFieldName(column);
            PropertyDeclaration prop = classDeclaration.AddProperty(propertyName, fieldName, column.DataType, column.Nullable);

            if (this.ConfigOptions.GenerateComments)
            {
                prop.AddComment("Gets or sets the {0} of the {1}.", this.NameProvider.GetPropertyName(column), classDeclaration.Name);
                if (column.Nullable)
                    prop.AddComment("This property is nullable.");
                else
                    prop.AddComment("This property cannot be null.");
                if (column.DataType == typeof(string))
                    prop.AddComment("The max length of this property is {0}.", column.Length);
            }

            AttributeDeclaration propAttrib = prop.AddAttribute("Castle.ActiveRecord.PropertyAttribute")
                    .AddQuotedArgument(column.Name)
                    .AddArgument("Length=" + (column.Length < 0 ? 0 : column.Length).ToString())
                    .AddArgument("NotNull=" + (column.Nullable == false).ToString().ToLower());

            if (column.SqlType == System.Data.SqlDbType.Image)
                propAttrib.AddArgument("ColumnType=\"BinaryBlob\"");

            if (column.SqlType == System.Data.SqlDbType.Xml)
                propAttrib.AddArgument("ColumnType=\"StringClob\"");
        }
Example #7
0
 /// <summary>
 /// Gets a property name derived from a column.  Override GetPropertyName(string) to customize.
 /// </summary>
 /// <param name="column">The column the property from which the name of the property is derived.</param>
 /// <returns>The name of the property based on the column.</returns>
 public string GetPropertyName(ColumnSchema column)
 {
     return this.GetPropertyName(column.Name);
 }
Example #8
0
 /// <summary>
 /// Gets a field name derived from the column name.  Override GetFieldName(string) to customize.
 /// </summary>
 /// <param name="column">The column from which field name is derived.</param>
 /// <returns>The name of the field derived from the column.</returns>
 public string GetFieldName(ColumnSchema column)
 {
     return this.GetFieldName(column.Name);
 }
Example #9
0
 /// <summary>
 /// Returns a name formatted in camelcase for use as an argument. For example, changes FirstName to firstName.
 /// </summary>
 /// <param name="column">The column to format.</param>
 /// <returns>The name formatted as an argument.</returns>
 public virtual string GetArgumentName(ColumnSchema column)
 {
     return this.GetArgumentName(column.Name);
 }