Exemple #1
0
        public TypeDiscoveryContext(
            ITypeSystemObject type,
            TypeRegistry typeRegistry,
            TypeLookup typeLookup,
            IDescriptorContext descriptorContext,
            ITypeInterceptor typeInterceptor,
            string scope)
        {
            Type = type ??
                   throw new ArgumentNullException(nameof(type));
            _typeRegistry = typeRegistry ??
                            throw new ArgumentNullException(nameof(typeRegistry));
            _typeLookup = typeLookup ??
                          throw new ArgumentNullException(nameof(typeLookup));
            DescriptorContext = descriptorContext ??
                                throw new ArgumentNullException(nameof(descriptorContext));
            TypeInterceptor = typeInterceptor ??
                              throw new ArgumentNullException(nameof(typeInterceptor));
            Scope = scope;

            IsDirective = type is DirectiveType;
            IsSchema    = type is Schema;

            if (type is INamedType nt)
            {
                IsType = true;
                IsIntrospectionType = nt.IsIntrospectionType();
            }

            InternalName = "Type_" + Guid.NewGuid().ToString("N");
        }
        public TypeInitializer(
            IDescriptorContext descriptorContext,
            TypeRegistry typeRegistry,
            IReadOnlyList <ITypeReference> initialTypes,
            IReadOnlyList <Type> externalResolverTypes,
            IsOfTypeFallback?isOfType,
            Func <TypeSystemObjectBase, bool> isQueryType)
        {
            _context = descriptorContext ??
                       throw new ArgumentNullException(nameof(descriptorContext));
            _typeRegistry = typeRegistry ??
                            throw new ArgumentNullException(nameof(typeRegistry));
            _initialTypes = initialTypes ??
                            throw new ArgumentNullException(nameof(initialTypes));
            _externalResolverTypes = externalResolverTypes ??
                                     throw new ArgumentNullException(nameof(externalResolverTypes));
            _isOfType    = isOfType;
            _isQueryType = isQueryType ??
                           throw new ArgumentNullException(nameof(isQueryType));

            _interceptor           = descriptorContext.TypeInterceptor;
            _typeInspector         = descriptorContext.TypeInspector;
            _typeLookup            = new TypeLookup(_typeInspector, _typeRegistry);
            _typeReferenceResolver = new TypeReferenceResolver(
                _typeInspector, _typeRegistry, _typeLookup);
        }
        public TypeDiscoveryContext(
            ITypeSystemObject type,
            string scope,
            IServiceProvider services,
            IDescriptorContext descriptorContext,
            ITypeInterceptor interceptor)
        {
            Type = type
                   ?? throw new ArgumentNullException(nameof(type));
            Scope    = scope;
            Services = services
                       ?? throw new ArgumentNullException(nameof(services));
            DescriptorContext = descriptorContext
                                ?? throw new ArgumentNullException(nameof(descriptorContext));
            Interceptor = interceptor
                          ?? throw new ArgumentNullException(nameof(interceptor));
            IsDirective = type is DirectiveType;
            IsSchema    = type is Schema;

            if (type is INamedType nt)
            {
                IsType = true;
                IsIntrospectionType = nt.IsIntrospectionType();
            }

            InternalName = "Type_" + Guid.NewGuid().ToString("N");
        }
Exemple #4
0
        public TypeDiscoverer(
            ISet <ITypeReference> initialTypes,
            IDictionary <ClrTypeReference, ITypeReference> clrTypeReferences,
            IDescriptorContext descriptorContext,
            ITypeInterceptor interceptor,
            IServiceProvider services)
        {
            _unregistered.AddRange(IntrospectionTypes.All);
            _unregistered.AddRange(Directives.All);
            _unregistered.AddRange(clrTypeReferences.Values);
            _unregistered.AddRange(initialTypes);

            _clrTypeReferences = clrTypeReferences;

            _typeRegistrar = new TypeRegistrar(
                _registeredTypes,
                clrTypeReferences,
                descriptorContext,
                interceptor,
                services);

            _handlers = new ITypeRegistrarHandler[]
            {
                new SchemaTypeReferenceHandler(),
                new ClrTypeReferenceHandler(),
                new SyntaxTypeReferenceHandler()
            };
        }
        public RegisteredType(
            TypeSystemObjectBase type,
            bool isInferred,
            TypeRegistry typeRegistry,
            TypeLookup typeLookup,
            IDescriptorContext descriptorContext,
            ITypeInterceptor typeInterceptor,
            string?scope)
        {
            Type              = type;
            _typeRegistry     = typeRegistry;
            _typeLookup       = typeLookup;
            IsInferred        = isInferred;
            DescriptorContext = descriptorContext;
            TypeInterceptor   = typeInterceptor;
            IsExtension       = Type is INamedTypeExtensionMerger;
            IsSchema          = Type is ISchema;
            Scope             = scope;

            if (type is INamedType nt)
            {
                IsNamedType         = true;
                IsIntrospectionType = nt.IsIntrospectionType();
                Kind = nt.Kind;
            }
            else if (type is DirectiveType)
            {
                IsDirectiveType = true;
                Kind            = TypeKind.Directive;
            }
        }
