I'm having troubles reaching this goal:

I have a entity called ApplicationUser that I want to save to a static property in my businesslayer so that I allways know wich user is authenticated.

When I create the domain the currently logged in user is saved to this property, but when the session ends and the next time I create a new Session and try to persist another entity (say Appointment) I want to do this:

Appointment.CreatedBy = ApplicationUser.CurrentLoggedInUser();

The application throws an exception that the operation is not permitted because the ApplicationUser is created in a different Session (duh... :P).

My question is how I can save a entity so that I can bind them later on to the currently active Session. I know it should be something with the DisconnectedState, but I can't figure it out...


Updated at 22.06.2010 10:10:29

No, roundtrip will happen just once per each transaction (i.e. exactly when necessary). Query.SingleOrDefault checks the cache first. Of course.. I forgot that DataObjects.Net is THAT smart! :D

Thanks for the explanation!

This thread was imported from our support forum. The original discussion may contain more detailed answer. Original topic by stefmen.

asked Jun 22 '10 at 07:04

Editor's gravatar image

Editor
46155156157


One Answer:

Save either its Key by the code like this:

private static Key authenticatedUserKey;
public static User AuthenticatedUser {
  get { return Query.SingleOrDefault<User>(authenticatedUserKey); } // Relies on Session.Demand()! 
  set { return authenticatedUserKey = value.Key; }
}

Or use Ref<t> type - a nice wrapper for such tasks:

private static Ref<User> authenticatedUserRef;
public static User AuthenticatedUser {
  get { return authenticatedUserRef; } // Relies on Session.Demand() + there is implicit conversion operator to T ;)
  set { return authenticatedUserRef = value; } // There is implicit conversion operator from T.
}

answered Jun 22 '10 at 07:28

Alex%20Yakunin's gravatar image

Alex Yakunin
29714412

stefmen wrote: Thank you!

This was also my 'workaround' solution, because now everytime the authenticateduser is requested DO 4.x has a roundtrip to the database? Or am I wrong?

(Jun 22 '10 at 07:28) Editor Editor's gravatar image

No, roundtrip will happen just once per each transaction (i.e. exactly when necessary).

Query.SingleOrDefault checks the cache first.

(Jun 22 '10 at 07:28) Alex Yakunin Alex%20Yakunin's gravatar image

Well, that's our own method, so that was easy ;)

(Jun 22 '10 at 07:28) Alex Yakunin Alex%20Yakunin's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA