I've recreated the problem with nested components that was previously
posted on this list. When integrating NHibernate Validator it throws a
NullReferenceException:

System.NullReferenceException: Object reference not set to an instance
of an object.
at
NHibernate.Validator.Event.ValidatePreInsertEventListener.SubElementsInspector.AddSubElement(Property
property, ValidatableElement element)
at
NHibernate.Validator.Event.ValidatePreInsertEventListener.SubElementsInspector.AddSubElement(Property
property, ValidatableElement element)
at
NHibernate.Validator.Event.ValidatePreInsertEventListener.SubElementsInspector.Inspect(ValidatableElement
element)
at NHibernate.Validator.Engine.ValidatorEngine.AddValidator(Type
entityType, IValidatableSubElementsInspector inspector)
at
NHibernate.Validator.Event.ValidatePreInsertEventListener.Initialize(Configuration
cfg)
at NHibernate.Event.EventListeners.InitializeListeners(Configuration
cfg, Object[] list)
at NHibernate.Event.EventListeners.InitializeListeners(Configuration
cfg)
at NHibernate.Cfg.Configuration.GetInitializedEventListeners()
at NHibernate.Cfg.Configuration.BuildSessionFactory()
at
Debify.Hospitality.Infrastructure.Test.NHibernateTests.Validation.NestedComponentsTest.TestValidationWithNestedComponent()
in NestedComponentsTest.cs: line 26

My test (quite lengthy):

using System;
using Decta.Common.Data;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Conventions.Helpers;
using FluentNHibernate.Mapping;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Validator.Cfg;
using NHibernate.Validator.Cfg.Loquacious;
using NHibernate.Validator.Engine;
using NHibernate.Validator.Exceptions;
using NUnit.Framework;

namespace
Debify.Hospitality.Infrastructure.Test.NHibernateTests.Validation
{
    [TestFixture]
    public class NestedComponentsTest
    {
        [Test]
        [ExpectedException(typeof(InvalidStateException))]
        public void TestValidationWithNestedComponent()
        {
            Configuration config = GetConfig(true);
            ISessionFactory factory = config.BuildSessionFactory();

            using (var session = factory.OpenSession())
            {

                new SchemaExport(config).Execute(true, true, false,
session.Connection, Console.Out);

                session.Save(new EntityWithComponent() { Name =
"ABCDEF" });
                session.Save(new EntityWithComponent() { Name =
"123456789012345678901234567890" });

                session.Flush();
            }
        }

        [Test]
        [ExpectedException(typeof(InvalidStateException))]
        public void TestValidationWithoutNestedComponent()
        {
            Configuration config = GetConfig(false);
            ISessionFactory factory = config.BuildSessionFactory();

            using (var session = factory.OpenSession())
            {

                new SchemaExport(config).Execute(true, true, false,
session.Connection, Console.Out);

                session.Save(new EntityWithoutComponent() { Name =
"ABCDEF" });
                session.Save(new EntityWithoutComponent() { Name =
"123456789012345678901234567890" });

                session.Flush();
            }
        }

        private static Configuration GetConfig(bool
withNestedComponent)
        {
            return Fluently.Configure()
                .Database(new SQLiteConfiguration()
                              .InMemory()
                              .ShowSql())
                .Mappings(m =>
                          m.FluentMappings
                              .Add(withNestedComponent ?
typeof(EntityWithComponentMap) : typeof (EntityWithoutComponentMap))
                              .Conventions.Add(DefaultLazy.Never()))

                .ExposeConfiguration(c => ValidationConfiguration(c,
withNestedComponent))
                .BuildConfiguration();
        }


        private static void ValidationConfiguration(Configuration
nhconfig, bool withNestedComponent)
        {

            var configure = new
global::NHibernate.Validator.Cfg.Loquacious.FluentConfiguration();

            if (withNestedComponent)
                configure.Register(new
EntityWithComponentValididate());
            else
                configure.Register(new
EntityWithoutComponentValididate());

            configure
                .SetDefaultValidatorMode(ValidatorMode.UseExternal)
                .IntegrateWithNHibernate
                .ApplyingDDLConstraints()
                .And
                .RegisteringListeners();

            var validatorEngine = new ValidatorEngine();
            validatorEngine.Configure(configure);

            ValidatorInitializer.Initialize(nhconfig,
validatorEngine);

 
global::NHibernate.Validator.Cfg.Environment.SharedEngineProvider =
new SharedValidatorEngineProvider(validatorEngine);

        }
    }

    public class EntityWithoutComponent
    {

        private int _id = 0;

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _name;

        public virtual string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }

    public class EntityWithComponent
    {

        private int _id = 0;

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _name;

        public virtual string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private Component _component = new Component();

        public Component Component
        {
            get { return _component; }
            set { _component = value; }
        }


    }

    public class Component
    {
        private int _value = 0;

        public int Value
        {
            get { return _value; }
            set { _value = value; }
        }

        public NestedComponent NestedComponent
        {
            get { return _nestedComponent; }
            set { _nestedComponent = value; }
        }

        private NestedComponent _nestedComponent = new
NestedComponent();
    }

    public class NestedComponent
    {
        private string _description = String.Empty;

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }
    }

    public class EntityWithComponentMap :
ClassMap<EntityWithComponent>
    {
        public EntityWithComponentMap()
        {
            Id(a => a.Id);
            Map(a => a.Name);

            Component(a => a.Component, m =>
                                            {
                                                m.Map(c => c.Value);
                                                m.Component(c =>
c.NestedComponent, m2 =>
 
{
 
m2.Map(n => n.Description);
                                                                                
        });
                                            });

        }
    }

    public class EntityWithoutComponentMap :
ClassMap<EntityWithoutComponent>
    {
        public EntityWithoutComponentMap()
        {
            Id(a => a.Id);
            Map(a => a.Name);

        }
    }

    public class EntityWithComponentValididate :
ValidationDef<EntityWithComponent>
    {
        public EntityWithComponentValididate()
        {
            Define(a => a.Name).LengthBetween(2, 10);
        }
    }

    public class EntityWithoutComponentValididate :
ValidationDef<EntityWithoutComponent>
    {
        public EntityWithoutComponentValididate()
        {
            Define(a => a.Name).LengthBetween(2, 10);
        }
    }
}

-- 
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to fluent-nhibern...@googlegroups.com.
To unsubscribe from this group, send email to 
fluent-nhibernate+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en.

Reply via email to