Exemple #6
0
        /// <summary>
        /// API to configure interception for a type.
        /// </summary>
        /// <param name="typeToIntercept">Type to intercept.</param>
        /// <param name="name">Name type is registered under.</param>
        /// <param name="interceptor">Interceptor to use.</param>
        /// <returns>This extension object.</returns>
        public Interception SetInterceptorFor(Type typeToIntercept, string name, ITypeInterceptor interceptor)
        {
            NamedTypeBuildKey      key    = new NamedTypeBuildKey(typeToIntercept, name);
            TypeInterceptionPolicy policy = new TypeInterceptionPolicy(interceptor);

            Context.Policies.Set <ITypeInterceptionPolicy>(policy, key);
            return(this);
        }
Exemple #7
0
 /// <summary>
 /// Creates a new instance of type <paramref name="type"/> that is intercepted with the behaviors in
 /// <paramref name="interceptionBehaviors"/>.
 /// </summary>
 /// <param name="type">The type of the object to create.</param>
 /// <param name="interceptor">The <see cref="ITypeInterceptor"/> to use when creating the proxy.</param>
 /// <param name="interceptionBehaviors">The interception behaviors for the new proxy.</param>
 /// <param name="constructorParameters">The arguments for the creation of the new instance.</param>
 /// <returns>An instance of a class compatible with <paramref name="type"/> that includes execution of the
 /// given <paramref name="interceptionBehaviors"/>.</returns>
 /// <exception cref="ArgumentNullException">when <paramref name="type"/> is <see langword="null"/>.</exception>
 /// <exception cref="ArgumentNullException">when <paramref name="interceptor"/> is <see langword="null"/>.</exception>
 /// <exception cref="ArgumentNullException">when <paramref name="interceptionBehaviors"/> is <see langword="null"/>.</exception>
 /// <exception cref="ArgumentException">when <paramref name="interceptor"/> cannot intercept
 /// <paramref name="type"/>.</exception>
 public static object NewInstance(
     Type type,
     ITypeInterceptor interceptor,
     IEnumerable <IInterceptionBehavior> interceptionBehaviors,
     params object[] constructorParameters)
 {
     return(NewInstanceWithAdditionalInterfaces(type, interceptor, interceptionBehaviors, Type.EmptyTypes, constructorParameters));
 }
Exemple #8
0
 internal override void DoConfigure(IUnityContainer container, ITypeInterceptor interceptor)
 {
     container.Configure <Interception>()
     .SetInterceptorFor(
         this.Type,
         string.IsNullOrEmpty(this.Name) ? null : this.Name,
         interceptor);
 }
Exemple #9
0
        /// <summary>
        /// Set the interceptor for a type, regardless of what name is used to resolve the instances.
        /// </summary>
        /// <param name="typeToIntercept">Type to intercept</param>
        /// <param name="interceptor">Interceptor instance.</param>
        /// <returns>This extension object.</returns>
        public Interception SetDefaultInterceptorFor(Type typeToIntercept, ITypeInterceptor interceptor)
        {
            Guard.ArgumentNotNull(typeToIntercept, "typeToIntercept");
            Guard.ArgumentNotNull(interceptor, "interceptor");
            GuardTypeInterceptable(typeToIntercept, interceptor);

            Context.Policies.Set <ITypeInterceptionPolicy>(new TypeInterceptionPolicy(interceptor), typeToIntercept);
            return(this);
        }
