Skip to content

Instantly share code, notes, and snippets.

@ChrisMissal
Created August 19, 2009 03:31
Show Gist options
  • Save ChrisMissal/170138 to your computer and use it in GitHub Desktop.
Save ChrisMissal/170138 to your computer and use it in GitHub Desktop.
A simple NH mapping that is not working. Seems to be a cascade/delete issue.
namespace CRIneta.Web.Core.Domain
{
public class Attendee
{
protected readonly string email;
protected readonly Meeting meeting;
protected readonly int attendeeId;
protected Attendee()
{
// no args constructor used for NHibernate. So much for persistence ignorance!
}
public Attendee(string email, Meeting meeting)
{
this.email = email;
this.meeting = meeting;
}
public virtual string Email
{
get { return email; }
}
public virtual Meeting Meeting
{
get { return meeting; }
}
public override bool Equals(object obj)
{
var other = obj as Attendee;
if(other == default(Attendee))
return false;
return (email == other.email);
}
}
}
<class name="Meeting" table="Meetings">
<id name="MeetingId" column="Id" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="Title" column="Title" length="255" not-null="true" />
<property name="StartTime" column="StartsAt" type="DateTime" not-null="true" />
<property name="EndTime" column="EndsAt" type="DateTime" not-null="true" />
<property name="Description" column="Description" type="StringClob" not-null="true" />
<property name="Presenter" column="Presenter" length="255" not-null="true" />
<many-to-one name="Facility" column="FacilityId" not-null="true" lazy="false" class="Facility" foreign-key="FK_Meetings_Facilities" />
<bag name="Attendees" access="nosetter.camelcase" cascade="all-delete-orphan" lazy="false" inverse="true">
<key column="MeetingId" />
<one-to-many class="Attendee" />
</bag>
</class>
<class name="Attendee" table="Attendees">
<id name="attendeeId" column="Id" type="Int32" unsaved-value="0" access="field">
<generator class="native" />
</id>
<many-to-one name="Meeting" column="MeetingId" class="Meeting" not-null="true" access="nosetter.camelcase" />
<property name="email" column="Email" length="100" not-null="true" access="field" />
</class>
using System;
using System.Collections.Generic;
using System.Linq;
namespace CRIneta.Web.Core.Domain
{
public class Meeting
{
private readonly IList<Attendee> attendees;
public Meeting()
{
attendees = new List<Attendee>();
}
public virtual int AttendeeCount
{
get { return attendees.Count(); }
}
protected virtual IEnumerable<Attendee> Attendees
{
get { return attendees; }
}
public virtual int MeetingId { get; set; }
public virtual string Title { get; set; }
public virtual DateTime StartTime { get; set; }
public virtual DateTime EndTime { get; set; }
public virtual string Description { get; set; }
public virtual string Presenter { get; set; }
public virtual Facility Facility { get; set; }
public virtual void AddAttendee(Attendee attendee)
{
if (!AlreadyAttending(attendee))
attendees.Add(attendee);
}
/// <summary>
/// Returns whether the attendee is already attending.
/// </summary>
/// <param name="attendee">The attendee.</param>
/// <returns></returns>
private bool AlreadyAttending(Attendee attendee)
{
return attendees.Contains(attendee);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment