zoukankan      html  css  js  c++  java
  • Unity IOC 生命周期(MVC3.0)

    1:默认不用LifetimeManager

        RegisterType<ICategoryRepository, CategoryRepository>(), 生命周期是每次都会创建一个实例。

    2:HttpContext.Item级别的

       RegisterType<ICategoryRepository, CategoryRepository>(new HttpContextLifetimeManager<ICategoryRepository>()),生命周期是HttpContext.Item级别。就是说

       每一次请求只会创建一个实例。

     public class HttpContextLifetimeManager<T> : LifetimeManager, IDisposable
        {
            public override object GetValue()
            {
                return HttpContext.Current.Items[typeof(T).AssemblyQualifiedName];
            }
            public override void RemoveValue()
            {
                HttpContext.Current.Items.Remove(typeof(T).AssemblyQualifiedName);
            }
            public override void SetValue(object newValue)
            {
                HttpContext.Current.Items[typeof(T).AssemblyQualifiedName] = newValue;
            }
            public void Dispose()
            {
                RemoveValue();
            }
        }
    

    3: 容器级别。

      RegisterType<ICategoryRepository, CategoryRepository>(new ContainerControlledLifetimeManager()),这样的生命周期与容器的生命周期一致。

  • 相关阅读:
    CF Round433 B. Jury Meeting
    CF Round433 C. Planning
    繁忙的都市
    联络员
    组合数模板
    Rinne Loves Xor
    [SDOI2016]齿轮
    水题(water)
    Music Problem
    小H和游戏
  • 原文地址:https://www.cnblogs.com/Kazaf/p/2095372.html
Copyright © 2011-2022 走看看