Überschreiben der Bool-Eigenschaftskonvention in fließendem NHHibernate
-
12-12-2019 - |
Frage
Ich experimentiere mit der Automap-Funktion von Fluent Nhibernate..
Ich sah Beispiel welches hatte eine Standard-String-Konvention wie folgt:
namespace Vuscode.Framework.NHibernate.Conventions
{
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;
public class DefaultStringPropertyConvention : IPropertyConvention
{
public void Apply(IPropertyInstance instance)
{
instance.Length(100);
instance.Nullable();
}
}
}
Hier sehe ich nichts, was überprüft, ob die instance
ist vom Typ string ..es macht nur die Länge 100 und nullbar..Woher weiß Automapper, dass er diese Konvention nur auf Zeichenfolgen anwenden kann?
Außerdem möchte ich alle meine Bools nicht nullfähig machen und einen Standardwert von 0 festlegen..also nach dem obigen Beispiel hätte ich das:
namespace Vuscode.Framework.NHibernate.Conventions
{
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;
public class DefaultBoolPropertyConvention : IPropertyConvention
{
public void Apply(IPropertyInstance instance)
{
instance.Not.Nullable();
instance.Default("0");
}
}
}
Aber wenn ich das mache und auch in meinem Projekt eine Standard-String-Konvention habe..woher weiß Automapper dann, auf welche Eigenschaftstypen die Konvention angewendet wird?
Ich habe versucht, das zu lesen fließendes Wiki über Konventionen habe dort aber nichts Nützliches gefunden..
Lösung
die Stringkonvention wird auf alle Eigenschaften angewendet, aber da NHibernate die Länge für Nicht-String-Eigenschaften ignoriert und Nullwert die Standardeinstellung ist, hat sie nur keine Auswirkung.Sie haben zwei Möglichkeiten, Nicht-Bool-Eigenschaften zu filtern
public void Apply(IPropertyInstance instance)
{
if (instance.Type == typeof(bool))
{
instance.Not.Nullable();
instance.Default("0");
}
}
// or
public class DefaultBoolPropertyConvention : IPropertyConvention, IPropertyConventionAcceptance
{
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(i => i.Type == typeof(bool));
}
public void Apply(IPropertyInstance instance)
{
instance.Not.Nullable();
instance.Default("0");
}
}