Example #1
0
        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            Type   serviceType     = instanceContext.Host.Description.ServiceType;
            object serviceInstance = Activator.CreateInstance(serviceType);

            serviceInstance = PolicyInjection.Wrap(this._serviceContractType, serviceInstance);
            return(serviceInstance);
            //PolicyInjector policyInjector = null;
            //if (string.IsNullOrEmpty(this._policyInjectorName))
            //{
            //    policyInjector = new PolicyInjectorFactory().Create();
            //}
            //else
            //{
            //    policyInjector = new PolicyInjectorFactory().Create(this._policyInjectorName);
            //}

            //Type serviceType = instanceContext.Host.Description.ServiceType;
            //object serviceInstance = Activator.CreateInstance(serviceType);
            //if (!this._serviceContractType.IsInterface && !serviceType.IsMarshalByRef && policyInjector is RemotingPolicyInjector)
            //{
            //    return serviceInstance;
            //}
            //return policyInjector.Wrap(serviceInstance, this._serviceContractType);
        }
Example #2
0
        /// <summary>
        /// 执行API方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="method"></param>
        /// <returns></returns>
        public T Execute <T>(MethodBase <T> method)
        {
            //将方法在策略中包装
            var m = (MethodBase <T>)PolicyInjection.Wrap(method.GetType(), method);

            return(m.Execute(this));
        }
        static void Main(string[] args)
        {
            //BMIProcessor BMI =  new BMIProcessor();
            //上面這行改為底下這樣
            BMIProcessor BMI = PolicyInjection.Create <BMIProcessor>();

            //其餘程式碼完全不變
            BMI.Height = 170;
            BMI.Weight = 70;

            //計算BMI
            var ret = BMI.Calculate();

            Console.WriteLine($"\nBMI : {ret}");
            Console.ReadKey();

            //測試exception
            BMI.Height = 0;
            BMI.Weight = 0;

            //計算BMI
            ret = BMI.Calculate();

            Console.WriteLine($"\nBMI : {ret}");
            Console.ReadKey();
        }
        public FindOutputManagersResponse FindOutputManagers(FindOutputManagersRequest request)
        {
            IClaimsIdentity identity = (IClaimsIdentity)Thread.CurrentPrincipal.Identity;
            string          upn      = identity.Claims.FindAll(c => { return(c.ClaimType == ClaimTypes.Upn); }).First().Value;

            InitializationActions.FindOutputManagersAction action = PolicyInjection.Create <InitializationActions.FindOutputManagersAction>();

            InitializationEntities.FindOutputManagersParameters parameters = Translators.FindOutputManagersTranslator.TranslateFromServiceToBusiness(request.FindOutputManagersParameters);

            //parameters.UserPrincipalIdentity = upn; /* user@domain */
            //parameters.UserIdentity = upn.Split('@')[0];


            InitializationEntities.FindOutputManagersResult r = action.Execute();

            FindOutputManagersResponse response = new FindOutputManagersResponse();

            response.FindOutputManagerResult = GenericMapper.MapNew <DataContracts.FindOutputManagerResult>(r);

            response.FindOutputManagerResult.OutputManagers =
                GenericMapper.MapListNew <DataContracts.OutputManagerCollection, InitializationEntities.FindOutputManagerResult, DataContracts.OutputManager>(
                    r.OutputManagers, Translators.FindOutputManagersTranslator.TranslateFromBusinessToService);


            return(response);
        }
Example #5
0
        static ApiClient()
        {
            //设置策略注入
            IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
            var injector = new PolicyInjector(configurationSource);

            PolicyInjection.SetPolicyInjector(injector);
        }
Example #6
0
        public void CanCreateWrappedObject()
        {
            SetupContainer("CanCreateWrappedObject");

            Wrappable wrappable = PolicyInjection.Create <Wrappable>();

            Assert.IsNotNull(wrappable);
            Assert.IsTrue(RemotingServices.IsTransparentProxy(wrappable));
        }
