Hey all, new NHibernate user with a problem I hope you can solve.
Let's say I had a class that implemented a recursive list of scheduled tasks. A "task" can be a single "process", or a child "schedule" of multiple tasks. A simplification of Schedule would be as follows:
Code:
public class Schedule:Task
{
private IList tasks = new IList<Task>
public AddNewTask(Task task)
{
//do some validation and add task to tasks
}
public DeleteTask(Task task)
{
//do some validation and remove the task from the list
}
public ReadOnlyCollection<Task> Tasks {get{return new ReadOnlyCollection<Task>(tasks);}}
}
The intent, of course, is to prevent manipulation of the list except through the class methods. This model predates my involvement with the codebase, and is not easily refactored to implement a pure method- or property-based pattern.
The relevant NHibernate elements are below:
Code:
<list name="tasks" table="ScheduleTask" access="field.lowercase" lazy="false" cascade="save-update" batch-size="10">
<key column="ScheduleID"
foreign-key="FK_Task_ScheduleID"/>
<index column="TaskIndex"/>
<many-to-many class="MyProject.Task, MyProject.Domain" column="TaskID"/>
</list>
The problem I am having is when I persist a new schedule, NHibernate tries to use the Tasks property to replace its proxy classes with real data. This occurs even though the access is by field and we are forcing it to use the name as specified.
Everything I've read in the docs say that this should behave as I intend; NHibernate SHOULD be reflectively accessing the backing field directly when hydrating, while all my code objects are forced to abide by the class' stricter access rules. Even the integrated tests are working properly, but production code still fails. Help!