コード例 #1
0
        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            //execute the next chain
            var result = getNext().Invoke(input, getNext);

            //if the method name is Create
            if (input.MethodBase.Name == "Create")
            {
                //let's get the generic argument for Create i.e. T in Create<T>
                var interfaceType = input.MethodBase.GetGenericArguments()[0];

                //set up ourlifetime to be controlled by the target object's disposed event
                var lifeTime = new ExternallyControlledLifetimeManager();
                var target = (IMockRepository)input.Target;
                target.Disposed += (s, e) => lifeTime.RemoveValue();

                //let's get our mock object
                var obj = result.ReturnValue as Mock;

                //let's register our mock with the container.
                if (obj != null && obj.Object != null)
                {
                    _container.RegisterInstance(interfaceType, obj.Object, lifeTime);
                }
            }

            return result;
        }
コード例 #2
0
        public LifetimeManager CreateLifeTimeManager(LifeTimeManagerType type)
        {
            LifetimeManager result = null;
            switch (type)
            {
                case LifeTimeManagerType.ContainerControlled:
                    result = new ContainerControlledLifetimeManager();
                    break;
                case LifeTimeManagerType.HierarchicalLifetime:
                    result = new HierarchicalLifetimeManager();
                    break;
                case LifeTimeManagerType.PerResolveLifetime:
                    result = new PerResolveLifetimeManager();
                    break;
                case LifeTimeManagerType.ExternallyControlledLifetime:
                    result = new ExternallyControlledLifetimeManager();
                    break;
                case LifeTimeManagerType.PerThreadLifetime:
                    result = new PerThreadLifetimeManager();
                    break;
                case LifeTimeManagerType.TransientLifetime:
                    result = new TransientLifetimeManager();
                    break;
            }

            return result;
        }
        public void WhenResolvingThroughTheExtensionWithAName_ThenTransientPoliciesAreUsed()
        {
            var singleton = new object();

            var transientLifetimeManager = new ExternallyControlledLifetimeManager();
            transientLifetimeManager.SetValue(singleton);

            object instance =
                this.container.Configure<TransientPolicyBuildUpExtension>()
                    .BuildUp(
                        typeof(object),
                        null,
                        "name",
                        new TestTransientLifetimePolicy { LifetimeManager = transientLifetimeManager });

            Assert.AreSame(singleton, instance);
        }
