DotLee

开源项目dropthings的业务容器BusinessContainer

开源项目dropthings的业务容器BusinessContainer

系统版本 Dropthings-2.1
Dropthings.Business.Container.BusinessContainer
是依赖注入(Dependency Injection)容器,
使用Unity 1.2版本
//Unity是微软Patterns & Practices团队所开发的一个轻量级的,可扩展的依赖注入(Dependency Injection)容器
//http://unity.codeplex.com
log4net

一、注入工作流运行环境WorkflowRuntime的实例
    // Create a global workflow runtime and store in Application context.此两行代码在Global.asax中
        System.Workflow.Runtime.WorkflowRuntime runtime = Dropthings.Business.Workflows.WorkflowHelper.CreateDefaultRuntime();
        Application[APPLICATION_WORKFLOW_RUNTIME_KEY] = runtime;
        保证容器生命周期内,只有一个工作流运行环境在执行。
       
public static void RegisterInstanceExternalLifetime<TInterface>(TInterface instance)
        {
           //RegisterInstance注册实例
            _container.RegisterInstance<TInterface>(instance, new ExternallyControlledLifetimeManager());
        }
       
   2个生命周期管理类:
  ContainerControlledLifetimeManager:在Container生命中期内,只有一个实例。
  ExternallyControlledLifetimeManager:同上。唯一的不同,容器一定要有强类型的引用,否则,会被gc回收。 
  还有一个 生命周期管理类在下面介绍。
  这3个生命周期管理类,都继承自:LifetimeManager;
       
二、注入IWorkflowHelper的实例WorkflowHelper,workflowHelper的生命周期有PerThreadLifetimeManager管理
    在同个线程中只有同一个workflowHelper对象实例,不同的线程,有不同的实例。
    参数数组params:必须是函数定义中的最后一个参数,数组中的参数的个数没有限制,也可以没有。
        public static void RegisterTypePerThread<TFrom, TTo>(params InjectionMember[] injectionMembers)
            where TTo : TFrom
        {       
            //RegisterType函数主要用来注册类型,
            //PerThreadLifetimeManager控制注册类型的生命周期
            //InjectionMember注入的成员类型,依赖命名空间Microsoft.Practices.ObjectBuilder2的业务规则列表接口IPolicyList,
            //本项目中没有指定业务规则列表
           
            _container.RegisterType<TFrom, TTo>(new PerThreadLifetimeManager(), injectionMembers);
        }

 
 通过上面的2点,说明只有一个asp.net工作流运行环境(并行环境)线程,他的生命周期由所在的容器管理,
                当这个容器销毁或没有强类型引用时,有gc回收或 Dropthings.Business.Container.ObjectContainer.Dispose()销毁;               
               
               
                 而工作流线程却有多个,同一工作流线程,只有同一工作流实例,并使用同样的工作流环境,
                 他的生命周期也由所在的容器管理, 当这个容器销毁时,有Dropthings.Business.Container.ObjectContainer.Dispose()销毁


 
 三、其他的方法:
 Resolve方法或者ResolveAll方法:获取注入的类型的实例。
 

posted on 2009-05-25 18:25  DotLee  阅读(765)  评论(2编辑  收藏  举报