Example #7
0
        public void CanCreateWrappedObject()
        {
            IConfigurationSource configurationSource = CreateConfigurationSource("CanCreateWrappedObject");

            Wrappable wrappable = PolicyInjection.Create <Wrappable>(configurationSource);

            Assert.IsNotNull(wrappable);
            Assert.IsTrue(RemotingServices.IsTransparentProxy(wrappable));
        }
Example #8
0
        static APIClient()
        {
            IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
            var injector = new PolicyInjector(configurationSource);

            try {
                PolicyInjection.SetPolicyInjector(injector);
            } catch { }
        }
Example #9
0
        // 定义单例模式将PolicyInjection.Create<UserOperation>()产生的这个对象传出去,这样就避免了在调用处写这些东西
        public static TestTool GetInstance()
        {
            if (oUserOpertion == null)
            {
                oUserOpertion = PolicyInjection.Create <TestTool>();
            }

            return(oUserOpertion);
        }
Example #10
0
        //定义单例模式将PolicyInjection.Create<UserOperation>()产生的这个对象传出去,这样就避免了在调用处写这些东西
        public static UserOperation GetInstance()
        {
            if (oUserOpertion == null)
            {
                oUserOpertion = PolicyInjection.Create <UserOperation>();
            }

            return(oUserOpertion);
        }
Example #11
0
        public void StaticFacadeUsesTheCurrentServiceLocator()
        {
            GlobalCountCallHandler.Calls.Clear();

            var interceptionConfigurationSource = new DictionaryConfigurationSource();

            interceptionConfigurationSource.Add(
                PolicyInjectionSettings.SectionName,
                new PolicyInjectionSettings
            {
                Policies =
                {
                    new PolicyData("policy")
                    {
                        MatchingRules =
                        {
                            new CustomMatchingRuleData("always", typeof(AlwaysMatchingRule))
                        },
                        Handlers =
                        {
                            new CustomCallHandlerData("count", typeof(GlobalCountCallHandler))
                            {
                                Attributes ={                  { "callhandler","count"  } }
                            }
                        }
                    }
                }
            });
            var interceptionLocator = EnterpriseLibraryContainer.CreateDefaultContainer(interceptionConfigurationSource);

            var noInterceptionLocator = EnterpriseLibraryContainer.CreateDefaultContainer(new DictionaryConfigurationSource());

            try
            {
                EnterpriseLibraryContainer.Current = interceptionLocator;

                var interceptedWrappable = PolicyInjection.Create <Wrappable>();
                interceptedWrappable.Method();
                Assert.AreEqual(1, GlobalCountCallHandler.Calls.Count);
                GlobalCountCallHandler.Calls.Clear();


                EnterpriseLibraryContainer.Current = noInterceptionLocator;

                var nonInterceptedWrappable = PolicyInjection.Create <Wrappable>();
                nonInterceptedWrappable.Method();
                Assert.AreEqual(0, GlobalCountCallHandler.Calls.Count);
            }
            finally
            {
                EnterpriseLibraryContainer.Current = null;
                GlobalCountCallHandler.Calls.Clear();
                interceptionLocator.Dispose();
                noInterceptionLocator.Dispose();
            }
        }
Example #12
0
        public void CanInterceptCallsToMBROOverInterface()
        {
            SetupContainer("CanInterceptCallsToMBROOverInterface");

            Wrappable wrappable = PolicyInjection.Create <Wrappable>();

            ((Interface)wrappable).Method();

            Assert.AreEqual(1, GlobalCountCallHandler.Calls["CanInterceptCallsToMBROOverInterface"]);
        }
