Пример #1
0
        public void Attribute_evaluates_preconditions()
        {
            var code = new CSharpCodeHelper();

            var ex = Assert.Throws <ArgumentNullException>(() => code.Attribute(null));

            Assert.Equal("configuration", ex.ParamName);
        }
Пример #2
0
        public void Attribute_surrounds_body()
        {
            var code          = new CSharpCodeHelper();
            var configuration = new Mock <IAttributeConfiguration>();

            configuration.Setup(c => c.GetAttributeBody(code)).Returns("Required");

            Assert.Equal("[Required]", code.Attribute(configuration.Object));
        }
        /// <summary>
        /// Create the template output
        /// </summary>
        public virtual string TransformText()
        {
            var code = new CSharpCodeHelper();
            var edm  = new EdmHelper(code);

            if (EntitySet == null)
            {
                throw new ArgumentNullException("EntitySet");
            }

            if (Model == null)
            {
                throw new ArgumentNullException("Model");
            }

            var entityType = EntitySet.ElementType;

            this.Write("namespace ");
            this.Write(this.ToStringHelper.ToStringWithCulture(Namespace));
            this.Write("\r\n{\r\n    using System;\r\n    using System.Collections.Generic;\r\n    using System.C" +
                       "omponentModel.DataAnnotations;\r\n    using System.ComponentModel.DataAnnotations." +
                       "Schema;\r\n    using System.Data.Entity.Spatial;\r\n\r\n");

            var typeConfigurations = edm.GetConfigurations(EntitySet, Model).OfType <IAttributeConfiguration>();

            foreach (var typeConfiguration in typeConfigurations)
            {
                this.Write("    ");
                this.Write(this.ToStringHelper.ToStringWithCulture(code.Attribute(typeConfiguration)));
                this.Write("\r\n");
            }

            this.Write("    public partial class ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entityType)));
            this.Write("\r\n    {\r\n");

            var collectionProperties = from p in entityType.NavigationProperties
                                       where p.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many
                                       select p;

            if (collectionProperties.Any())
            {
                this.Write("        public ");
                this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entityType)));
                this.Write("()\r\n        {\r\n");

                foreach (var collectionProperty in collectionProperties)
                {
                    this.Write("            ");
                    this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(collectionProperty)));
                    this.Write(" = new HashSet<");
                    this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(collectionProperty.ToEndMember.GetEntityType())));
                    this.Write(">();\r\n");
                }

                this.Write("        }\r\n\r\n");
            }

            var first = true;

            foreach (var property in entityType.Properties)
            {
                if (!first)
                {
                    WriteLine(string.Empty);
                }
                else
                {
                    first = false;
                }

                var propertyConfigurations = edm.GetConfigurations(property, Model).OfType <IAttributeConfiguration>();

                foreach (var propertyConfiguration in propertyConfigurations)
                {
                    this.Write("        ");
                    this.Write(this.ToStringHelper.ToStringWithCulture(code.Attribute(propertyConfiguration)));
                    this.Write("\r\n");
                }

                this.Write("        public ");
                this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(property)));
                this.Write(" ");
                this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(property)));
                this.Write(" { get; set; }\r\n");
            }

            foreach (var navigationProperty in entityType.NavigationProperties)
            {
                if (!first)
                {
                    WriteLine(string.Empty);
                }
                else
                {
                    first = false;
                }


                this.Write("        public virtual ");
                this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(navigationProperty)));
                this.Write(" ");
                this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(navigationProperty)));
                this.Write(" { get; set; }\r\n");
            }

            this.Write("    }\r\n}\r\n");
            return(this.GenerationEnvironment.ToString());
        }
        /// <summary>
        /// Create the template output
        /// </summary>
        public virtual string TransformText()
        {

    var code = new CSharpCodeHelper();
    var edm = new EdmHelper(code);

    if (EntitySet == null)
    {
        throw new ArgumentNullException("EntitySet");
    }

    if (Model == null)
    {
        throw new ArgumentNullException("Model");
    }

    var entityType = EntitySet.ElementType;

            this.Write("namespace ");
            this.Write(this.ToStringHelper.ToStringWithCulture(Namespace));
            this.Write("\r\n{\r\n    using System;\r\n    using System.Collections.Generic;\r\n    using System.C" +
                    "omponentModel.DataAnnotations;\r\n    using System.ComponentModel.DataAnnotations." +
                    "Schema;\r\n    using System.Data.Entity.Spatial;\r\n\r\n");

    var typeConfigurations = edm.GetConfigurations(EntitySet, Model).OfType<IAttributeConfiguration>();

    foreach (var typeConfiguration in typeConfigurations)
    {

            this.Write("    ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Attribute(typeConfiguration)));
            this.Write("\r\n");

    }

            this.Write("    public partial class ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entityType)));
            this.Write("\r\n    {\r\n");

    var collectionProperties = from p in entityType.NavigationProperties
                               where p.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many
                               select p;

    if (collectionProperties.Any())
    {

            this.Write("        [System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Usage\", \"CA22" +
                    "14:DoNotCallOverridableMethodsInConstructors\")]\r\n        public ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(entityType)));
            this.Write("()\r\n        {\r\n");

    foreach (var collectionProperty in collectionProperties)
    {

            this.Write("            ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(collectionProperty)));
            this.Write(" = new HashSet<");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(collectionProperty.ToEndMember.GetEntityType())));
            this.Write(">();\r\n");

    }

            this.Write("        }\r\n\r\n");

    }

    var first = true;

    foreach (var property in entityType.Properties)
    {
        if (!first)
        {
            WriteLine(string.Empty);
        }
        else
        {
            first = false;
        }

        var propertyConfigurations = edm.GetConfigurations(property, Model).OfType<IAttributeConfiguration>();

        foreach (var propertyConfiguration in propertyConfigurations)
        {

            this.Write("        ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Attribute(propertyConfiguration)));
            this.Write("\r\n");

        }

            this.Write("        public ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(property)));
            this.Write(" ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(property)));
            this.Write(" { get; set; }\r\n");

    }

    foreach (var navigationProperty in entityType.NavigationProperties)
    {
        if (!first)
        {
            WriteLine(string.Empty);
        }
        else
        {
            first = false;
        }

            if (navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {

            this.Write("        [System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Usage\", \"CA22" +
                    "27:CollectionPropertiesShouldBeReadOnly\")]\r\n");

            }

            this.Write("        public virtual ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Type(navigationProperty)));
            this.Write(" ");
            this.Write(this.ToStringHelper.ToStringWithCulture(code.Property(navigationProperty)));
            this.Write(" { get; set; }\r\n");

    }

            this.Write("    }\r\n}\r\n");
            return this.GenerationEnvironment.ToString();
        }
        public void Attribute_surrounds_body()
        {
            var code = new CSharpCodeHelper();
            var configuration = new Mock<IAttributeConfiguration>();
            configuration.Setup(c => c.GetAttributeBody(code)).Returns("Required");

            Assert.Equal("[Required]", code.Attribute(configuration.Object));
        }
        public void Attribute_evaluates_preconditions()
        {
            var code = new CSharpCodeHelper();

            var ex = Assert.Throws<ArgumentNullException>(() => code.Attribute(null));
            Assert.Equal("configuration", ex.ParamName);
        }