Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

get user information in the application layer #413


User avatar
0
cicciottino created

im my application layer, using "AbpSession.UserId" i can recover the id of the user, but what if i want to recover the username or other properties related to the user?

Should i query the db each time to gather these information?

I've seen that in the "AbpZeroSampleAppServiceBase"(downloaded from the template) there is a method named "GetCurrentUserAsync", could i use this? if yes, what is the better way to do it?

  1. make my application layer inherited from "AbpZeroSampleAppServiceBase" insted of "ApplicationService"
  2. inject this application layer inside my application layer
  3. avoiding to requery each time the db and extend the AbpSession in order to contain also the information i need (if yes, is there an extension point where i can load this information the first time and thant use wherever: AbpSession.UserName, AbpSession.MyCustomProperty)

4 Answer(s)
  • User Avatar
    0
    cicciottino created

    the AbpZeroSampleAppServiceBase.cs is an abstract class, so it can't be injected but must be only inherited, is it true? in this case the option 2) in not possible Correct me if i'm wrong, please. what about the others options?

  • User Avatar
    0
    ddnils created

    Have you considered using

        private readonly UserManager _userManager;
    

    in your Controller (use DI to get the actual Manager)?

    this should give you: _userManager.GetUserByIdAsync()

  • User Avatar
    0
    cicciottino created

    <cite>ddNils: </cite> Have you considered using

       private readonly UserManager _userManager;
    

    in your Controller (use DI to get the actual Manager)?

    in my ApplicationLayer you mean? right?

    this is what i need thanks!

    just a doubt: anyway this method re-query the db each time is it right?! nothing to do with cache or session

  • User Avatar
    0
    hikalkan created
    Support Team

    Yes, it gets from query every time, doesn't cache. You can write a caching mechanism for it. But I think it's not needed since it gets by Id and it's fast. Caching is problematic since you should consider invalidate the cache when User properties changes. Also, if you cache User entity, then you can not use it in a transaction, since concurrent requests will share the same User object. So, if you need caching, I suggest to cache the only UserName.