Example #13
0
        public GetSystemTimeResponse GetSystemTime()
        {
            GetSystemTimeAction action = PolicyInjection.Create <GetSystemTimeAction>();

            GetSystemTimeResponse response = new GetSystemTimeResponse();

            response.GetSystemTimeResult = GetSystemTimeResultTranslator.TranslateFromBusinessToService(action.Execute());

            return(response);
        }
 public OutputHandlerServiceAdapter()
 {
     lock (_createOutputActionLock)
     {
         if (_createOutputAction == null)
         {
             _createOutputAction = PolicyInjection.Create <CreateOutputAction>();
         }
     }
 }
        /// <summary>
        /// GetInstance is used to create an instance of ProjectService wrapped by the PolicyInjection framework.
        /// </summary>
        /// <param name="instanceContext"></param>
        /// <param name="message"></param>
        /// <returns>Contract.IProjectService</returns>
        public object GetInstance(System.ServiceModel.InstanceContext instanceContext, System.ServiceModel.Channels.Message message)
        {
            // Start client connection to database.
            var context = EntityContext.CreateInstance(_connectionStringName);

            context.Open();
            // Return indexer service.
            return(PolicyInjection.Wrap <Contract.IProjectService>(
                       new ProjectService(context, context.Identity)));
        }
Example #16
0
        public void CanInterceptWrappedObject()
        {
            SetupContainer("CanCreateWrappedObject");

            Wrappable wrappable = PolicyInjection.Create <Wrappable>();

            wrappable.Method2();

            Assert.AreEqual(1, GlobalCountCallHandler.Calls["CanCreateWrappedObject"]);
        }
Example #17
0
        public void CanInterceptCallFromBaseOfWrappedInterface()
        {
            SetupContainer("CanInterceptCallFromBaseOfWrappedInterface");

            Interface wrappedOverInterface = PolicyInjection.Create <WrappableThroughInterface, Interface>();

            wrappedOverInterface.Method3();

            Assert.AreEqual(1, GlobalCountCallHandler.Calls["CanInterceptCallFromBaseOfWrappedInterface"]);
        }
Example #18
0
        public void CanCreateWrappedObjectOverInterfaceWithNonGenericMethods()
        {
            SetupContainer("CanCreateWrappedObjectOverInterface");

            Interface wrappedOverInterface = (Interface)PolicyInjection.Create(typeof(WrappableThroughInterface), typeof(Interface));

            wrappedOverInterface.Method();

            Assert.AreEqual(1, GlobalCountCallHandler.Calls["CanCreateWrappedObjectOverInterface"]);
        }
Example #19
0
        public void InterfaceImplementationsOnDerivedClassesAreWrappedMultipleTimes()
        {
            SetupContainer("InterfaceImplementationsOnDerivedClassesAreWrappedMultipleTimes");

            DerivedWrappable wrappable = PolicyInjection.Create <DerivedWrappable>();

            wrappable.Method();

            Assert.AreEqual(1, GlobalCountCallHandler.Calls["InterfaceImplementationsOnDerivedClassesAreWrappedMultipleTimes"]);
        }
Example #20
0
        public void CanInterceptCallsToDerivedOfMBRO()
        {
            SetupContainer("CanInterceptCallsToDerivedOfMBRO");

            DerivedWrappable wrappable = PolicyInjection.Create <DerivedWrappable>();

            wrappable.Method2();

            Assert.AreEqual(1, GlobalCountCallHandler.Calls["CanInterceptCallsToDerivedOfMBRO"]);
        }
Example #21
0
        public void CanInterceptWrappedObjectWithNonGenericMethods()
        {
            SetupContainer("CanCreateWrappedObject");

            Wrappable wrappable = (Wrappable)PolicyInjection.Create(typeof(Wrappable));

            wrappable.Method2();

            Assert.AreEqual(1, GlobalCountCallHandler.Calls["CanCreateWrappedObject"]);
        }
Example #22
0
        /// <summary>
        /// This function demostrates EntLib Policy Injection usage. No actual use.
        /// 关于使用EntLib的PolicyInjection的几点说明[重要总结]
        /// # 需要添加Unity.*.dll的引用
        /// # 添加Reference时,确保EnterpriseLibrary.*.dll和Unity.*.dll是来自同一份EntLib安装的文件,因为前者对后者有依赖,版本信息必须严格匹配
        /// </summary>
        static partial void InitPolicyInjection()
        {
            // PolicyInjection: A static facade class that provides the main entry point into the Policy Injection Application Block. Methods on this class create intercepted objects, or wrap existing instances with interceptors.
            //
            // The interception handler is defined in Web.config
            var         obj   = new ClinicalPathwayInstance();
            IComparable proxy = PolicyInjection.Wrap <IComparable>(obj); //or PolicyInjection.Create<ClinicalPathwayInstance,IComparable>

            proxy.CompareTo(null);
        }
