Exemplo n.º 1
0
        public static T GetService <T>()
        {
            var temp = _container.Resolve <T>();

            if (!(temp is IAop))
            {
                return(temp);
            }

            var enableLogFunctionData = false;

            bool.TryParse(ConfigurationManager.AppSettings["EnableLogFunctionData"], out enableLogFunctionData);

            var enableCustomCache = false;

            bool.TryParse(ConfigurationManager.AppSettings["EnableCustomCache"], out enableCustomCache);

            if (!enableLogFunctionData && !enableCustomCache)
            {
                return(temp);
            }

            var tempProxy    = new AopProxy <T>(temp);
            var proxyService = (T)tempProxy.GetTransparentProxy();

            return(proxyService);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configFile">配置文件</param>
        /// <param name="isEnabledAop">是否使用Aop</param>
        public IocContainer(string configFile, bool isEnabledAop)
        {
            if (!string.IsNullOrEmpty(configFile))
            {
                string filePath = PathUtils.GetFileFullPath(configFile);
                if (!File.Exists(filePath))
                {
                    throw new FileNotFoundException(configFile + " not found!", configFile);
                }
            }

            this.m_defaultAssembly = null;
            this.m_rwLock          = new ReadWriteLock();
            this.m_dicType         = new Dictionary <Type, List <Type> >();

            if (isEnabledAop)
            {
                this.m_aopProxy = new AopProxy();
            }

            if (!string.IsNullOrEmpty(configFile))
            {
                this.Load(configFile);
            }
        }
Exemplo n.º 3
0
        public override MarshalByRefObject CreateInstance(Type serverType)
        {
            var target       = base.CreateInstance(serverType);
            var aopRealProxy = new AopProxy(serverType, target);

            aopRealProxy.InjectInterception(_interception, _enablePreInterception, _enableAfterInterception,
                                            _enableArroundInterception);
            return(aopRealProxy.GetTransparentProxy() as MarshalByRefObject);
        }
Exemplo n.º 4
0
        public async Task Always_ShouldInvokeMethodsCorrectly()
        {
            // arrange
            var performedActions = new List <string>();
            var proxy            = AopProxy.Create <ITest>(new Test(performedActions),
                                                           GetHandler(performedActions, 1), GetHandler(performedActions, 2));

            // act
            var strMethodResult     = proxy.StrMethod("t1");
            var taskIntMethodResult = await proxy.TaskIntMethod(5);

            await proxy.TaskVoidMethod();

            proxy.VoidMethod();
            var TaskIntMethodWithoutDelayResult = await proxy.TaskIntMethodWithoutDelay(6);

            // assert
            strMethodResult.Should().Be("t1 h2 h1");
            taskIntMethodResult.Should().Be(115);
            TaskIntMethodWithoutDelayResult.Should().Be(116);

            performedActions.Should().BeEquivalentTo(new List <string>
            {
                "StrMethod before 1 with args [\"t1\"]",
                "StrMethod before 2 with args [\"t1\"]",
                "StrMethod",
                "StrMethod after 2 with res t1",
                "StrMethod after 1 with res t1 h2",

                "TaskIntMethod before 1 with args [5]",
                "TaskIntMethod before 2 with args [5]",
                "TaskIntMethod",
                "TaskIntMethod after delay",
                "TaskIntMethod after 2 with res 5",
                "TaskIntMethod after 1 with res 105",

                "TaskVoidMethod before 1 with args []",
                "TaskVoidMethod before 2 with args []",
                "TaskVoidMethod",
                "TaskVoidMethod after delay",
                "TaskVoidMethod after 2 with res null",
                "TaskVoidMethod after 1 with res null",

                "VoidMethod before 1 with args []",
                "VoidMethod before 2 with args []",
                "VoidMethod",
                "VoidMethod after 2 with res null",
                "VoidMethod after 1 with res null",

                "TaskIntMethodWithoutDelay before 1 with args [6]",
                "TaskIntMethodWithoutDelay before 2 with args [6]",
                "TaskIntMethodWithoutDelay",
                "TaskIntMethodWithoutDelay after 2 with res 6",
                "TaskIntMethodWithoutDelay after 1 with res 106",
            }, o => o.WithStrictOrdering());
        }
Exemplo n.º 5
0
        public void Generate_BaseAndCurrent_Done()
        {
            var document = GetDocument(GetFileName(nameof(IAppConfigPeriodicRefreshHandler)));
            var interfaceDeclarationSyntax = GetInterface(document, nameof(IAppConfigPeriodicRefreshHandler));

            var actualClassDeclarationSyntax = new AopProxy().Generate(
                interfaceDeclarationSyntax,
                document.GetSemanticModelAsync().Result);

            var expectedTree = GetClass(Resources.AppConfigPeriodicRefreshHandlerAopProxy);

            Assert.IsTrue(
                expectedTree.IsEquivalentTo(actualClassDeclarationSyntax),
                GetDifference(expectedTree, actualClassDeclarationSyntax));
        }
Exemplo n.º 6
0
        public void Generate_BaseInterface_Done()
        {
            var document = GetDocument(GetFileName(nameof(IIgnoredInterface)));
            var interfaceDeclarationSyntax = GetInterface(document, nameof(IIgnoredInterface));

            var actualClassDeclarationSyntax = new AopProxy().Generate(
                interfaceDeclarationSyntax,
                document.GetSemanticModelAsync().Result);

            var expectedTree = GetClass(Resources.IgnoredInterfaceAopProxy);

            Assert.IsTrue(
                expectedTree.IsEquivalentTo(actualClassDeclarationSyntax, false),
                GetDifference(expectedTree, actualClassDeclarationSyntax));
        }
Exemplo n.º 7
0
 private T WrapIfNeeded <T>(T obj, string name)
 {
     return(_wrappers.Count > 0
         ? AopProxy.Create(obj, name, _wrappers.Select(w => (AopProxy.MethodCallHandler)w.HandleMethodCall).ToArray())
         : obj);
 }
Exemplo n.º 8
0
        private T AddCaching <T>(T obj)
        {
            var cachingHelper = new CachingHelper();

            return(AopProxy.Create(obj, cachingHelper.HandleMethodCall));
        }