Given a parent child relationship between User and FailedLogin where a user has many failed logins. I’d like to map this into
class User
{
public virtual FailedLogin LastFailedLogin { get; set; }
}
class FailedLogin
{
public virtual User User { get; set; }
public virtual DateTime AttemptOn { get; set; }
}
So that the LastFailedLogin property contains the FailedLogin with the most recent AttemptOn date and time. If LastFailedLogin is set it should save that FailedLogin to the database (i.e. .Cascade.SaveUpdate()) Note, I do not want to map a collection of all FailedLogins for this user for performance reasons.
I have been totally unable to write a fluent mapping for this. How do I map this using Fluent NHibernate?
public class UserMap : ClassMap<User>
{
public UserMap()
{
// What goes here for LastFailedLogin?
}
}
public class FailedLoginMap: ClassMap<FailedLogin>
{
public FailedLoginMap()
{
References(x => x.User).Not.Update();
Map(x => x.AttemptOn).Not.Update();
}
}
The solution I settled on, which I’m not entirely happy with, is:
This solution ensures that only a single
FailedLoginis loaded with theUser. It also handles correctly saving a new failed login that is associated to the user. Finally it ensures that the implementation is hidden from consumers of theUserclass. Unfortunately, this still imposes persistence concerns on theUserclass.