Exemple #10
0
        public TypeDiscoverer(
            IDescriptorContext context,
            TypeRegistry typeRegistry,
            TypeLookup typeLookup,
            IEnumerable <ITypeReference> initialTypes,
            ITypeInterceptor interceptor,
            bool includeSystemTypes = true)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (typeRegistry is null)
            {
                throw new ArgumentNullException(nameof(typeRegistry));
            }

            if (typeLookup is null)
            {
                throw new ArgumentNullException(nameof(typeLookup));
            }

            if (initialTypes is null)
            {
                throw new ArgumentNullException(nameof(initialTypes));
            }

            if (interceptor is null)
            {
                throw new ArgumentNullException(nameof(interceptor));
            }

            _typeRegistry = typeRegistry;

            if (includeSystemTypes)
            {
                _unregistered.AddRange(
                    IntrospectionTypes.CreateReferences(context.TypeInspector));
                _unregistered.AddRange(
                    Directives.CreateReferences(context.TypeInspector));
            }

            _unregistered.AddRange(typeRegistry.GetTypeRefs());
            _unregistered.AddRange(initialTypes.Distinct());

            _typeRegistrar = new TypeRegistrar(context, typeRegistry, typeLookup, interceptor);

            _handlers = new ITypeRegistrarHandler[]
            {
                new SchemaTypeReferenceHandler(),
                new ExtendedTypeReferenceHandler(context.TypeInspector),
                new SyntaxTypeReferenceHandler(context.TypeInspector)
            };

            _typeInspector = context.TypeInspector;
        }
Exemple #11
0
 /// <summary>
 /// Creates a new instance of type <typeparamref name="T"/> that is intercepted with the behaviors in
 /// <paramref name="interceptionBehaviors"/>.
 /// </summary>
 /// <typeparam name="T">The type of the object to create.</typeparam>
 /// <param name="interceptor">The <see cref="ITypeInterceptor"/> to use when creating the proxy.</param>
 /// <param name="interceptionBehaviors">The interception behaviors for the new proxy.</param>
 /// <param name="constructorParameters">The arguments for the creation of the new instance.</param>
 /// <returns>An instance of a class compatible with <typeparamref name="T"/> that includes execution of the
 /// given <paramref name="interceptionBehaviors"/>.</returns>
 /// <exception cref="ArgumentNullException">when <paramref name="interceptor"/> is <see langword="null"/>.</exception>
 /// <exception cref="ArgumentNullException">when <paramref name="interceptionBehaviors"/> is <see langword="null"/>.</exception>
 /// <exception cref="ArgumentException">when <paramref name="interceptor"/> cannot intercept
 /// <typeparamref name="T"/>.</exception>
 public static T NewInstance <T>(
     ITypeInterceptor interceptor,
     IEnumerable <IInterceptionBehavior> interceptionBehaviors,
     params object[] constructorParameters)
     where T : class
 {
     return
         ((T)NewInstanceWithAdditionalInterfaces(typeof(T), interceptor, interceptionBehaviors, Type.EmptyTypes, constructorParameters));
 }
        /// <summary>
        /// Interceptor to use to create type proxy
        /// </summary>
        /// <param name="context">Context for current build operation.</param>
        public ITypeInterceptor GetInterceptor(IUnityContainer container)
        {
            if (null == _policy)
            {
                _policy = (ITypeInterceptor)container.Resolve(_type, _name, null);
            }

            return(_policy);
        }
 public TypeRegistrar(
     IDictionary <ITypeReference, RegisteredType> registeredTypes,
     IDictionary <ClrTypeReference, ITypeReference> clrTypeReferences,
     IDescriptorContext descriptorContext,
     ITypeInterceptor interceptor,
     IServiceProvider services)
 {
     _registered              = registeredTypes;
     _clrTypeReferences       = clrTypeReferences;
     _descriptorContext       = descriptorContext;
     _interceptor             = interceptor;
     _serviceFactory.Services = services;
 }
