Hi all,
I've ran into a problem when I tried to migrate the idea of this
artice 'http://www.codeproject.com/KB/cs/NHibernateForWinforms.aspx'
to fluent:
I use this approach in my projects, but the one that I'm currently
working has a table with a composite primary key.
This is my class...
namespace Hist.Core.Domain
{
public class AssetPrice : DomainObject<AssetPrice.AssetPriceID>
{
#region CompositeID Definition
/// <summary>
/// CompositeID Definition for AssetPrice class
/// </summary>
[Serializable]
public class AssetPriceID
{
public DateTime Date { get; protected set; }
public int AssetIndex { get; protected set; }
public int QualifierIndex { get; protected set; }
public AssetPriceID() { }
public AssetPriceID(DateTime priceDate,int assetIndex,int
qualifierIndex)
{
Date = priceDate;
AssetIndex = assetIndex;
QualifierIndex = qualifierIndex;
}
public override bool Equals(object obj)
{
if (obj == this) return true;
if (obj == null) return false;
AssetPriceID that = obj as AssetPriceID;
if (that == null)
{
return false;
}
else
{
if (AssetIndex != that.AssetIndex) return false;
if (QualifierIndex != that.QualifierIndex) return
false;
if (Date.CompareTo(that.Date) != 0) return false;
return true;
}
}
public override int GetHashCode()
{
return HashCodeGenerator.GenerateHashCode(Date,
AssetIndex, QualifierIndex);
}
}
#endregion
#region Properties
public virtual DateTime Date { get { return base.ID.Date; } }
public virtual int AssetIndex { get { return
base.ID.AssetIndex; } }
public virtual int QualifierIndex { get { return
base.ID.QualifierIndex; } }
public virtual int ProviderIndex { get; set; }
public virtual double Price { get; set; }
#endregion
#region Constructors
public AssetPrice() { }
public AssetPrice(AssetPriceID id)
{
base.ID = id;
}
#endregion
#region Public Methods
public override int GetHashCode()
{
return ID.GetHashCode();
}
public override string ToString()
{
return this.GetPropertiesInfo();
}
#endregion
}
}
and this is the base class
/// <summary>
/// For a discussion of this object, see
///
http://devlicio.us/blogs/billy_mccafferty/archive/2007/04/25/using-equals-gethashcode-effectively.aspx
/// </summary>
public abstract class DomainObject<IdT>
{
public static readonly string IDStr = "ID";
private IdT id = default(IdT);
/// <summary>
/// ID may be of type string, int, custom type, etc.
/// Setter is protected to allow unit tests to set this
property via reflection and to allow
/// domain objects more flexibility in setting this for those
objects with assigned IDs.
/// </summary>
/// <value>The ID.</value>
public virtual IdT ID
{
get { return id; }
protected set { id = value; }
}
/// <summary>
/// Determines whether the specified <see
cref="T:System.Object"/> is equal to the current <see
cref="T:System.Object"/>.
/// </summary>
/// <param name="obj">The <see cref="T:System.Object"/> to
compare with the current <see cref="T:System.Object"/>.</param>
/// <returns>
/// true if the specified <see cref="T:System.Object"/> is
equal to the current <see cref="T:System.Object"/>; otherwise, false.
/// </returns>
/// <exception cref="T:System.NullReferenceException">The
<paramref name="obj"/> parameter is null.</exception>
/// <remarks>
/// HACK:Removed the sealed property, because the tests were
failing.
/// </remarks>
public override bool Equals(object obj)
{
DomainObject<IdT> compareTo = obj as DomainObject<IdT>;
return (compareTo != null) &&
(HasSameNonDefaultIdAs(compareTo) ||
// Since the IDs aren't the same, either of them must
be transient to
// compare business value signatures
(((IsTransient()) || compareTo.IsTransient()) &&
HasSameBusinessSignatureAs(compareTo)));
}
/// <summary>
/// Transient objects are not associated with an item already
in storage. For instance,
/// a <see cref="Customer"/> is transient if its ID is 0.
/// </summary>
/// <returns>
/// <c>true</c> if this instance is transient; otherwise,
<c>false</c>.
/// </returns>
public virtual bool IsTransient()
{
return ID == null || ID.Equals(default(IdT));
}
/// <summary>
/// Must be provided to properly compare two objects
/// </summary>
/// <returns>
/// A hash code for the current <see cref="T:System.Object"/>.
/// </returns>
public abstract override int GetHashCode();
/// <summary>
/// Determines whether [has same business signature as] [the
specified compare to].
/// </summary>
/// <param name="compareTo">The compare to.</param>
/// <returns>
/// <c>true</c> if [has same business signature as] [the
specified compare to]; otherwise, <c>false</c>.
/// </returns>
private bool HasSameBusinessSignatureAs(DomainObject<IdT>
compareTo)
{
//Check.Require(compareTo != null, "compareTo may not be
null");
return GetHashCode().Equals(compareTo.GetHashCode());
}
/// <summary>
/// Returns true if self and the provided persistent object
have the same ID values
/// and the IDs are not of the default ID value
/// </summary>
/// <param name="compareTo">The compare to.</param>
/// <returns>
/// <c>true</c> if [has same non default id as] [the
specified compare to]; otherwise, <c>false</c>.
/// </returns>
private bool HasSameNonDefaultIdAs(DomainObject<IdT>
compareTo)
{
//Check.Require(compareTo != null, "compareTo may not be
null");
return (ID != null && !ID.Equals(default(IdT))) &&
(compareTo.ID != null && !compareTo.ID.Equals
(default(IdT))) &&
ID.Equals(compareTo.ID);
}
}
However, everytime the FixtureBase class throws an error when I reach
SessionSource = new SessionSource(cfg.ToProperties(), new TModel
());...
public class FixtureBase<TModel> where TModel : PersistenceModel,
new()
{
protected SessionSource SessionSource { get; set; }
protected ISession Session { get; private set; }
[SetUp]
public void SetupContext()
{
Console.WriteLine("********* Setup Context *********");
var cfg = new SQLiteConfiguration()
.InMemory()
.ProxyFactoryFactory
("NHibernate.ByteCode.Spring.ProxyFactoryFactory,
NHibernate.ByteCode.Spring")
.ShowSql();
SessionSource = new SessionSource(cfg.ToProperties(), new
TModel());
Here, I get the following exception:
PoloHist.Tests.CoreTest.ModelTests.PoloAssetPricesTests.can_correctly_map_PoloAssetPrice:
NHibernate.PropertyNotFoundException : Could not find a setter for
property 'Date' in class 'PoloHist.Core.Domain.PoloAssetPrice'
Ok, I can see the exception, but if I remove the protected clause,
I'll go against the hole thing that I'm trying to do, that is, protect
the ID.
What should I do. Should I change my implementation?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Fluent NHibernate" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/fluent-nhibernate?hl=en
-~----------~----~----~----~------~----~------~--~---