/// <summary>
        /// Initializes a new instance of the AttributeDeclaration class.
        /// </summary>
        /// <param name="typeReference"></param>
        public AttributeDeclaration(CodeDomTypeReference typeReference)
        {
            if (typeReference == null)
                throw new ArgumentNullException("typeReference");

            _typeReference = typeReference;
        }
 public void TestDifferentTypes()
 {
     CodeDomTypeReference intref = new CodeDomTypeReference(typeof(int));
     Assert.IsTrue(intref.IsTypeOf("System.Int32"));
     Assert.AreEqual("System.Int32", intref.ToString());
     CodeDomTypeReference personref = new CodeDomTypeReference(typeof(Person));
     Assert.IsTrue(personref.IsTypeOf("HyperActive.Dominator.Tests.Person"));
     CodeDomTypeReference personlistref = new CodeDomTypeReference("System.Collections.Generic.List", "HyperActive.Dominator.Tests.Person");
     Assert.AreEqual("System.Collections.Generic.List`1[HyperActive.Dominator.Tests.Person]", personlistref.ToString());
     Assert.IsTrue(personlistref.ContainsTypeParameter("HyperActive.Dominator.Tests.Person"));
     Assert.IsTrue(personlistref.IsTypeOf("System.Collections.Generic.List`1[HyperActive.Dominator.Tests.Person]"));
     CodeDomTypeReference dictref = new CodeDomTypeReference("System.Collections.Generic.Dictionary").AddTypeParameters("int", "Person");
     Assert.AreEqual("System.Collections.Generic.Dictionary`2[int,Person]", dictref.ToString());
 }
 public void VerifyMostOfFunctionality()
 {
     CodeDomTypeReference typeReference = new CodeDomTypeReference("Castle.ActiveRecord.HasAndBelongsToManyAttribute");
     AttributeDeclaration attrdecl = new AttributeDeclaration(typeReference);
     Assert.AreEqual("Castle.ActiveRecord.HasAndBelongsToManyAttribute", attrdecl.TypeReference.ToString());
     attrdecl.AddArgument("typeof(Snippet)");
     attrdecl.AddQuotedArgument("column_name");
     attrdecl.AddQuotedArgument("Table", "my_table_name");
     Assert.AreEqual(3, attrdecl.Arguments.Count);
     Assert.AreEqual("typeof(Snippet)", attrdecl.Arguments[0]);
     Assert.AreEqual("\"column_name\"", attrdecl.Arguments[1]);
     Assert.AreEqual("Table=\"my_table_name\"", attrdecl.Arguments[2]);
     CodeAttributeDeclaration codedomattr = attrdecl.ToCodeDom();
     Assert.AreEqual(3, codedomattr.Arguments.Count);
     Assert.AreEqual("Castle.ActiveRecord.HasAndBelongsToManyAttribute", codedomattr.AttributeType.BaseType);
 }
        /// <summary>
        /// Creates a property that has many children that point to the primary key contained within this class.  Typically these will have HasMany attributes.
        /// </summary>
        /// <param name="classDeclaration">The class to which the property is added.</param>
        /// <param name="foreignKey">The foreign key in the table that references the primary key in this class.</param>
        /// <returns>The property that was added.</returns>
        protected override PropertyDeclaration CreateForeignKeyReference(ClassDeclaration classDeclaration, ForeignKeyColumnSchema foreignKey)
        {
            if (foreignKey.Table.PrimaryKey == null)
                return null;

            string propertyName = this.NameProvider.GetListPropertyName(foreignKey);
            string fieldName = this.NameProvider.GetListFieldName(foreignKey);
            string typeParam = this.NameProvider.GetClassName(foreignKey.Table);
            CodeDomTypeReference genericList = new CodeDomTypeReference("System.Collections.Generic.List").AddTypeParameters(typeParam);
            PropertyDeclaration result = classDeclaration.AddProperty(propertyName, fieldName, "System.Collections.Generic.IList");
            result.InitializeField(genericList)
                .AddTypeParameter(typeParam)
                .AddAttribute("Castle.ActiveRecord.HasManyAttribute")
                .AddArgument("typeof(" + this.NameProvider.GetClassName(foreignKey.Table) + ")");

            return result;
        }
        protected override void CreateAssociationProperties(ClassDeclaration classDeclaration, TableSchema table)
        {
            if (classDeclaration == null || table == null || table.Associations.Count == 0)
                return;

            foreach (var assocTable in table.Associations)
            {
                Console.WriteLine("assocTable.Name=" + assocTable.Name);
                var otherTable = assocTable.ForeignKeys.Single(fk => { return fk.PrimaryKeyTable.Name != table.Name; }).PrimaryKeyTable;

                string propName = Inflector.Pluralize(this.NameProvider.GetPropertyName(otherTable.Name));
                string fieldName = Inflector.Pluralize(this.NameProvider.GetFieldName(otherTable.Name));
                var typeRef = new CodeDomTypeReference(typeof(IList<>).FullName, this.NameProvider.GetClassName(otherTable));
                var prop = classDeclaration.AddProperty(propName, fieldName, typeRef);
                prop.AddAttribute("Castle.ActiveRecord.HasAndBelongsToMany")
                    .AddArgument("typeof(" + this.NameProvider.GetClassName(otherTable) + ")")
                    .AddQuotedArgument("Table", assocTable.Name)
                    .AddQuotedArgument("ColumnKey", assocTable.ForeignKeys.Single(fk => { return fk.PrimaryKeyTable.Name == table.Name; }).Name)
                    .AddQuotedArgument("ColumnRef", assocTable.ForeignKeys.Single(fk => { return fk.PrimaryKeyTable.Name != table.Name; }).Name)
                    .AddArgument("Lazy = true");

            }
        }
        protected override PropertyDeclaration CreateForeignKeyReference(ClassDeclaration classDeclaration, ForeignKeyColumnSchema foreignKey)
        {
            if (foreignKey.Table.PrimaryKey == null)
                return null;

            string propertyName = this.NameProvider.GetListPropertyName(foreignKey);
            string fieldName = this.NameProvider.GetListFieldName(foreignKey);
            string typeParam = this.NameProvider.GetClassName(foreignKey.Table);
            CodeDomTypeReference genericList = new CodeDomTypeReference("System.Collections.Generic.List").AddTypeParameters(typeParam);

            //create the field
            classDeclaration.Members.Add(new CodeMemberField(genericList.ToCodeDom(), fieldName));

            //create the prop
            var prop = new CodeMemberProperty();
            prop.Name = propertyName;
            prop.Type = genericList.ToCodeDom();
            prop.Attributes = MemberAttributes.Final | MemberAttributes.Public;
            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(prop);
            return null;
        }