views:

106

answers:

2

Hi

I'm trying to get my schema export to work with Validators Expressed in my Domain Object. I have a [NotNull] attribute AND a ValidatioDef on a property but the column is still expressed as nullable by the schema export. Im sure its a config issue, but not sure where. Some wiring has gone haywire. Here is my config and gerneration code.

[Test]
  public void GenerateSchemaWithValidation()
  {

   var nhvConfiguration = new FluentConfiguration();
   nhvConfiguration
      .SetDefaultValidatorMode(ValidatorMode.UseExternal)
      .Register(Assembly.Load("MyDomainAssembly")
       .ValidationDefinitions())
      .IntegrateWithNHibernate
        .ApplyingDDLConstraints()
        .And
        .RegisteringListeners();

   var nhibernateConfig = new Configuration().Configure();

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

   nhibernateConfig.Initialize(validatorEngine);

   ConfigureDatabaseAndMappings()
    .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
    .BuildSessionFactory();
  }

protected static FluentConfiguration ConfigureDatabaseAndMappings()
    {
        return Fluently.Configure()
            .Database(
                MsSqlConfiguration.MsSql2005.ConnectionString(c => c.FromConnectionStringWithKey("MyDb"))
                .ShowSql())
            .Mappings(m => 
                m.FluentMappings.AddFromAssemblyOf<MediaDescriptionMap>()
                .Conventions.AddFromAssemblyOf<WellNamedForeignKeyColumnConvention>());


    }
A: 

NHibernate Validator doesn't alter the mappings that are created, it just validates your entity against your rules before saving. You need to also specify in your mappings that you don't want your columns to be nullable.

Map(x => x.Property)
  .Not.Nullable();

If you're doing that for a lot of properties, it might be worth looking into using a convention; specifically a PropertyAttributeConvention would work well in your case.

James Gregory
+1  A: 

Yes you can. You have to tell Fluent to tell NHibernate via ExposeConfiguration:

This works.

[Test]
    public void DoGenerateSchema()
    {
        ConfigureDatabaseAndMappings()
   .ExposeConfiguration(ExportSchema)
            .BuildSessionFactory();
    }

 private static void ExportSchema(Configuration cfg)
 {
  var nhvConfiguration = new FluentConfiguration();
  nhvConfiguration
     .SetDefaultValidatorMode(ValidatorMode.UseAttribute)
     .Register(Assembly.Load("MyDomainAssembly")
      .ValidationDefinitions())
     .IntegrateWithNHibernate
       .ApplyingDDLConstraints()
       .And
       .RegisteringListeners();


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

  cfg.Initialize(validatorEngine);
  new SchemaExport(cfg).Create(true, true);
 }

and for those wondering like I was I know see that ValidatorMode.UseAttribute only picks up e.g.[NotNull] and ValidatorMode.UseExternal picks up ValidationDefs

All this so that business rules are encapsulated in the domain/business tier not by the database mappings. (check HunabKu's blog for some good discussions and examples)

BobTodd
which version of fluent-nhibernate, nhibernate and nhibernate validator are you using here?
ajma

related questions