Fluente NHibernate Alterações / Convenções
-
03-07-2019 - |
Pergunta
Eu gosto da serra padrão que eu neste blog ( http: //marekblotny.blogspot.com/2009/04/conventions-after-rewrite.html ), onde o autor está a verificar para ver se um nome de tabela alteração já foi feita antes de aplicar uma convenção.
public bool Accept(IClassMap target)
{
//apply this convention if table wasn't specified with WithTable(..) method
return string.IsNullOrEmpty(target.TableName);
}
A convenção de interface que estou usando para um comprimento da corda é IProperty:
public class DefaultStringLengthConvention: IPropertyConvention
{
public bool Accept(IProperty property) {
//apply if the string length hasn't been already been specified
return ??; <------ ??
}
public void Apply(IProperty property) {
property.WithLengthOf(50);
}
}
Não vejo onde IProperty expõe tudo o que me diz se a propriedade já foi configurado. Isso é possível?
TIA, Berryl
Solução 3
Para esclarecer em código que Stuart e Jamie estão dizendo, aqui está o que funciona:
public class UserMap : IAutoMappingOverride<User>
{
public void Override(AutoMap<User> mapping) {
...
const int emailStringLength = 30;
mapping.Map(x => x.Email)
.WithLengthOf(emailStringLength) // actually set it
.SetAttribute("length", emailStringLength.ToString()); // flag it is set
...
}
}
public class DefaultStringLengthConvention: IPropertyConvention
{
public bool Accept(IProperty property) {
return true;
}
public void Apply(IProperty property) {
// only for strings
if (!property.PropertyType.Equals(typeof(string))) return;
// only if not already set
if (property.HasAttribute("length")) return;
property.WithLengthOf(50);
}
}
Outras dicas
.WithLengthOf()
adiciona uma "alteração" (Action<XmlElement>
) para a lista de alterações para aplicar quando o mapeamento XML é gerado. Infelizmente, esse campo é private
e não há nenhuma propriedade para acessar a lista de alteração, então eu tenho medo há (atualmente) nenhuma maneira de verificar se um mapa da propriedade teve WithLengthOf
aplicada a ele.
Até que uma alternativa melhor vem junto, você pode usar HasAttribute("length")
.