Example #23
0
        protected void Page_Load(object sender, EventArgs e)
        {
            Employee emp = PolicyInjection.Create <Employee>();

            emp.Name = "Lele";

            emp.Work();

            Response.Write(emp);
        }
Example #24
0
        static void Main()
        {
            //PolicyInjection.SetPolicyInjector(new PolicyInjector());
            Employee emp = PolicyInjection.Create <Employee>();

            emp.Name = "Lele";

            emp.Work();
            Console.WriteLine(emp);
            Console.Read();
        }
        public LogonResponse Logon(LogonRequest request)
        {
            AuthenticationActions.LogonAction action = PolicyInjection.Create <AuthenticationActions.LogonAction>();

            AuthenticationEntities.LogonResult resultParams = action.Execute(Translators.LogonTranslator.TranslateFromServiceToBusiness(request.LogonParameters));
            LogonResponse response = new LogonResponse();

            response.LogonResult = Translators.LogonTranslator.TranslateFromBusinessToService(resultParams);

            return(response);
        }
Example #26
0
        public object GetInstance(System.ServiceModel.InstanceContext instanceContext, System.ServiceModel.Channels.Message message)
        {
            Type   serviceType     = instanceContext.Host.Description.ServiceType;
            object serviceInstance = Activator.CreateInstance(serviceType);

            if (!this._serviceContractType.IsInterface && !serviceType.IsMarshalByRef && string.IsNullOrEmpty(this._policyInjectorName))
            {
                return(serviceInstance);
            }

            return(PolicyInjection.Wrap(this._serviceContractType, serviceInstance));
        }
Example #27
0
        public void CanInterceptWrappedObject()
        {
            GlobalCountCallHandler.Calls.Clear();

            IConfigurationSource configurationSource = CreateConfigurationSource("CanCreateWrappedObject");

            Wrappable wrappable = PolicyInjection.Create <Wrappable>(configurationSource);

            wrappable.Method2();

            Assert.AreEqual(1, GlobalCountCallHandler.Calls["CanCreateWrappedObject"]);
        }
Example #28
0
        public void CanInterceptCallsToMBROOverInterface()
        {
            GlobalCountCallHandler.Calls.Clear();

            IConfigurationSource configurationSource = CreateConfigurationSource("CanInterceptCallsToMBROOverInterface");

            Wrappable wrappable = PolicyInjection.Create <Wrappable>(configurationSource);

            ((Interface)wrappable).Method();

            Assert.AreEqual(1, GlobalCountCallHandler.Calls["CanInterceptCallsToMBROOverInterface"]);
        }
Example #29
0
        public void InterfaceImplementationsOnDerivedClassesAreWrappedMultipleTimes()
        {
            GlobalCountCallHandler.Calls.Clear();

            IConfigurationSource configurationSource = CreateConfigurationSource("InterfaceImplementationsOnDerivedClassesAreWrappedMultipleTimes");

            DerivedWrappable wrappable = PolicyInjection.Create <DerivedWrappable>(configurationSource);

            wrappable.Method();

            Assert.AreEqual(1, GlobalCountCallHandler.Calls["InterfaceImplementationsOnDerivedClassesAreWrappedMultipleTimes"]);
        }
Example #30
0
        public void CanInterceptCallsToDerivedOfMBRO()
        {
            GlobalCountCallHandler.Calls.Clear();

            IConfigurationSource configurationSource = CreateConfigurationSource("CanInterceptCallsToDerivedOfMBRO");

            DerivedWrappable wrappable = PolicyInjection.Create <DerivedWrappable>(configurationSource);

            wrappable.Method2();

            Assert.AreEqual(1, GlobalCountCallHandler.Calls["CanInterceptCallsToDerivedOfMBRO"]);
        }