Exemple #14
0
        public static object NewInstanceWithAdditionalInterfaces(
            Type type,
            ITypeInterceptor interceptor,
            IEnumerable <IInterceptionBehavior> interceptionBehaviors,
            IEnumerable <Type> additionalInterfaces,
            params object[] constructorParameters)
        {
            Guard.ArgumentNotNull(type, "type");
            Guard.ArgumentNotNull(interceptor, "interceptor");
            Guard.ArgumentNotNull(interceptionBehaviors, "interceptionBehaviors");
            Guard.ArgumentNotNull(additionalInterfaces, "additionalInterfaces");

            if (!interceptor.CanIntercept(type))
            {
                throw new ArgumentException(
                          string.Format(
                              CultureInfo.CurrentCulture,
                              Resources.InterceptionNotSupported,
                              type.FullName),
                          "type");
            }

            var behaviors = interceptionBehaviors.ToList();

            if (behaviors.Where(ib => ib == null).Count() > 0)
            {
                throw new ArgumentException(
                          string.Format(CultureInfo.CurrentCulture, Resources.NullBehavior),
                          "interceptionBehaviors");
            }

            Type implementationType = type;

            var activeBehaviors = behaviors.Where(ib => ib.WillExecute);

            Type[] allAdditionalInterfaces = GetAllAdditionalInterfaces(activeBehaviors, additionalInterfaces);

            Type interceptionType = interceptor.CreateProxyType(implementationType, allAdditionalInterfaces);

            var proxy = (IInterceptingProxy)Activator.CreateInstance(interceptionType, constructorParameters);

            foreach (IInterceptionBehavior interceptionBehavior in activeBehaviors)
            {
                proxy.AddInterceptionBehavior(interceptionBehavior);
            }

            return(proxy);
        }
Exemple #15
0
 public TypeRegistrar(
     IDescriptorContext context,
     TypeRegistry typeRegistry,
     TypeLookup typeLookup,
     ITypeInterceptor typeInterceptor)
 {
     _context = context ??
                throw new ArgumentNullException(nameof(context));
     _typeRegistry = typeRegistry ??
                     throw new ArgumentNullException(nameof(typeRegistry));
     _typeLookup = typeLookup ??
                   throw new ArgumentNullException(nameof(typeLookup));
     _interceptor = typeInterceptor ??
                    throw new ArgumentNullException(nameof(typeInterceptor));
     _serviceFactory.Services = context.Services;
 }
Exemple #16
0
        /// <summary>
        /// Set the interceptor for a type, regardless of what name is used to resolve the instances.
        /// </summary>
        /// <param name="typeToIntercept">Type to intercept</param>
        /// <param name="interceptor">Interceptor instance.</param>
        /// <returns>This extension object.</returns>
        public Interception SetDefaultInterceptorFor(Type typeToIntercept, ITypeInterceptor interceptor)
        {
            Guard.ArgumentNotNull(typeToIntercept, "typeToIntercept");
            Guard.ArgumentNotNull(interceptor, "interceptor");
            GuardTypeInterceptable(typeToIntercept, interceptor);

            Context.Policies.Set <ITypeInterceptionPolicy>(new FixedTypeInterceptionPolicy(interceptor), typeToIntercept);

            // add policy injection behavior if using this configuration API to set the interceptor
            var interceptionBehaviorsPolicy = new InterceptionBehaviorsPolicy();

            interceptionBehaviorsPolicy.AddBehaviorKey(NamedTypeBuildKey.Make <PolicyInjectionBehavior>());
            Context.Policies.Set <IInterceptionBehaviorsPolicy>(interceptionBehaviorsPolicy, typeToIntercept);

            return(this);
        }
 public TypeInitializer(
     IServiceProvider services,
     IDescriptorContext descriptorContext,
     IEnumerable <ITypeReference> initialTypes,
     IEnumerable <Type> externalResolverTypes,
     ITypeInterceptor interceptor,
     IsOfTypeFallback isOfType,
     Func <TypeSystemObjectBase, bool> isQueryType)
 {
     _services              = services;
     _descriptorContext     = descriptorContext;
     _contextData           = _descriptorContext.ContextData;
     _initialTypes          = initialTypes.ToList();
     _externalResolverTypes = externalResolverTypes.ToList();
     _interceptor           = interceptor;
     _isOfType              = isOfType;
     _isQueryType           = isQueryType;
 }
