I am having troubles with using the .Contains() method on my bags. Basically, half of the time the Contain() method matches and half the time it doesn't. I thought it had to do with the equals() method but I've added those and it still isn't working as I expect. It only seems to be a problem when I am passing a component object... as an example:
I have a Role object, a Member Object, and a MemberRole association object. In my application I have a search function that allows you to add a Member to a given role so I perform a query based on search parameters and return a list of Members. Then I load a Role from the database. I then pass each Member from the searchResults List to the Role.Members.Contains() method and like I said, half the time it works and half the time it doesn/t My test case always involves a Member that I know to be associated with the given Role.
some code follows...
Role Mapping:
Code:
<class name="Role" table="Role" proxy="Role">
<id name="id" column="RoleID" type="Int32" unsaved-value="-1" access="field" >
<generator class="identity" />
</id>
<property name="Name" column="`Name`" type="String" not-null="true" />
<property name="AccessLevel" column="AccessLevel" type="Int32" not-null="true" access="nosetter.camelcase" />
<bag name="Members" table="MemberRole" cascade="none" lazy="true">
<key column="RoleID" />
<one-to-many class="MemberRole"/>
</bag>
</class>
MemberRole association class:
Code:
<class name="MemberRole" table="MemberRole" proxy="MemberRole">
<id name="id" column="`ID`" type="Int32" unsaved-value="-1" access="field">
<generator class="identity" />
</id>
<many-to-one name="Member"
class="Member, AMA.Core"
column="MemberID"
not-null="true" />
<many-to-one name="Role"
class="Role"
column="RoleID"
not-null="true" />
<property name="DateCreated" column="DateCreated" type="DateTime" not-null="true" update="false" access="nosetter.camelcase" />
<many-to-one name="Creator" class="Member" column="CreatorID" />
</class>
Application code trying to use ArrayList.Contains() method. The idea here is that if a member is already part of a role domain, then remove them from the search results list and show a message to the viewer along with the results list:
Code:
IList members = base.DomainManager.FindMembersByName(this.FirstName.Text, this.LastName.Text);
if (members.Count > 0)
{
bool foundDuplicateMember = false;
foreach (MemberRole mr in this._role.Members)
{
if (members.Contains(mr.Member))
{
members.Remove(mr.Member);
foundDuplicateMember = true;
}
}
if (foundDuplicateMember)
ShowFormMessage(MessageType.Note, "Some Members are not visible because they already belong to this Role.");
if (members.Count > 0)
{
this.SearchResults.DataSource = members;
this.SearchResults.DataBind();
}
}
else
{
base.ShowFormMessage(MessageType.Note, "There are no existing Members that match your search parameters.");
}
and the equals code from the Member class. LoginID is always unique and, therefore, a business key:
Code:
#region IComparable Members
public int CompareTo(object obj)
{
if (obj is Member)
{
return this.loginID.CompareTo(((Member) obj).loginID);
}
return 0;
}
#endregion
I realize this is not entirely an NH question, but I'm really struggling with this...
TIA,
-devon