简体   繁体   中英

Structure (Or any DI component) setting singleton values

I am using StructureMap, but i think any DI component or should be able to answer this question.

Let me lay out the concern which i think is possible but figured it would be good to talk it out.

  • I have want to inject and IUser into application stack. I want to create it as a singleton when created by structuremap
  • I want it to have a IUser.SetUser(permissions perm, accessRules, rules) method that will be called in the authentication logic

Question

After the method in IUser.SetUser(permissions perm, accessRules, rules) gets called lets say in my controller layer. When IUser now gets injected into the lower layers such as command handlers, services, event handlers, etc. that singleton instance will retain the values of the properties set IUser.SetUser(permissions perm, accessRules, rules) was called correct?

it seems like a simple yes from what i believe about DI, but figured i would ask to make sure.

Usually, all instances of objects in web applications are created per one request. So, if your IUser has per request lifetime, it'll keep all data during current request. If you create IUser as a singleton for whole application, it'll keep data till app pool recycling .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM