質問

ドメインオブジェクトで表現されたバリデーターでスキーマエクスポートを動作させようとしています。プロパティに[NotNull]属性とValidatioDefがありますが、スキーマのエクスポートでは列がnull可能として表されます。構成の問題は確かですが、どこかはわかりません。一部の配線が問題になりました。これが私の設定と生成コードです。

[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>());


    }
役に立ちましたか?

解決

はい、できます。 FluoseにExposeConfigurationを介してNHibernateに指示する必要があります:

これは動作します。

[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);
    }

ValidatorMode.UseAttributeは[NotNull]のみをピックアップし、ValidatorMode.UseExternalはValidationDefsをピックアップするということを知っているので、私がそうであるように不思議に思う人のために

これにより、ビジネスルールはデータベースマッピングではなくドメイン/ビジネス層にカプセル化されます。 (HunabKuのブログで適切な議論と例について確認してください)

他のヒント

NHibernate Validatorは作成されたマッピングを変更せず、保存する前にルールに対してエンティティを検証するだけです。また、マッピングで列をNULL可能にしたくないことを指定する必要があります。

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

多くのプロパティでこれを行っている場合は、コンベンション;具体的には、 PropertyAttributeConvention があなたの場合にうまく機能します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top