• 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()),这样的生命周期与容器的生命周期一致。

  • 相关阅读:
    git技能
    iOS 命名规则
    iOS crash 报错类型
    iOS 面试相关
    【转】app后端如何选择合适的数据库产品
    App的token机制
    【转】Spring注解详解
    spring mvc ModelAndView 404的原因
    ibatis 环境搭建(1)
    Android中的Selector的用法
  • 原文地址:https://www.cnblogs.com/Kazaf/p/2095372.html
Copyright © 2020-2023  润新知