Given a fairly simple structure in C # ...
class Member
{
virtual string Email { get; set; }
virtual IList<Character> Characters { get; set; }
}
class Character
{
virtual string Name { get; set; }
}
interface IMemberRepository
{
MemberCreateStatus CreateMember(string email, string password);
bool ValidateMember(string email, string password);
Member RetrieveMember(string email);
void SaveMember(Member member);
}
interface ICharacterRepository
{
Character CreateCharacter(string name);
}
class MemberRepository : IMemberRepository
{
private readonly ISessionFactory sessionFactory;
public MemberRepository(ISessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
public Member RetrieveMember(string email)
{
using(var session = sessionFactory.OpenSession())
{
return
}
}
}
I have an ASP.NET MVC application configured to use nHibernate (smooth setup) and Castle.Windsor for IoC. This works fine and good, but that's the problem.
Lazy Loading
By default, nature IList<Character>loads in most ORMs (nHibernate, Entity Framework, etc.) - now this is not the case, but I ran into a lot of confusion about how to approach this scenario.
- I want to add
Characterin Member. Logically, I would use Member.Characters.Add(n);and then callMemberRepository.SaveMember(m);
- . Session, , . , ...
Member
- nHibernate Lazy Loaded Proxy (
IList<Character>) Email
, var member = memberRepository.RetrieveMember(email); my Controller, member.Characters.Add(character); .
, , Session .
, ? , IList<Character> Lazy Load, . ORM , . UnitOfWork IRepository<T> IoC ?
, AddCharacter(Character character) IMemberRepository, , , . , . , , right .
nHibernate Session Persist(object obj), UnitOfWork, , , Lazy , . , , , . Session - Repository SessionFactory. , .