Mapeo de enum con nhibernate fluido
Estoy siguiendo el http://wiki.fluentnhibernate.org/Getting_started tutorial para crear mi primer proyecto de NHibernate con Fluent NHibernate
Tengo 2 tablas
1) Cuenta con campos
Id
AccountHolderName
AccountTypeId
2) Tipo de cuenta con campos
Id
AccountTypeName
En este momento los tipos de cuentas pueden ser de Ahorro o Corriente Así que la tabla AccountTypes almacena 2 filas 1 - Ahorro 2-Corriente
Para la tabla AccoutType he definido enum
public enum AccountType {
Savings=1,
Current=2
}
Para la tabla de cuentas que defino la clase de entidad
public class Account {
public virtual int Id {get; private set;}
public virtual string AccountHolderName {get; set;}
public virtual string AccountType {get; set;}
}
Las asignaciones de nhibernate fluidas son:
public AgencyMap() {
Id(o => o.Id);
Map(o => o.AccountHolderName);
Map(o => o.AccountType);
}
Cuando intento ejecutar la solución, da una excepción - InnerException = {"(XmlDocument)(2,4): Error de validación XML: El elemento 'class' en el espacio de nombres 'urn:nhibernate-mapping-2.2' tiene contenido incompleto. Lista de posibles elementos esperados: 'meta, subselect, cache, synchronize, comment, tuplizer, id, composite-id' in namespace 'ur...
Supongo que eso es porque no he especificado ningún mapeo para AccountType.
Las preguntas son:
- Cómo puedo usar AccountType enum ¿en lugar de una clase AccountType?
- Tal vez estoy yendo por el camino equivocado. ¿Hay una mejor manera de hacer esto?
Gracias!
3 answers
Lo siguiente aparentemente ya no funciona https://stackoverflow.com/a/503327/189412
¿Qué tal simplemente hacer esto:
public AgencyMap() {
Id(o => o.Id);
Map(o => o.AccountHolderName);
Map(o => o.AccountType).CustomType<AccountType>();
}
El tipo personalizado maneja todo:)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 11:47:19
public class Account {
public virtual int Id {get; private set;}
public virtual string AccountHolderName {get; set;}
public virtual AccountType AccountType {get; set;}
}
public AgencyMap() {
Id(o => o.Id);
Map(o => o.AccountHolderName);
Map(o => o.AccountType);
}
Fluent NHibernate guarda los valores de enumeración como cadena de forma predeterminada si desea anular que necesita proporcionar una convención para ella. Algo como:
public class EnumConvention :
IPropertyConvention,
IPropertyConventionAcceptance
{
#region IPropertyConvention Members
public void Apply(IPropertyInstance instance)
{
instance.CustomType(instance.Property.PropertyType);
}
#endregion
#region IPropertyConventionAcceptance Members
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => x.Property.PropertyType.IsEnum ||
(x.Property.PropertyType.IsGenericType &&
x.Property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) &&
x.Property.PropertyType.GetGenericArguments()[0].IsEnum)
);
}
#endregion
}
Casi olvido que también necesita agregar la convención a su configuración fluent. Lo hace en el mismo lugar donde agrega las asignaciones:
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<BillingRecordMap>()
.Conventions.AddFromAssemblyOf<EnumConvention>()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-01-18 07:38:49
Una Gran manera de hacer esto, es implementar la interfaz IUserType y Crear un CustomType con las reglas para escribir y leer, este es un ejemplo para booleano:
public class CharToBoolean : IUserType
{
public SqlType[] SqlTypes => new[] { NHibernateUtil.String.SqlType };
public Type ReturnedType => typeof(bool);
public bool IsMutable =>true;
public object Assemble(object cached, object owner)
{
return (cached);
}
public object DeepCopy(object value)
{
return (value);
}
public object Disassemble(object value)
{
return (value);
}
public new bool Equals(object x, object y)
{
if (ReferenceEquals(x, y)) return true;
var firstObject = x as string;
var secondObject = y as string;
if (string.IsNullOrEmpty(firstObject) || string.IsNullOrEmpty(secondObject)) return false;
if (firstObject == secondObject) return true;
return false;
}
public int GetHashCode(object x)
{
return ((x != null) ? x.GetHashCode() : 0);
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var obj = NHibernateUtil.String.NullSafeGet(rs, names[0]);
if (obj == null) return null;
var value = (string)obj;
return value.ToBoolean();
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
if(value != null)
{
if ((bool)value)
{
((IDataParameter)cmd.Parameters[index]).Value = "S";
}
else
{
((IDataParameter)cmd.Parameters[index]).Value = "N";
}
}
else
{
((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
}
}
public object Replace(object original, object target, object owner)
{
return original;
}
}
}
El mapeo:
this.Map(x => x.DominioGenerico).Column("fldominiogen").CustomType<CharToBoolean>();
Es una muestra, pero puede hacer esto con otros tipos.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-18 12:14:09