Say I have a class, e.g.:
public class Person
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual int SpouseId { get; set; }
public virtual Person Spouse { get; set; }
}
I have included the SpouseId because that is the out-of-the-box way to model bind using (Try)UpdateModel (based on ScottGu's sample MVC 2.0 code here). In my NHibernate mapping I have set SpouseId to insert=false and update=false, and my mapping works just fine.
I to have a "create person" form in ASP.NET MVC (2), which then uses SpouseId to select the spouse:
<%
using (Html.BeginForm())
{
%>
<%= Html.LabelFor(m => m.Name) %>
<%= Html.TextBoxFor(m => m.Name) %>
<%= Html.ValidationMessageFor(m => m.Name) %>
<%= Html.LabelFor(m => m.SpouseId) %>
<%= Html.EditorFor(m => m.SpouseId, "PersonPicker") %>
<%= Html.ValidationMessageFor(m => m.SpouseId) %>
<%
}
%>
Where I have an EditorTemplate for "PersonPicker" that just displays a dropdown based on a list of person's in ViewData.
When I then do TryUpdateModel() it works fine, I get my values populated like expected, i.e. SpouseId gets set but not Spouse. and the problem with this is of course that NHibernate uses Person.Spouse.
I would definitely prefer to skip the SpouseId entirely since it is cluttering my model and it is only there because of this issue.
I could of course do person.Spouse = myService.Find<Person>(person.SpouseId);
in code but that has a very, very nasty smell to it.
What is the best way to do this? Preferably dropping the SpouseId property entirely.
(I might add that I am using VS2008 and .NET 3.5)