Exemple #18
0
        /// <summary>
        /// Set the interceptor for a type, regardless of what name is used to resolve the instances.
        /// </summary>
        /// <param name="typeToIntercept">Type to intercept</param>
        /// <param name="interceptor">Interceptor instance.</param>
        /// <returns>This extension object.</returns>
        public Interception SetDefaultInterceptorFor(Type typeToIntercept, ITypeInterceptor interceptor)
        {
            throw new NotImplementedException();

            //Guard.ArgumentNotNull(typeToIntercept, "typeToIntercept");
            //Guard.ArgumentNotNull(interceptor, "interceptor");
            //GuardTypeInterceptable(typeToIntercept, interceptor);

            //Context.Policies.Set(typeToIntercept, UnityContainer.All, typeof(ITypeInterceptionPolicy),
            //                     new FixedTypeInterceptionPolicy(interceptor));

            //// add policy injection behavior if using this configuration API to set the interceptor
            //var interceptionBehaviorsPolicy = new InterceptionBehaviorsPolicy();
            //interceptionBehaviorsPolicy.AddBehaviorKey(NamedTypeBuildKey.Make<PolicyInjectionBehavior>());
            //Context.Policies.Set(typeToIntercept, UnityContainer.All,
            //                    typeof(IInterceptionBehaviorsPolicy), interceptionBehaviorsPolicy);
            //return this;
        }
Exemple #19
0
        /// <summary>
        /// API to configure interception for a type.
        /// </summary>
        /// <param name="typeToIntercept">Type to intercept.</param>
        /// <param name="name">Name type is registered under.</param>
        /// <param name="interceptor">Interceptor to use.</param>
        /// <returns>This extension object.</returns>
        public Interception SetInterceptorFor(Type typeToIntercept, string name, ITypeInterceptor interceptor)
        {
            Guard.ArgumentNotNull(typeToIntercept, "typeToIntercept");
            Guard.ArgumentNotNull(interceptor, "interceptor");
            GuardTypeInterceptable(typeToIntercept, interceptor);

            var key = new NamedTypeBuildKey(typeToIntercept, name);

            var policy = new FixedTypeInterceptionPolicy(interceptor);

            Context.Policies.Set(key.Type, key.Name, typeof(ITypeInterceptionPolicy), policy);

            // add policy injection behavior if using this configuration API to set the interceptor
            var interceptionBehaviorsPolicy = new InterceptionBehaviorsPolicy();

            interceptionBehaviorsPolicy.AddBehaviorKey(NamedTypeBuildKey.Make <PolicyInjectionBehavior>());
            Context.Policies.Set(key.Type, key.Name, typeof(IInterceptionBehaviorsPolicy), interceptionBehaviorsPolicy);

            return(this);
        }
        internal void Configure(IUnityContainer container)
        {
            IInterceptor interceptor = this.CreateInstance <IInterceptor>();

            IInstanceInterceptor instanceInterceptor = interceptor as IInstanceInterceptor;

            if (instanceInterceptor != null)
            {
                foreach (InterceptorTargetConfigurationElementBase target in Targets)
                {
                    target.DoConfigure(container, instanceInterceptor);
                }
            }
            else
            {
                ITypeInterceptor typeInterceptor = (ITypeInterceptor)interceptor;
                foreach (InterceptorTargetConfigurationElementBase target in Targets)
                {
                    target.DoConfigure(container, typeInterceptor);
                }
            }
        }
        public override void PreBuildUp(IBuilderContext context)
        {
            Guard.ArgumentNotNull(context, "context");
            if (!this.CanIntercept(context))
            {
                return;
            }
            if (context.Existing != null)
            {
                return;
            }
            var interceptionPolicy       = FindInterceptionPolicy <ITypeInterceptionPolicy>(context);
            ITypeInterceptor interceptor = null;

            if (interceptionPolicy == null)
            {
                interceptor = context.NewBuildUp <IInterceptor>(typeof(IInterceptor).AssemblyQualifiedName) as ITypeInterceptor;
                if (null == interceptor)
                {
                    return;
                }
                if (!interceptor.CanIntercept(context.BuildKey.Type))
                {
                    return;
                }
                interceptionPolicy = new FixedTypeInterceptionPolicy(interceptor);
                context.Policies.Set <ITypeInterceptionPolicy>(interceptionPolicy, context.BuildKey);
                context.Policies.Clear <IInstanceInterceptionPolicy>(context.BuildKey);
            }
            var interceptionBehaviorsPolicy = FindInterceptionPolicy <IInterceptionBehaviorsPolicy>(context);

            if (null == interceptionBehaviorsPolicy)
            {
                var policyInjectionBehavior = new InterceptionBehavior <PolicyInjectionBehavior>();
                policyInjectionBehavior.AddPolicies(context.OriginalBuildKey.Type, context.BuildKey.Type, context.BuildKey.Name, context.Policies);
            }
        }
