简体   繁体   English

设置单例值的结构(或任何DI组件)

[英]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. 我正在使用StructureMap,但我认为任何DI组件或应该能够回答此问题。

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. 我想将IUser注入到应用程序堆栈中。 I want to create it as a singleton when created by structuremap 我想在由structuremap创建时将其创建为单例
  • I want it to have a IUser.SetUser(permissions perm, accessRules, rules) method that will be called in the authentication logic 我希望它具有IUser.SetUser(permissions perm,accessRules,rules)方法,该方法将在身份验证逻辑中调用

Question

After the method in IUser.SetUser(permissions perm, accessRules, rules) gets called lets say in my controller layer. 在IUser.SetUser(permissions perm,accessRules,rules)中的方法被调用之后,请在我的控制器层中说。 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? 现在,当IUser注入较低的层(例如命令处理程序,服务,事件处理程序等)时,单例实例将保留IUser.SetUser(permissions perm,accessRules,rules)设置的属性集的值吗?

it seems like a simple yes from what i believe about DI, but figured i would ask to make sure. 从我对DI的信念看来,这似乎是一个简单的肯定,但是我想我要确保确定。

Usually, all instances of objects in web applications are created per one request. 通常,Web应用程序中对象的所有实例都是根据一个请求创建的。 So, if your IUser has per request lifetime, it'll keep all data during current request. 因此,如果您的IUser具有每个请求的生存期,它将在当前请求期间保留所有数据。 If you create IUser as a singleton for whole application, it'll keep data till app pool recycling . 如果将IUser创建为整个应用程序的单例,它将保留数据直到应用程序池回收为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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