我不確定對象圖是怎麼樣的,但爲什麼不讓會話弄清楚什麼改變了?
void DoSomethingWithMembers()
{
// if session which loaded the members is not open anymore
foreach (var member in loggedInMembers.Select(loggedInMember => loggedInMember.Member))
{
session.Lock(member, LockMode.None); // reattach to session
}
// end if
// make changes to the (loggedIn)members
session.Flush(); // will persist any changes to a member
}
更新:爲響應註釋,像這樣
public AuthenticationResult Authenticate(string username, string password)
{
if (loggedInUsers.ContainsKey(username))
return AuthenticationResult.AlreadyLoggedIn;
using (var session = OpenSession())
{
var member = session.Query<Member>().Where(m => m.Name == username).FirstOrDefault();
}
if (member == null)
return AuthenticationResult.NoUser;
if (member.Password != password)
return AuthenticationResult.WrongPassword;
loggedInUsers.Add(username, new LoggedInMember(DateTime.Now, member));
var dto = mapper.Map<Member, MemberDto>(member);
return new AuthenticationResult(dto);
}
public void Update(ICollection<MemberDto> dtos)
{
using (var session = OpenSession())
{
foreach (var dto in dtos)
{
Update(session, dto);
}
session.Flush();
}
}
void Update(ISession session, MemberDto dto)
{
LoggedInMember liMember;
if (!loggedInUsers.TryGetValue(dto.Name, out liMember)
throw new AuthException("not logged in")
session.Lock(li.Member, LockMode.None); // attach
mapper.Map<Member, MemberDto>(li.Member, dto);
}
凡/當你創建你登錄會員實例? –
這些實例是在用戶登錄時創建的,當它們註銷時會被刪除。至於「哪裏」,我不確定你的意思......(對不起) –
還有其他建議嗎?我想盡可能保持圖形完整。對解決方案有任何意見建議? –