How to create tests for Conventions

14 views
Skip to first unread message

Riderman Sousa

unread,
May 21, 2013, 9:46:39 AM5/21/13
to Fluent NHibernate Group
Using DataAnnotations, I set my string properties with the length using StringLength attribute

[StringLength(10)]
public virtual string Identificacao { get; set; }

For this convection is applied to my database, I created a StringLengthConventions see: http://chopapp.com/#jlu7vz5v

Now I need to configure the properties as NotNull then follow the same steps:

1. I added the attribute [Required] on properties.
2. Create a RequiredConvention attribute, see: http://chopapp.com/#4milknwk

Error

As you may have noticed, the conventions retrieve only the first attribute. What generates an error in the generated schema.
The schema will follow the convention only the first attribute!

Question

The first step to fix the error is to create a test that fails. The question is, how to create a test for conventions?

Thanks,

Riderman de Sousa Barbosa

Web Developer | MCPD Certify

Skype.: 4042-6002 | Cel.: (31) 8681-1986

bindsolution.com

Microsoft Parner Network

Carl Bussema

unread,
May 21, 2013, 9:57:33 AM5/21/13
to fluent-n...@googlegroups.com
using System.ComponentModel.DataAnnotations;
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;

namespace YourApp.Infrastructure.Conventions
{
    public class ColumnNullConvention : IPropertyConvention
    {
        public void Apply(IPropertyInstance instance)
        {
            if (instance.Property.MemberInfo.IsDefined(typeof(RequiredAttribute), false))
                instance.Not.Nullable();
        }

    }
}

----

using System.ComponentModel.DataAnnotations;
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;

namespace YourApp.Infrastructure.Conventions
{
    public class ColumnLengthConvention : AttributePropertyConvention<StringLengthAttribute>
    {
        protected override void Apply(StringLengthAttribute attribute, IPropertyInstance instance)
        {
            // override the default column length
            if (attribute.MaximumLength != default(int)) instance.Length(attribute.MaximumLength);
        }
    }
}


----------------

Make sure when you generate your mappings, you're actually adding all your conventions, something like

mappings.Conventions.Setup(GetConventions());

   private static Action<IConventionFinder> GetConventions()
        {
            return c =>
                   {
  c.AddFromAssemblyOf<ColumnNullConvention>();
                   };
        }

--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fluent-nhibern...@googlegroups.com.
To post to this group, send email to fluent-n...@googlegroups.com.
Visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Riderman Sousa

unread,
May 21, 2013, 10:35:34 AM5/21/13
to Fluent NHibernate Group
Very nice, did not know the property AttributePropertyConvention. Why you not used in ColumnNullConvention? The same will apply to reference types?

Another question, in ColumnLengthConvention the default is 4001 (this way understands that the SQL type is varchar (max)). So even for those properties that do not have the attribute StringLengthAttribute set, the value should be 4001.  How can I do this?



My mapping is like this:

m.AutoMappings.Add(
AutoMap.AssemblyOf<Entidade>(mapConfig)
.UseOverridesFromAssemblyOf<AudityConvention>()
.Conventions.AddFromAssemblyOf<EnumConvention>()
);


Thanks.

Carl Bussema

unread,
May 21, 2013, 10:41:34 AM5/21/13
to fluent-n...@googlegroups.com
if (attribute.MaximumLength != default(int)) instance.Length(attribute.MaximumLength);
else instance.Length(4001); //force MS-SQL to varchar(MAX)

I have a separate reference convention where I also set the Foreign Key name:

 public class ReferenceConvention : IReferenceConvention
    {
        public void Apply(FNH.Conventions.Instances.IManyToOneInstance instance)
        {
       

            if (instance.Property.MemberInfo.IsDefined(typeof(RequiredAttribute), false))
                instance.Not.Nullable();

instance.ForeignKey(string.Format("FK_{0}_{1}",
instance.Name));

        }
    }

Riderman Sousa

unread,
May 21, 2013, 12:48:22 PM5/21/13
to Fluent NHibernate Group
One problem I'm seeing is that AttributePropertyConvention only applies to properties that have the attribute set. 
So for string properties that do not have the attribute, varchar(max) is not set!
How to configure convention for all string properties.





Reply all
Reply to author
Forward
0 new messages