コード例 #4
0
        public void StrategyProperlyWiresEvents()
        {
            MockBuilderContext context = CreateContext();
            NamedTypeBuildKey buildKey = NamedTypeBuildKey.Make<ClipboardManager>();

            EventBroker broker = new EventBroker();
            var brokerLifetime = new ExternallyControlledLifetimeManager();
            brokerLifetime.SetValue(broker);
            context.Policies.Set<ILifetimePolicy>(brokerLifetime, NamedTypeBuildKey.Make<EventBroker>());

            EventBrokerInfoPolicy policy = new EventBrokerInfoPolicy();
            policy.AddPublication("cut", "Cut");
            policy.AddPublication("copy", "Copy");
            policy.AddPublication("paste", "Paste");

            policy.AddSubscription("copy", typeof(ClipboardManager).GetMethod("OnCopy"));
            policy.AddSubscription("clipboard data available",
                                   typeof(ClipboardManager).GetMethod("OnClipboardDataAvailable"));

            context.Policies.Set<IEventBrokerInfoPolicy>(policy, buildKey);

            ClipboardManager existing = new ClipboardManager();

            context.ExecuteBuildUp(buildKey, existing);

            List<string> registeredEvents = new List<string>(broker.RegisteredEvents);
            registeredEvents.Sort();

            List<string> expectedEvents = new List<string>(new string[]
            {
                "cut",
                "copy",
                "paste",
                "clipboard data available"
            });
            expectedEvents.Sort();

            CollectionAssert.AreEqual(expectedEvents, registeredEvents);
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: huanlin/di-book-support
        static void TestExternallyControlledLifetime()
        {
            Console.WriteLine("Test ExternallyControlledLifetimeManager");
            LifetimeTest.ResetCounter();
            using (var container = new UnityContainer())
            {
                var lifeManager = new ExternallyControlledLifetimeManager();
                container.RegisterType<ILifetimeTest, LifetimeTest>(lifeManager);

                var obj1 = container.Resolve<ILifetimeTest>();
                var obj2 = container.Resolve<ILifetimeTest>();
                obj1.Dispose();
                LifetimeTest.PrintCounter();
            }
            LifetimeTest.PrintCounter();
        }
コード例 #6
0
ファイル: DependencyFactory.cs プロジェクト: 493409332/MT.Orm
        //   DependencyFactory.RegisterDependency(ProjectType.Wcf, "MT.Complex");
        public static void RegisterDependency(ProjectType projecttype, string projectname)
        {
            string DllPath = string.Empty;

            switch ( projecttype )
            {
                case ProjectType.Web:
                    DllPath = AppDomain.CurrentDomain.BaseDirectory + "\\bin\\" + projectname + ".dll";
                    break;
                case ProjectType.Winfom:
                case ProjectType.WPF:
                case ProjectType.Wcf:
                case ProjectType.Test:
                    DllPath = AppDomain.CurrentDomain.BaseDirectory + "\\" + projectname + ".dll";
                    break;

            }
            if ( DllPath == null || DllPath.Length == 0 )
            {
                throw new Exception("无法解析项目DLL");
            }
            var typeList =
                  Assembly.LoadFrom(DllPath).GetTypes().Where(t => t.Namespace != null && t.Namespace.Contains("Realization") && t.IsInterface == false && t.IsAbstract == false);

            var LifetimeManagerRegisterlist =
             typeList.Where(t =>
             {
                 return t.GetInterfaces().Intersect(ILifetimeManagerRegisterList).Count() > 0;
             });

            foreach ( var t in LifetimeManagerRegisterlist )
            {
                var InterfaceList = t.GetInterfaces().Where(p =>
                { 
                    return !ILifetimeManagerRegisterList.Contains(p) && p.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false) != null;
                });
                LifetimeManager lifetimemanager = new TransientLifetimeManager();
                var intertype = t.GetInterfaces().Intersect(ILifetimeManagerRegisterList).First();
                switch ( intertype.Name )
                {
                    case "IContainerControlledLifetimeManagerRegister":
                        lifetimemanager = new ContainerControlledLifetimeManager();
                        break;
                    case "IHierarchicalLifetimeManagerRegister":
                        lifetimemanager = new HierarchicalLifetimeManager();
                        break;
                    case "IExternallyControlledLifetimeManagerRegister":
                        lifetimemanager = new ExternallyControlledLifetimeManager();
                        break;
                    case "IPerThreadLifetimeManagerRegister":
                        lifetimemanager = new PerThreadLifetimeManager();
                        break;
                    case "IPerResolveLifetimeManagerRegister":
                        lifetimemanager = new PerResolveLifetimeManager();
                        break;
                }

                foreach ( var iType in InterfaceList )
                {
                    ICOConfigAttribute ds = (ICOConfigAttribute) t.GetCustomAttribute(typeof(ICOConfigAttribute), false);
                    ICO_AOPEnableAttribute ia = (ICO_AOPEnableAttribute) iType.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false);
                     

                    if ( ia.AOPEnable )
                    {
                        var generator = new DynamicProxyGenerator(t, iType);
                        Type type = generator.GenerateType();

                      //  Type type = typeof(TransientLifetimeManager);
                        DependencyUnityContainer.Current.RegisterType(iType, type, ds.Description, lifetimemanager);
                    }
                    else
                    {
                        DependencyUnityContainer.Current.RegisterType(iType, t, ds.Description, lifetimemanager);
                    } 

                }
            }



        }