Exemplo n.º 1
0
        public void PostProcess(IServiceRequestResult result)
        {
            // Ignore any null results
            if (result.ActualResult == null)
            {
                return;
            }

            // The service type must match the current service
            if (result.ServiceType != typeof(TService))
            {
                return;
            }

            // The service names must be equal
            if (result.ServiceName != _context.ServiceName)
            {
                return;
            }

            var service = (TService)result.ActualResult;
            IServiceContainer container = result.Container;
            Action <IServiceContainer, TService> action = _context.Action;

            // Execute the action associated with the
            // context
            action(container, service);
        }
Exemplo n.º 2
0
        public void PostProcess(IServiceRequestResult result)
        {
            var instance = result.ActualResult;
            // instance cannot be resolved (e.g. not registered in container)
            if (instance == null)
            {
                return;
            }
            var instanceTypeName = instance.GetType().FullName;

            // Ignore any LinFu factories or Snap-specific instances.
            if (instanceTypeName.Contains("LinFu.") || instanceTypeName == "Snap.AspectConfiguration"
                || instanceTypeName == "Snap.IMasterProxy" || instanceTypeName == "Snap.MasterProxy")
            {
                return;
            }

            // inteceptors could not be intercepted too, thus skip the code below
            if(instance is IInterceptor)
            {
                return;
            }

            var proxy = result.Container.GetService<IMasterProxy>();

            if (!instance.IsDecorated(proxy.Configuration))
            {
                return;
            }

            result.ActualResult = _proxyFactory.CreateProxy(instance, proxy);
        }
Exemplo n.º 3
0
        public void PostProcess(IServiceRequestResult result)
        {
            var instance = result.ActualResult;

            // instance cannot be resolved (e.g. not registered in container)
            if (instance == null)
            {
                return;
            }
            var instanceTypeName = instance.GetType().FullName;

            // Ignore any LinFu factories or Snap-specific instances.
            if (instanceTypeName.Contains("LinFu.") || instanceTypeName == "Snap.AspectConfiguration" ||
                instanceTypeName == "Snap.IMasterProxy" || instanceTypeName == "Snap.MasterProxy")
            {
                return;
            }

            // inteceptors could not be intercepted too, thus skip the code below
            if (instance is IInterceptor)
            {
                return;
            }

            var proxy = result.Container.GetService <IMasterProxy>();

            if (!instance.IsDecorated(proxy.Configuration))
            {
                return;
            }

            result.ActualResult = _proxyFactory.CreateProxy(instance, proxy);
        }
Exemplo n.º 4
0
        public void PostProcess(IServiceRequestResult result)
        {
            var instance = result.ActualResult;
            var instanceTypeName = instance.GetType().FullName;

            // Ignore any LinFu factories or Snap-specific instances.
            if (instanceTypeName.Contains("LinFu.") || instanceTypeName == "Snap.AspectConfiguration"
                || instanceTypeName == "Snap.IMasterProxy" || instanceTypeName == "Snap.MasterProxy")
            {
                return;
            }

            var proxy = result.Container.GetService<IMasterProxy>();

            if (!instance.IsDecorated(proxy.Configuration))
            {
                return;
            }

            var pseudoList = new IInterceptor[proxy.Configuration.Interceptors.Count];
            pseudoList[0] = proxy;

            for (var i = 1; i < pseudoList.Length; i++)
            {
                pseudoList[i] = new PseudoInterceptor();
            }

            var interfaceTypes = instance.GetType().GetInterfaces();
            var targetInterface =
                interfaceTypes.FirstMatch(proxy.Configuration.Namespaces);

            result.ActualResult = new ProxyGenerator().CreateInterfaceProxyWithTargetInterface(targetInterface, instance, pseudoList);
        }
        /// <summary>
        /// Generates a proxy instance from an existing <see cref="IServiceRequestResult"/> instance.
        /// </summary>
        /// <param name="request">The <see cref="IServiceRequestResult"/> instance that describes the proxy type that must be generated.</param>
        /// <param name="getInterceptor">The <see cref="IInterceptor"/> functor that will create the interceptor which will handle all calls made to the proxy instance.</param>
        /// <returns
        /// >A service proxy.</returns>
        private static object CreateProxyFrom(IServiceRequestResult request,
                                              Func <IServiceRequestResult, IInterceptor> getInterceptor)
        {
            var interceptor = getInterceptor(request);

            var container    = request.Container;
            var proxyFactory =
                container.GetService <IProxyFactory>();

            // The proxy factory must exist
            if (proxyFactory == null)
            {
                return(null);
            }

            // Generate the proxy type
            var proxyType = proxyFactory.CreateProxyType(request.ServiceType,
                                                         new Type[0]);

            // The generated proxy instance
            // must implement IProxy
            var proxy = Activator.CreateInstance(proxyType) as IProxy;

            // Assign the interceptor
            if (proxy != null)
            {
                proxy.Interceptor = interceptor;
            }

            return(proxy);
        }