Exemple #22
0
 /// <summary>
 /// API to configure interception for a type.
 /// </summary>
 /// <typeparam name="T">Type to intercept</typeparam>
 /// <param name="interceptor">Interceptor object to use.</param>
 /// <returns>This extension object.</returns>
 public Interception SetInterceptorFor <T>(ITypeInterceptor interceptor)
 {
     return(this.SetInterceptorFor(typeof(T), null, interceptor));
 }
Exemple #23
0
 /// <summary>
 /// API to configure interception for a type.
 /// </summary>
 /// <typeparam name="T">Type to intercept</typeparam>
 /// <param name="name">Name type is registered under.</param>
 /// <param name="interceptor">Interceptor object to use.</param>
 /// <returns>This extension object.</returns>
 public Interception SetInterceptorFor <T>(string name, ITypeInterceptor interceptor)
 {
     return(this.SetInterceptorFor(typeof(T), name, interceptor));
 }
Exemple #24
0
 /// <summary>
 /// API to configure interception for a type.
 /// </summary>
 /// <param name="typeToIntercept">Type to intercept.</param>
 /// <param name="interceptor">Interceptor to use.</param>
 /// <returns>This extension object.</returns>
 public Interception SetInterceptorFor(Type typeToIntercept, ITypeInterceptor interceptor)
 {
     return(this.SetInterceptorFor(typeToIntercept, null, interceptor));
 }
Exemple #25
0
 /// <summary>
 /// Set the interceptor for a type, regardless of what name is used to resolve the instances.
 /// </summary>
 /// <typeparam name="TTypeToIntercept">Type to intercept</typeparam>
 /// <param name="interceptor">Interceptor instance.</param>
 /// <returns>This extension object.</returns>
 public Interception SetDefaultInterceptorFor <TTypeToIntercept>(ITypeInterceptor interceptor)
 {
     return(this.SetDefaultInterceptorFor(typeof(TTypeToIntercept), interceptor));
 }
Exemple #26
0
 public static Type InterceptWithAttributes <TService>(this ITypeInterceptor interceptor) where TService : class
 {
     return(InterceptWithAttributes(interceptor, typeof(TService)));
 }
 /// <summary>
 /// Create a new instance of <see cref="FixedTypeInterceptionPolicy"/> that
 /// uses the given <see cref="ITypeInterceptor"/>.
 /// </summary>
 /// <param name="interceptor">Interceptor to use.</param>
 public FixedTypeInterceptionPolicy(ITypeInterceptor interceptor)
 {
     this.interceptor = interceptor;
 }
Exemple #28
0
 internal abstract void DoConfigure(IUnityContainer container, ITypeInterceptor interceptor);
 public static Type Intercept <TToIntercept, TInterceptor>(this ITypeInterceptor interceptor) where TInterceptor : IInterceptionHandler, new()
 {
     return(interceptor.Intercept(typeof(TToIntercept), typeof(TInterceptor)));
 }
 /// <summary>
 /// Create a new instance of <see cref="FixedTypeInterceptionPolicy"/> that
 /// uses the given <see cref="ITypeInterceptor"/>.
 /// </summary>
 /// <param name="interceptor">Interceptor to use.</param>
 public FixedTypeInterceptionPolicy(ITypeInterceptor interceptor)
 {
     this.interceptor = interceptor;
 }
Exemple #31
0
 /// <summary>
 /// Creates an instance of TInterface.
 /// </summary>
 /// <returns></returns>
 public TInterface Create(ITypeInterceptor typeInterceptor, TInterceptionBehavior[] behaviors)
 {
     return((TInterface)Intercept.NewInstance <object>(typeInterceptor, behaviors));
 }