加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Lifestyles

发布时间:2020-12-13 23:30:25 所属栏目:Linux 来源:网络整理
导读:Lifestyle controls in what scope instances are reused,and when to release them(that is do all the necessary clean up steps and then let it go for the Garbage Collector to destroy). Lifestyle控制实例在什么作用域(scope)内被重用,在什么时

Lifestyle controls in what scope instances are reused,and when to release them(that is do all the necessary clean up steps and then let it go for the Garbage Collector to destroy).

Lifestyle控制实例在什么作用域(scope)内被重用,在什么时候被释放(清理的必要步骤,清理之后被GC回收)

常见的生命方式??Standard Lifestyle :the common ones?

Singleton(default style)

在第一次请求时创建,后续请求重复使用。显示调用释放(container.Release(instance))不起作用。只有当容器释放时才会释放singleton的实例

什么样的类设置为单例呢?

  • 无状态的(State-less)组件。如果有状态的组件为单例,多线程情况下需要保证线程安全
  • 组件的状态很大(object have a big state 理解为对象占用内存多)创建多个实例消耗内存

Transient

每次程序需要时都会创建新的实例

trannsient实例需要释放。

如果是这样获取实例(container.Resolve<MyTransientComponent>()) 需要调用container.Release(myTransientInstance) 释放它

Transient components may be tracked by the container 用完之后如果不是放GC不会回收

不常见的生命方式the less common ones?

Scope

using Castle.MicroKernel.Lifestyle;

using (Container.BeginScope()) //extension method
{
? ? ?var one = Container.Resolve<MyScopedComponent>();
? ? ?var two = Container.Resolve<MyScopedComponent>();
? ? ?Assert.AreSame(one,two);

} // releases the instance

the scope is bound to the?CallContext. 注意在多线程情况下子操作应该在scope范围内完成,否则出现空引用错误。

Custom Scope

Implementing custom scope

Bound

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读