Exemplo n.º 6
0
        /// <summary>
        /// A method that injects service proxies in place of the actual <see cref="IServiceRequestResult.ActualResult"/>.
        /// </summary>
        /// <param name="result">The <see cref="IServiceRequestResult"/> instance that describes the service request.</param>
        public void PostProcess(IServiceRequestResult result)
        {
            if (!_filterPredicate(result))
            {
                return;
            }

            var container       = result.Container;
            var hasProxyFactory = container.Contains(typeof(IProxyFactory));

            // Inject proxies only if a
            // proxy factory instance is available
            if (!hasProxyFactory)
            {
                return;
            }

            // Sealed types cannot be intercepted
            var serviceType = result.ServiceType;

            if (result.ActualResult != null && serviceType.IsSealed)
            {
                return;
            }

            // Replace the actual result with the proxy itself
            result.ActualResult = _createProxy(result);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Initializes every service that implements
        /// the <see cref="IInitialize{T}"/> interface.
        /// </summary>
        /// <param name="result">The <see cref="IServiceRequestResult"/> instance that contains the service instance to be initialized.</param>
        public void PostProcess(IServiceRequestResult result)
        {
            var originalResult = result.OriginalResult as IInitialize <T>;
            var actualResult   = result.ActualResult as IInitialize <T>;

            var source = _getSource(result);

            // Initialize the original result, if possible
            Initialize(originalResult, source);

            // Initialize the actual result
            Initialize(actualResult, source);
        }
        /// <summary>
        /// A method that passes every request result made
        /// to the list of postprocessors.
        /// </summary>
        /// <param name="result">The <see cref="IServiceRequestResult"/> instance that describes the result of the service request.</param>        
        /// <returns>A <see cref="IServiceRequestResult"/> representing the results returned as a result of the postprocessors.</returns>
        public void PostProcess(IServiceRequestResult result)
        {
            // Let each postprocessor inspect
            // the results and/or modify the
            // returned object instance
            IPostProcessor[] postprocessors = _postProcessors.ToArray();
            foreach (IPostProcessor postProcessor in postprocessors)
            {
                if (postProcessor == null)
                    continue;

                postProcessor.PostProcess(result);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Monitors the <see cref="IServiceContainer"/> for any services that are created and automatically disposes them
        /// once the <see cref="IScope"/> is disposed.
        /// </summary>
        /// <param name="result">The <see cref="IServiceRequestResult"/> that describes the service being instantiated.</param>
        public void PostProcess(IServiceRequestResult result)
        {
            if (_disposed)
                return;

            // Only handle requests from the same thread
            if (_threadId != Thread.CurrentThread.ManagedThreadId)
                return;

            // Ignore any nondisposable instances
            if (result.ActualResult == null || !(result.ActualResult is IDisposable))
                return;

            var disposable = result.ActualResult as IDisposable;
            _disposables.Add(disposable);
        }
Exemplo n.º 10
0
        /// <summary>
        /// A method that passes every request result made
        /// to the list of postprocessors.
        /// </summary>
        /// <param name="result">The <see cref="IServiceRequestResult"/> instance that describes the result of the service request.</param>
        /// <returns>A <see cref="IServiceRequestResult"/> representing the results returned as a result of the postprocessors.</returns>
        public void PostProcess(IServiceRequestResult result)
        {
            // Let each postprocessor inspect
            // the results and/or modify the
            // returned object instance
            IPostProcessor[] postprocessors = _postProcessors.ToArray();
            foreach (IPostProcessor postProcessor in postprocessors)
            {
                if (postProcessor == null)
                {
                    continue;
                }

                postProcessor.PostProcess(result);
            }
        }
Exemplo n.º 11
0
        public void PostProcess(IServiceRequestResult result)
        {
            var instance = result.ActualResult;
            // instance cannot be resolved (e.g. not registered in container)
            if (instance == null)
            {
                return;
            }
            var instanceTypeName = instance.GetType().FullName;

            // Ignore any LinFu factories or Snap-specific instances.
            if (instanceTypeName.Contains("LinFu.") || instanceTypeName == "Snap.AspectConfiguration"
                || instanceTypeName == "Snap.IMasterProxy" || instanceTypeName == "Snap.MasterProxy")
            {
                return;
            }

            // inteceptors could not be intercepted too, thus skip the code below
            if(instance is IInterceptor)
            {
                return;
            }

            var proxy = result.Container.GetService<IMasterProxy>();

            if (!instance.IsDecorated(proxy.Configuration))
            {
                return;
            }

            var pseudoList = new IInterceptor[proxy.Configuration.Interceptors.Count];
            pseudoList[0] = proxy;

            for (var i = 1; i < pseudoList.Length; i++)
            {
                pseudoList[i] = new PseudoInterceptor();
            }

            var targetInterface = instance.GetType().GetTypeToDynamicProxy(proxy.Configuration.Namespaces);

            result.ActualResult = AspectUtility.CreateProxy(targetInterface, instance, pseudoList);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Automatically injects service instances
        /// into properties as soon as they are initialized.
        /// </summary>
        /// <param name="result">The service request result that contains the service whose members will be injected with service instances.</param>
        public void PostProcess(IServiceRequestResult result)
        {
            // Prevent recursion
            if (_inProcess)
            {
                return;
            }

            lock (this)
            {
                _inProcess = true;
            }

            AutoInject(result);

            lock (this)
            {
                _inProcess = false;
            }
        }
Exemplo n.º 13
0
        public void PostProcess(IServiceRequestResult result)
        {
            var instance = result.ActualResult;
            if (instance == null)
            {
                return;
            }

            // inteceptors could not be intercepted too, thus skip the code below
            if (instance is IInterceptor)
            {
                return;
            }

            if (InterceptorHelper.HasInterceptor(instance.GetType()))
            {
                var proxy = result.Container.GetService(typeof(IInterceptorProxy)) as IInterceptorProxy;
                result.ActualResult = _proxyFactory.CreateProxy(instance, proxy);
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// A method that injects service proxies in place of the actual <see cref="IServiceRequestResult.ActualResult"/>.
        /// </summary>
        /// <param name="result">The <see cref="IServiceRequestResult"/> instance that describes the service request.</param>
        public void PostProcess(IServiceRequestResult result)
        {
            if (!_filterPredicate(result))
                return;

            var container = result.Container;
            var hasProxyFactory = container.Contains(typeof(IProxyFactory));

            // Inject proxies only if a
            // proxy factory instance is available
            if (!hasProxyFactory)
                return;

            // Sealed types cannot be intercepted
            if (result.ActualResult != null && result.ActualResult.GetType().IsSealed)
                return;

            // Replace the actual result with the proxy itself
            result.ActualResult = _createProxy(result);
        }
        public void PostProcess(IServiceRequestResult result)
        {
            var instance = result.ActualResult;

            if (instance == null)
            {
                return;
            }

            // inteceptors could not be intercepted too, thus skip the code below
            if (instance is IInterceptor)
            {
                return;
            }

            if (InterceptorHelper.HasInterceptor(instance.GetType()))
            {
                var proxy = result.Container.GetService(typeof(IInterceptorProxy)) as IInterceptorProxy;
                result.ActualResult = _proxyFactory.CreateProxy(instance, proxy);
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Monitors the <see cref="IServiceContainer"/> for any services that are created and automatically disposes them
        /// once the <see cref="IScope"/> is disposed.
        /// </summary>
        /// <param name="result">The <see cref="IServiceRequestResult"/> that describes the service being instantiated.</param>
        public void PostProcess(IServiceRequestResult result)
        {
            if (_disposed)
            {
                return;
            }

            // Only handle requests from the same thread
            if (_threadId != Thread.CurrentThread.ManagedThreadId)
            {
                return;
            }

            // Ignore any nondisposable instances
            if (result.ActualResult == null || !(result.ActualResult is IDisposable))
            {
                return;
            }

            var disposable = result.ActualResult as IDisposable;

            _disposables.Add(disposable);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Generates a proxy instance from an existing <see cref="IServiceRequestResult"/> instance.
        /// </summary>
        /// <param name="request">The <see cref="IServiceRequestResult"/> instance that describes the proxy type that must be generated.</param>
        /// <param name="getInterceptor">The <see cref="IInterceptor"/> functor that will create the interceptor which will handle all calls made to the proxy instance.</param>
        /// <returns
        /// >A service proxy.</returns>
        private static object CreateProxyFrom(IServiceRequestResult request, Func<IServiceRequestResult, IInterceptor> getInterceptor)
        {
            var interceptor = getInterceptor(request);

            var container = request.Container;
            var proxyFactory =
                container.GetService<IProxyFactory>();

            // The proxy factory must exist
            if (proxyFactory == null)
                return null;

            // Generate the proxy type
            var proxyType = proxyFactory.CreateProxyType(request.ServiceType,
                                                         new Type[0]);

            // The generated proxy instance
            // must implement IProxy
            var proxy = Activator.CreateInstance(proxyType) as IProxy;

            // Assign the interceptor
            if (proxy != null)
                proxy.Interceptor = interceptor;

            return proxy;
        }
Exemplo n.º 18
0
        /// <summary>
        /// Injects a member service dependency into a target service instance.
        /// </summary>
        /// <param name="result">The <see cref="IServiceRequestResult"/> that will be processed for injection.</param>
        private void AutoInject(IServiceRequestResult result)
        {
            // Ignore the excluded services
            if (_excludedServices.Contains(result.ServiceType))
            {
                return;
            }

            if (result.ServiceType.IsGenericType)
            {
                Type baseDefinition = result.ServiceType.GetGenericTypeDefinition();
                if (baseDefinition == typeof(IMemberInjectionFilter <>))
                {
                    return;
                }
            }

            IServiceContainer container = result.Container;

            if (!container.Contains(typeof(IMemberInjectionFilter <TMember>)))
            {
                return;
            }

            var filter = container.GetService <IMemberInjectionFilter <TMember> >();

            if (filter == null || result.ActualResult == null)
            {
                return;
            }

            // Determine which members can be injected
            Type targetType = result.ActualResult.GetType();

            // Use the base class if the
            // target type is a proxy type
            if (typeof(IProxy).IsAssignableFrom(targetType) && targetType.BaseType != typeof(object))
            {
                targetType = targetType.BaseType;
            }

            List <TMember> members = filter.GetInjectableMembers(targetType).ToList();

            if (members.Count == 0)
            {
                return;
            }

            // Use the resolver to determine
            // which value should be injected
            // into the member
            var resolver = container.GetService <IArgumentResolver>();

            if (resolver == null)
            {
                return;
            }

            object target = result.ActualResult;

            foreach (TMember member in members)
            {
                Inject(target, member, resolver, container, result.AdditionalArguments);
            }
        }
Exemplo n.º 19
0
 public void PostProcess(IServiceRequestResult result)
 {
     // Do nothing
 }
 public void PostProcess(IServiceRequestResult result)
 {
     // Do nothing
 }