pour mon projet en cours J'utilise Castle ActiveRecord en C#. Pour l'une de mes tables, j'ai besoin d'utiliser une classe de type personnalisée (traitant d'une conversion de temps stupide en timespan). Pour garder mon code propre, j'aime définir la classe qui est dérivée de IUserType
dans la classe de mappage d'objet. Mais je ne peux trouver aucun moyen de cartographier cette propriété en utilisant cette sous-classe, ActiveRecord garde se plaindre: Could not determine type for (...)
Castle ActiveRecord: Mapper à IUserType wihtin Classe en C#
Voici un petit échantillon:
namespace testForActiveRecord
{
[ActiveRecord("[firstTable]")]
public class firstTable:ActiveRecordBase<firstTable>
{
private TimeSpan _TStest;
// more private fields and properties here
[Property(ColumnType = "testForActiveRecord.firstTable.StupidDBTimeSpan, testForActiveRecord")]
public TimeSpan TStest
{
get { return _TStest; }
set { _TStest = value; }
}
// Usertype doing the conversion from a date saved in the DB to the timespan it is representing
// The TimeSpan is saved by an offset to the date 30.12.1899...
public class StupidDBTimeSpan : IUserType
{
#region IUserType Member
DateTime Basis = new DateTime(1899,12,30,00,00,00);
object IUserType.Assemble(object cached, object owner)
{
return cached;
}
object IUserType.DeepCopy(object value)
{
return value;
}
object IUserType.Disassemble(object value)
{
return value;
}
bool IUserType.Equals(object x, object y)
{
if (x == y) return true;
if (x == null || y == null) return false;
return x.Equals(y);
}
int IUserType.GetHashCode(object x)
{
return x.GetHashCode();
}
bool IUserType.IsMutable
{
get { return false; }
}
public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
{
object obj = NHibernateUtil.DateTime.NullSafeGet(rs, names[0]);
TimeSpan Differenz = new TimeSpan();
if (obj != null)
{
Differenz = (DateTime)obj - Basis;
}
return Differenz;
}
public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
{
if (value == null)
{
((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
}
else
{
NHibernateUtil.DateTime.NullSafeSet(cmd, Basis + (TimeSpan)value, index);
}
}
object IUserType.Replace(object original, object target, object owner)
{
return original;
}
Type IUserType.ReturnedType
{
get { return typeof(TimeSpan); }
}
NHibernate.SqlTypes.SqlType[] IUserType.SqlTypes
{
get { return new SqlType[] { new SqlType(DbType.DateTime) }; }
}
#endregion
}
}
}
Il n'y a pas de problème si la classe StupidDBTimeSpan
est définie en dehors de la classe testForActiveRecord
et je cartographie la propriété en utilisant [Property(ColumnType = "testForActiveRecord.StupidDBTimeSpan, testForActiveRecord")]
.
Qu'est-ce que je fais mal? Est-il possible d'utiliser cette construction de sous-classe avec ActiveRecord?
Cordialement sc911
Cela a résolu le problème! Merci beaucoup! Maintenant pouvez-vous me dire un lien fournissant toute la documentation qui répondra à ces questions pour moi? Parce que je ne pouvais pas trouver de documentation complète pour cela ... – sc911
@ sc911: c'est assez complet ...: http://msdn.microsoft.com/en-us/library/yfsftwz6.aspx - et sinon, un 'typeof (xxx) .FullName' devrait vous donner le nom de type correct. – Lucero