views:

491

answers:

4

Hi, Could some one help, how would I instruct automap to have not-null for a cloumn?

public class Paper : Entity
{
    public Paper() { }

            [DomainSignature]
            [NotNull, NotEmpty]
            public virtual string ReferenceNumber { get; set; }

            [NotNull]
            public virtual Int32 SessionWeek { get; set; }
}

But I am getting the following:

 <column name="SessionWeek"/>

I know it can be done using fluent-map. but i would like to know it in auto-mapping way. Many thanks.

Regards Robie

A: 
public class Paper Map : IAutoMappingOverride<Paper >
{
    public void Override(AutoMapping<Paper> mapping)
    {
        mapping.Map(x => x.ReferenceNumber).Not.Nullable();
    }
}

Int32 is not nullable type by default. Int32? is nullable, so you make it non-nullable just by specifying it as Int32.

You can use conventions to do this automatically. I am not sure which convention to use, but have a look at FluentNHibernate.Conventions.Instances to find the right one. It'll look like this.

public class ColumnConvention : IColumnConvention
{
    public void Apply(FluentNHibernate.Conventions.Instances.ColumnInstance instance)
    {
        if (instance.EntityType.IsDefined(typeof(NotNullAttribute), false))
            instance.NotNull = true;
    }

    public void Apply(FluentNHibernate.Conventions.Instances.IColumnInstance instance)
    {
        return;
    }
}

Just add this convention to your automapping.

HeavyWave
Thanks, But what if I have many properties in many entities in my application that is not null. Do I override them all? It does not sound right to me though.
Robie
nHibernate Validator is a separate thing. You will have to check for the attribute in your automapper.
HeavyWave
Many thanks for your reply.OK. So, it does mean that there is no AutoMapping convention to instruct Fluent Nhibernate to specify a field to be not null other then overriding all the properties of my application. This indactes to me that AutoMap is lacking a very basic feature. If I need to ovverride each class map as alomost all of them have more or less "not-null" fields then why should someone use automapping over fluent-mapping. Please share your thoughts.
Robie
No, it's not lacking a feature, because NHibernate Validator is NOT a part of NHibernate, it's an extension. You don't have to write it every time. I have updated my answer to show you what you can do.
HeavyWave
(instance.EntityType.IsDefined(typeof(NotNullAttribute), false)this is always returning false resulting the convention not being applyed. I am not sure how else to detect the presense of the attribute. I have also tried this with PropertConvesntion and its the same isssue.Thanks for your contunius support though.
Robie
Well, duh, the attribute is on the properties, not on the class -_- sorry
HeavyWave
+2  A: 

Thank you. Also, for reference properties ReferenceConvention need to be done. This is the code that works:

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

}  public class ReferenceConvention : IReferenceConvention
{
    public void Apply(FluentNHibernate.Conventions.Instances.IManyToOneInstance instance)
    {
        instance.Column(instance.Property.Name + "Fk");


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

    }
}
Robie
A: 

Hi Robie

Here is the way I do it, basically taken from the link you see in the code. There are some other useful conventions there as well

HTH,
Berryl

/// <summary>
/// If nullability for the column has not been specified explicitly to allow NULL, then set to “NOT NULL”.
/// </summary>
/// <remarks>see http://marcinobel.com/index.php/fluent-nhibernate-conventions-examples/&lt;/remarks&gt;
public class ColumnNullabilityConvention : IPropertyConvention, IPropertyConventionAcceptance
{
    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(x => x.Nullable, Is.Not.Set);
    }

    public void Apply(IPropertyInstance instance)
    {
        instance.Not.Nullable();
    }
}
Berryl