zoukankan      html  css  js  c++  java
  • unity LifetimeManager 实现session或者请求级别的生命周期的二种方式

    一种方式使用同一个unitycontainer 不同的LifetimeManager:

    I need one instance of an object to be created per HttpRequest in my ASP.NET MVC application. I think I can accomplish this with a custom LifetimeManager that stores created instances inside of HttpContext.Current.Items. If my class below works, how does one deal with objects created by Unity that need to have Dispose called? In my case, I have a service layer that takes a database context. At the end of my HttpRequest, Dispose should really be called on that object, yet I'm allowing Unity to handle the creation of it, and am storing it in HttpContext.Current.Items. Any thoughts on this issue?

    Here's my HttpRequestLifetimeManager:
    public class HttpRequestLifetimeManager : LifetimeManager
        {
            private readonly Guid key;

            public HttpRequestLifetimeManager()
            {
                key = Guid.NewGuid();
            }

            public override object GetValue()
            {
                return HttpContext.Current.Items[key];
            }

            public override void SetValue(object newValue)
            {
                HttpContext.Current.Items[key] = newValue;
            }

            public override void RemoveValue()
            {
                HttpContext.Current.Items.Remove(key);
            }
        }
    In Global.asax under Application_Start()
    // Any request for a SystemEntityDatabase data context will provide one instance per HttpRequest
    container.RegisterType<SystemEntityDatabase, SystemEntityDatabase>(new HttpRequestLifetimeManager());

    另外一种指定注册类型为ContainerControlledLifetimeManager,而container使用session或者item的生命周期:

    <unity>
       <typeAliases>
         <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
       </typeAliases>
       <containers>
         <container name="system">
           <types>
             <type name="a"  type="Tjb.Facade.ILogger,Tjb.Facade" mapTo="Tjb.FacadeProvider.SystemLogger,Tjb.FacadeProvider" />
           </types>
           <!--<instances>
             <add name="session" type="System.Boolean" value="false"/>
             <add name="request" type="System.Boolean" value="false"/>
           </instances>-->
         </container>
         <container name="session">
           <types>
             <type type="Tjb.Facade.ILogger,Tjb.Facade" mapTo="Tjb.FacadeProvider.SessionLogger,Tjb.FacadeProvider"  >
               <lifetime type="singleton" />
             </type>
           </types>
         </container>
         <container name="request">
           <types>
             <type type="Tjb.Facade.ILogger,Tjb.Facade" mapTo="Tjb.FacadeProvider.ItemLogger,Tjb.FacadeProvider" >
               <lifetime type="singleton" />
             </type>
           </types>
         </container>
       </containers>
    </unity>

    引用某达人(ctavares)所说:

    If you want to combine dispose semantics with per-request, an easier approach would be to use a per-request child container instead. Configure your main container in Application_Start. In BeginRequest, call container.CreateChildContainer();. Configure your per-request objects in the child container as ContainerControlledLifetime (you don't need the custom lifetime manager). Then, in EndRequest call Dispose on the child container. Then it'll take all the per-request stuff with it.

    不知道这2种方式哪个更好?希望有高人指教。

  • 相关阅读:
    android实现 服务器功能
    jQuery部分源码帮助理解
    jquery 2.0.3代码结构
    Mac下配置JAVA_HOME
    用户环境变量
    你的apk有多不安全
    JadClipse eclipse反编译插件
    vim 使用笔记
    Makefile简易模板
    Linux watch 监控系统状态
  • 原文地址:https://www.cnblogs.com/68681395/p/1527271.html
Copyright © 2011-2022 走看看