Beispiel #1
0
        /// <summary>
        /// Creates a <see cref="ConstructorTarget"/> or <see cref="GenericConstructorTarget"/>
        /// for the type <paramref name="type"/>.
        /// </summary>
        /// <param name="type">The type whose constructor is to be bound by the target.</param>
        /// <param name="namedArgs">Optional.  A dictionary of targets that are to be bound to the type's
        /// constructor by name and type.  If <paramref name="type"/> is a generic type definition, then
        /// this parameter must be null, or an <see cref="ArgumentException"/> will be thrown.</param>
        /// <param name="memberBinding">Optional - provides an explicit member injection behaviour to be used when creating the instance,
        /// if different from the behaviour configured via options on any target container in which the target is subsequently registered.</param>
        /// <returns>A new target for the type <paramref name="type"/></returns>
        /// <remarks>If the type is a generic type definition, then a <see cref="GenericConstructorTarget"/>
        /// is created; otherwise a <see cref="ConstructorTarget"/> is created.</remarks>
        public static ITarget ForType(
            Type type,
            IDictionary <string, ITarget> namedArgs,
            IMemberBindingBehaviour memberBinding = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (type.IsGenericTypeDefinition)
            {
                // can't pass named arguments if the type is generic, because there's no reliable
                // way to guarantee that the arguments can actually be bound at the moment.
                // once we have conditional targets, then perhaps.
                if (namedArgs?.Count > 0)
                {
                    throw new ArgumentException("Cannot use namedArguments with a generic type", nameof(namedArgs));
                }

                return(new GenericConstructorTarget(type, memberBinding));
            }

            return(new ConstructorTarget(type, namedArgs, memberBinding));
        }
Beispiel #2
0
 /// <summary>
 /// Creates a <see cref="ConstructorTarget"/> or <see cref="GenericConstructorTarget"/>
 /// for the type <paramref name="type"/>.
 /// </summary>
 /// <param name="type">The type whose constructor is to be bound by the target.</param>
 /// <param name="namedArgs">Optional.  An object whose publicly readable members which are of the
 /// type <see cref="ITarget"/> (or a type which implements it) are to be bound to the type's constructor
 /// by name and <see cref="ITarget.DeclaredType"/>.
 ///
 /// If <paramref name="type"/> is a generic type definition, then
 /// this parameter must be null, or an <see cref="ArgumentException"/> will be thrown.</param>
 /// <param name="memberBinding">Optional - provides an explicit member injection behaviour to be used when creating the instance,
 /// if different from the behaviour configured via options on any target container in which the target is subsequently registered.</param>
 /// <remarks>If the type is a generic type definition, then a <see cref="GenericConstructorTarget"/>
 /// is created; otherwise a <see cref="ConstructorTarget"/> is created.
 /// </remarks>
 /// <example>This example shows how to provide an ObjectTarget for the parameter 'param1' when creating a
 /// ConstructorTarget for the type 'MyType':
 /// <code>Target.ForType(typeof(MyType), namedArguments: new { param1 = new ObjectTarget(&quot; Hello World&quot;) });</code>
 /// </example>
 public static ITarget ForType(
     Type type,
     object namedArgs,
     IMemberBindingBehaviour memberBinding = null)
 {
     return(ForType(type, namedArgs.ToMemberValueDictionary <ITarget>(), memberBinding));
 }
        /// <summary>
        /// Registers a type by constructor alone.
        /// </summary>
        /// <param name="targets"></param>
        /// <param name="constructor">The constructor to be bound. The <see cref="MemberInfo.DeclaringType"/> will be used
        /// as the service type to be registered against.</param>
        /// <param name="memberBindingBehaviour">Optional. If you wish to bind members on the new instance, passing a member binding
        /// behaviour here.</param>
        /// <remarks>If the <paramref name="constructor"/> belongs to an open generic type, then a <see cref="Targets.GenericConstructorTarget"/>
        /// will be created and registered.</remarks>
        public static void RegisterConstructor(
            this ITargetContainer targets,
            ConstructorInfo constructor,
            IMemberBindingBehaviour memberBindingBehaviour = null)
        {
            if (targets == null)
            {
                throw new ArgumentNullException(nameof(targets));
            }

            targets.Register(Target.ForConstructor(constructor ?? throw new ArgumentNullException(nameof(constructor)), memberBindingBehaviour));
        }
        /// <summary>
        /// Creates and registers a target bound to the constructor of a generic type definition using the
        /// <see cref="Target.ForGenericConstructor{TExample}(Expression{Func{TExample}}, IMemberBindingBehaviour)"/> factory method.
        ///
        /// See the documentation on that method for more.
        ///
        /// The registration will be made against the open generic type.
        /// </summary>
        /// <typeparam name="TExample">Must be a generic type which represents a concrete generic whose generic type definition will be
        /// bound by the created target.  This type is also used as the service type for the registration.</typeparam>
        /// <param name="targets">The container into which the registration will be made.</param>
        /// <param name="newExpr">Exemplar expression which is used to identify the constructor to be bound.</param>
        /// <param name="memberBindingBehaviour">A member binding behaviour to be passed to the created target</param>
        /// <seealso cref="Target.ForGenericConstructor{TExample}(Expression{Func{TExample}}, IMemberBindingBehaviour)"/>
        public static void RegisterGenericConstructor <TExample>(
            this ITargetContainer targets,
            Expression <Func <TExample> > newExpr,
            IMemberBindingBehaviour memberBindingBehaviour = null)
        {
            if (targets == null)
            {
                throw new ArgumentNullException(nameof(targets));
            }

            targets.Register(Target.ForGenericConstructor(newExpr, memberBindingBehaviour));
        }
        /// <summary>
        /// Same as <see cref="RegisterGenericConstructor{TObject}(ITargetContainer, Expression{Func{TObject}}, IMemberBindingBehaviour)"/>
        /// except this creates the target and then registers it against a generic base or interface of the generic type definition identified
        /// from <typeparamref name="TExampleService"/>.
        /// </summary>
        /// <typeparam name="TExample">Must be a generic type which represents a concrete generic whose generic type definition will be
        /// bound by the target that is created and registered.  The type must inherit or implement the type <typeparamref name="TExampleService"/>.</typeparam>
        /// <typeparam name="TExampleService">Must be a generic type that is a base or interface of <typeparamref name="TExample"/>.  The registration will
        /// be made against this type's generic type definition.</typeparam>
        /// <param name="targets">The container into which the registration will be made.</param>
        /// <param name="newExpr">Exemplar expression which is used to identify the constructor to be bound.</param>
        /// <param name="memberBindingBehaviour">A member binding behaviour to be passed to the created target</param>
        /// <seealso cref="Target.ForGenericConstructor{TExample}(Expression{Func{TExample}}, IMemberBindingBehaviour)"/>
        public static void RegisterGenericConstructor <TExample, TExampleService>(
            this ITargetContainer targets,
            Expression <Func <TExample> > newExpr,
            IMemberBindingBehaviour memberBindingBehaviour = null)
            where TExample : TExampleService
        {
            if (targets == null)
            {
                throw new ArgumentNullException(nameof(targets));
            }

            if (!typeof(TExampleService).IsGenericType)
            {
                throw new ArgumentException($"Service type {typeof(TExampleService)} is not a generic type", nameof(TExampleService));
            }

            targets.Register(Target.ForGenericConstructor(newExpr, memberBindingBehaviour), typeof(TExampleService).GetGenericTypeDefinition());
        }
Beispiel #6
0
        /// <summary>
        /// Creates a target which binds to the constructor of an open generic type, an exemplar of which is passed in <paramref name="newExpr"/>.
        ///
        /// The created target will be <see cref="Targets.GenericConstructorTarget"/> whose <see cref="Targets.GenericConstructorTarget.GenericTypeConstructor"/>
        /// will be set to the constructor that is identified from the expression.
        /// </summary>
        /// <typeparam name="TExample">Must be a generic type.  It doesn't matter what the type arguments are, however,
        /// as the target that is created will be for the generic type definition of this type.</typeparam>
        /// <param name="newExpr">A lambda expression whose <see cref="LambdaExpression.Body"/> is a <see cref="NewExpression"/>
        /// which identifies the constructor that is to be used to create the instance of all concrete types derived from the
        /// the same generic type definition as <typeparamref name="TExample"/>.</param>
        /// <param name="memberBindingBehaviour">Optional. If you wish to bind members on the new instance, pass a member binding
        /// behaviour here.</param>
        /// <remarks>Note - a concrete generic is used as an *example* - the equivalent open generic constructor is located
        /// and registered against the open generic of the type you actually invoke this method for; e.g. if
        /// <typeparamref name="TExample"/> is `MyGeneric&lt;Foo, Bar&gt;`, then a target bound to the equivalent
        /// constructor on the open generic `MyGeneric&lt;,&gt;` will be what is actually created.</remarks>
        public static ITarget ForGenericConstructor <TExample>(
            Expression <Func <TExample> > newExpr,
            IMemberBindingBehaviour memberBindingBehaviour = null)
        {
            if (!typeof(TExample).IsGenericType)
            {
                throw new ArgumentException($"{typeof(TExample)} is not a generic type.");
            }

            var ctor = Extract.GenericConstructor(newExpr ?? throw new ArgumentNullException(nameof(newExpr)));

            if (ctor == null)
            {
                throw new ArgumentException($"The expression ${newExpr} does not represent a NewExpression invoking a generic type's constructor.", nameof(newExpr));
            }

            return(ForConstructor(ctor, memberBindingBehaviour));
        }
        /// <summary>
        /// Register a type by constructor (represented by the expression <paramref name="newExpr"/>).
        /// </summary>
        /// <typeparam name="TObject"></typeparam>
        /// <param name="targets"></param>
        /// <param name="newExpr">A lambda expression whose <see cref="LambdaExpression.Body"/> is a <see cref="NewExpression"/>
        /// which identifies the constructor that is to be used to create the instance of <typeparamref name="TObject"/>.</param>
        /// <param name="memberBindingBehaviour">Optional. If you wish to bind members on the new instance, passing a member binding
        /// behaviour here.</param>
        /// <remarks>Note that you can achieve a similar result by simply registering an expression which
        /// represents a call to a type's constructor.</remarks>
        public static void RegisterConstructor <TObject>(
            this ITargetContainer targets,
            Expression <Func <TObject> > newExpr,
            IMemberBindingBehaviour memberBindingBehaviour = null)
        {
            if (targets == null)
            {
                throw new ArgumentNullException(nameof(targets));
            }

            var ctor = Extract.Constructor(newExpr ?? throw new ArgumentNullException(nameof(newExpr)));

            if (ctor == null)
            {
                throw new ArgumentException($"The expression ${newExpr} does not represent a NewExpression", nameof(newExpr));
            }

            targets.Register(Target.ForConstructor(ctor, memberBindingBehaviour));
        }
        private ConstructorTarget(Type type,
                                  ConstructorInfo ctor,
                                  IMemberBindingBehaviour memberBinding,
                                  ParameterBinding[] parameterBindings,
                                  IDictionary <string, ITarget> suppliedArgs)
        {
            this._ctor   = ctor;
            DeclaredType = type ?? ctor?.DeclaringType;
            if (type != null)
            {
                if (type.IsInterface || type.IsAbstract)
                {
                    throw new ArgumentException("Type must not be an interface or an abstract class", nameof(type));
                }
            }

            this._parameterBindings = parameterBindings ?? ParameterBinding.None;
            MemberBindingBehaviour  = memberBinding;
            this._namedArgs         = suppliedArgs ?? new Dictionary <string, ITarget>();
        }
Beispiel #9
0
        /// <summary>
        /// Creates a <see cref="ConstructorTarget"/> for the given constructor, or a <see cref="GenericConstructorTarget"/> if the
        /// constructor belongs to a generic type definition ('open generic type').
        /// </summary>
        /// <param name="constructor">Required.  The constructor to be bound by the target.</param>
        /// <param name="parameterBindings">Can be null/empty.  An array of <see cref="ParameterBinding"/>
        /// objects containing targets to be bound to somme or all of the constructor parameters.  **Must not be supplied if <paramref name="constructor"/>
        /// is a constructor belonging to an open generic type,**</param>
        /// <param name="memberBinding">Optional - provides an explicit member injection behaviour to be used when creating the instance,
        /// if different from the behaviour configured via options on any target container in which the target is subsequently registered.</param>
        public static ITarget ForConstructor(
            ConstructorInfo constructor,
            ParameterBinding[] parameterBindings,
            IMemberBindingBehaviour memberBinding = null)
        {
            if (constructor == null)
            {
                throw new ArgumentNullException(nameof(constructor));
            }

            if (constructor.DeclaringType.IsGenericTypeDefinition)
            {
                if (parameterBindings?.Length > 0)
                {
                    throw new ArgumentException("You cannot currently supply parameter bindings for generic constructors", nameof(parameterBindings));
                }

                return(new GenericConstructorTarget(constructor, memberBinding));
            }

            return(new ConstructorTarget(constructor, parameterBindings, memberBinding));
        }
Beispiel #10
0
        /// <summary>
        /// Creates a <see cref="ConstructorTarget"/> for the given constructor, or a <see cref="GenericConstructorTarget"/> if the
        /// constructor belongs to a generic type definition ('open generic type').
        /// </summary>
        /// <param name="constructor">Required.  The constructor to be bound by the target.</param>
        /// <param name="namedArgs">Optional.  An object whose publicly readable members which are of the
        /// type <see cref="ITarget"/> (or a type which implements it) are to be bound to the constructor
        /// by name and <see cref="ITarget.DeclaredType"/>.  **Must not be supplied if <paramref name="constructor"/>
        /// is a constructor belonging to an open generic type,**</param>
        /// <param name="memberBinding">Optional - provides an explicit member injection behaviour to be used when creating the instance,
        /// if different from the behaviour configured via options on any target container in which the target is subsequently registered.</param>
        public static ITarget ForConstructor(
            ConstructorInfo constructor,
            object namedArgs,
            IMemberBindingBehaviour memberBinding = null)
        {
            if (constructor == null)
            {
                throw new ArgumentNullException(nameof(constructor));
            }

            if (constructor.DeclaringType.IsGenericTypeDefinition)
            {
                if (namedArgs != null)
                {
                    throw new ArgumentException("You cannot current supply named argument bindings for open generic constructors", nameof(namedArgs));
                }

                return(new GenericConstructorTarget(constructor, memberBinding));
            }

            var bindings = ParameterBinding.BindMethod(constructor, namedArgs.ToMemberValueDictionary <ITarget>());

            return(new ConstructorTarget(constructor, bindings, memberBinding));
        }
Beispiel #11
0
 /// <summary>
 /// Creates a <see cref="ConstructorTarget"/> or <see cref="GenericConstructorTarget"/>
 /// for the type <typeparamref name="T"/>.
 /// </summary>
 /// <typeparam name="T">The type whose constructor is to be bound by the created target.</typeparam>
 /// <param name="namedArgs">Optional.  An object whose publicly readable members which are of the
 /// type <see cref="ITarget"/> (or a type which implements it) are to be bound to the type's constructor
 /// by name and <see cref="ITarget.DeclaredType"/>.</param>
 /// <param name="memberBinding">Optional - provides an explicit member injection behaviour to be used when creating the instance,
 /// if different from the behaviour configured via options on any target container in which the target is subsequently registered.</param>
 /// <returns>A new target for the type <typeparamref name="T"/></returns>
 /// <example>This example shows how to provide an ObjectTarget for the parameter 'param1' when creating a
 /// ConstructorTarget for the type 'MyType':
 /// <code>Target.ForType&lt;MyType&gt;(namedArguments: new { param1 = new ObjectTarget(&quot;Hello World&quot;) });</code>
 /// </example>
 public static ITarget ForType <T>(
     object namedArgs,
     IMemberBindingBehaviour memberBinding = null)
 {
     return(ForType(typeof(T), namedArgs, memberBinding));
 }
 /// <summary>
 /// Registers the type <typeparamref name="TObject"/> for the service type <typeparamref name="TService"/> to be created by
 /// an <see cref="Container"/> via constructor injection.
 /// The registration will auto-bind a constructor based on the services available in the <see cref="ITargetContainer"/> and
 /// <see cref="Container"/> available at the time <see cref="Container.Resolve(ResolveContext)"/> is first called.
 /// </summary>
 /// <typeparam name="TObject">The type of the object that is to be constructed when resolved.</typeparam>
 /// <typeparam name="TService">The type against which the registration will be performed.  <typeparamref name="TObject"/> must be
 /// compatible with this type.</typeparam>
 /// <param name="targetContainer">The target container on which the registration is to be performed.</param>
 /// <param name="memberBinding">Optional - provides an explicit member injection behaviour to be used when creating the instance,
 /// if different from the behaviour configured via options on the <paramref name="targetContainer"/>.</param>
 /// <remarks>This is equivalent to creating either a <see cref="ConstructorTarget"/> or <see cref="GenericConstructorTarget"/> via
 /// the <see cref="Target.ForType{T}(IMemberBindingBehaviour)"/> static method and then registering it against
 /// the type <typeparamref name="TService"/>.</remarks>
 public static void RegisterType <TObject, TService>(this ITargetContainer targetContainer, IMemberBindingBehaviour memberBinding = null)
     where TObject : TService
 {
     RegisterType(targetContainer, typeof(TObject), serviceType: typeof(TService), memberBinding: memberBinding);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ConstructorTarget"/> class bound in advance to a specific constructor.
 /// </summary>
 /// <param name="ctor">Required - the constructor that is to be bound.  The <see cref="DeclaredType"/> of the new instance
 /// will be set to the <see cref="MemberInfo.DeclaringType"/> of this object.</param>
 /// <param name="parameterBindings">Optional.  Specific bindings for the parameters of the given <paramref name="ctor"/>
 /// which should be used during code generation.  Note that this array can contain fewer or more entries than there are
 /// parameters on the <paramref name="ctor"/>.  Any missing bindings will be automatically generated when <see cref="Bind(ICompileContext)"/>
 /// is called.</param>
 /// <param name="memberBinding">Optional - provides an explicit member injection behaviour to be used when creating the instance.
 /// If not provided, then the <see cref="Bind(ICompileContext)"/> method will attempt to obtain one via the options API from the
 /// <see cref="ICompileContext"/> - and if one is still not available, then no member binding will be performed.</param>
 public ConstructorTarget(ConstructorInfo ctor, ParameterBinding[] parameterBindings = null, IMemberBindingBehaviour memberBinding = null)
     : this(null, ctor, memberBinding, parameterBindings, null)
 {
     if (ctor == null)
     {
         throw new ArgumentNullException(nameof(ctor));
     }
 }
Beispiel #14
0
        /// <summary>
        /// Registers an explicitly instance of <paramref name="objectType"/> (optionally for the service type <paramref name="serviceType"/>) to be
        /// created by an <see cref="Container"/> via constructor injection.
        /// The registration will auto-bind a constructor based on the services available in the <see cref="ITargetContainer"/> and
        /// <see cref="Container"/> available at the time <see cref="Container.Resolve(ResolveContext)"/> is first called.
        /// </summary>
        /// <param name="targetContainer">The target container on which the registration is to be performed.</param>
        /// <param name="objectType">The type of the object that is to be constructed when resolved.</param>
        /// <param name="serviceType">Optional.  The type against which the registration will be performed, if different from
        /// <paramref name="objectType"/>.  <paramref name="objectType"/> must be compatible with this type, if it's provided.</param>
        /// <param name="memberBinding">Optional - provides an explicit member injection behaviour to be used when creating the instance,
        /// if different from the behaviour configured via options on the <paramref name="targetContainer"/>.</param>
        /// <remarks>This is equivalent to creating either a <see cref="ConstructorTarget"/> or <see cref="GenericConstructorTarget"/> via
        /// the <see cref="Target.ForType(Type, IMemberBindingBehaviour)"/> static method, wrapping it with a <see cref="ScopedTarget"/>
        /// and then registering it against the type <paramref name="serviceType"/> or <paramref name="objectType"/>.</remarks>
        public static void RegisterScoped(this ITargetContainer targetContainer, Type objectType, Type serviceType = null, IMemberBindingBehaviour memberBinding = null)
        {
            if (targetContainer == null)
            {
                throw new ArgumentNullException(nameof(targetContainer));
            }
            if (objectType == null)
            {
                throw new ArgumentNullException(nameof(objectType));
            }

            RegisterScopedInternal(targetContainer, objectType, serviceType, memberBinding);
        }
Beispiel #15
0
 /// <summary>
 /// Creates a <see cref="ConstructorTarget" /> or <see cref="GenericConstructorTarget" />
 /// for the type <typeparamref name="T" />.
 /// </summary>
 /// <typeparam name="T">The type whose constructor is to be bound by the target.</typeparam>
 /// <param name="namedArgs">Can be null. A dictionary of targets that are to be bound to the type's
 /// constructor by name and <see cref="ITarget.DeclaredType"/>.
 ///
 /// If <typeparamref name="T"/> is a generic type definition, then
 /// this parameter must be null, or an <see cref="ArgumentException"/> will be thrown.</param>
 /// <param name="memberBinding">Optional - provides an explicit member injection behaviour to be used when creating the instance,
 /// if different from the behaviour configured via options on any target container in which the target is subsequently registered.</param>
 /// <returns>A new target for the type <typeparamref name="T"/></returns>
 public static ITarget ForType <T>(
     IDictionary <string, ITarget> namedArgs,
     IMemberBindingBehaviour memberBinding = null)
 {
     return(ForType(typeof(T), namedArgs, memberBinding));
 }
Beispiel #16
0
 /// <summary>
 /// Creates a <see cref="ConstructorTarget"/> for the type <typeparamref name="T"/>.
 /// </summary>
 /// <typeparam name="T">The type whose constructor is to be bound by the target.</typeparam>
 /// <param name="memberBinding">Optional - provides an explicit member injection behaviour to be used when creating the instance,
 /// if different from the behaviour configured via options on any target container in which the target is subsequently registered.</param>
 /// <returns>A new target for the type <typeparamref name="T"/></returns>
 public static ITarget ForType <T>(IMemberBindingBehaviour memberBinding = null)
 {
     return(ForType <T>(namedArgs: null, memberBinding: memberBinding));
 }
Beispiel #17
0
 /// <summary>
 /// Registers the type <typeparamref name="TObject"/> as a singleton (<see cref="SingletonTarget"/>) in the target container.
 ///
 /// The instance will be built automatically with constructor injection by leveraging either the <see cref="ConstructorTarget"/> or
 /// <see cref="GenericConstructorTarget"/>, depending on whether <typeparamref name="TObject"/> is a generic type or not.
 /// </summary>
 /// <typeparam name="TObject">The type to be created, and the type against which the registration will be made</typeparam>
 /// <param name="targetContainer">The container on which the registrations will be made.</param>
 /// <param name="memberBinding">Optional - provides an explicit member injection behaviour to be used when creating the instance,
 /// if different from the behaviour configured via options on the <paramref name="targetContainer"/>.</param>
 public static void RegisterSingleton <TObject>(this ITargetContainer targetContainer, IMemberBindingBehaviour memberBinding = null)
 {
     RegisterSingleton(targetContainer, typeof(TObject), memberBinding: memberBinding);
 }
Beispiel #18
0
 internal static void RegisterSingletonInternal(ITargetContainer builder, Type objectType, Type serviceType, IMemberBindingBehaviour memberBinding)
 {
     builder.Register(Target.ForType(objectType, memberBinding).Singleton(), serviceType: serviceType);
 }
Beispiel #19
0
 /// <summary>
 /// Registers an explicitly scoped instance of <typeparamref name="TObject"/> for the service type <typeparamref name="TService"/>
 /// to be created by an <see cref="Container"/> via constructor injection.
 /// The registration will auto-bind a constructor based on the services available in the <see cref="ITargetContainer"/> and
 /// <see cref="Container"/> available at the time <see cref="Container.Resolve(ResolveContext)"/> is first called.
 /// </summary>
 /// <typeparam name="TObject">The type of the object that is to be constructed when resolved.</typeparam>
 /// <typeparam name="TService">The type against which the registration will be performed.  <typeparamref name="TObject"/> must be
 /// compatible with this type.</typeparam>
 /// <param name="targetContainer">The target container on which the registration is to be performed.</param>
 /// <param name="memberBinding">Optional - provides an explicit member injection behaviour to be used when creating the instance,
 /// if different from the behaviour configured via options on the <paramref name="targetContainer"/>.</param>
 /// <remarks>This is equivalent to creating either a <see cref="ConstructorTarget"/> or <see cref="GenericConstructorTarget"/> via
 /// the <see cref="Target.ForType{T}(IMemberBindingBehaviour)"/> static method, wrapping it with a
 /// <see cref="ScopedTarget"/> and then registering it against
 /// the type <typeparamref name="TService"/>.</remarks>
 public static void RegisterScoped <TObject, TService>(this ITargetContainer targetContainer, IMemberBindingBehaviour memberBinding = null)
 {
     RegisterScoped(targetContainer, typeof(TObject), typeof(TService), memberBinding: memberBinding);
 }
Beispiel #20
0
 /// <summary>
 /// Creates a <see cref="ConstructorTarget"/> for the given constructor, or a <see cref="GenericConstructorTarget"/> if the
 /// constructor belongs to a generic type definition ('open generic type').
 /// </summary>
 /// <param name="constructor">Required.  The constructor to be bound by the target.</param>
 /// <param name="memberBinding">Optional - provides an explicit member injection behaviour to be used when creating the instance,
 /// if different from the behaviour configured via options on any target container in which the target is subsequently registered.</param>
 public static ITarget ForConstructor(
     ConstructorInfo constructor,
     IMemberBindingBehaviour memberBinding = null)
 {
     return(ForConstructor(constructor, (ParameterBinding[])null, memberBinding));
 }
Beispiel #21
0
 internal static void RegisterScopedInternal(ITargetContainer targetContainer, Type objectType, Type serviceType, IMemberBindingBehaviour memberBinding)
 {
     targetContainer.Register(Target.ForType(objectType, memberBinding).Scoped(), serviceType: serviceType);
 }
 /// <summary>
 /// Initializes a just-in-time-bound instance of the <see cref="ConstructorTarget" /> class which must be bound
 /// to the best constructor at compile-time by calling the <see cref="Bind(ICompileContext)"/> method.
 /// </summary>
 /// <param name="type">Required.  The type whose constructor is to bound.</param>
 /// <param name="namedArgs">Optional.  The named arguments which will be passed to, and used to find, the best-matched constructor.
 /// These are taken into account when the constructor is sought - with the constructor containing the most matched parameters matched being selected.</param>
 /// <param name="memberBinding">Optional - provides an explicit member injection behaviour to be used when creating the instance.
 /// If not provided, then the <see cref="Bind(ICompileContext)"/> method will attempt to obtain one via the options API from the
 /// <see cref="ICompileContext"/> - and if one is still not available, then no member binding will be performed.</param>
 /// <remarks>To compile this target, a <see cref="Compilation.ITargetCompiler"/> first calls the <see cref="Bind(ICompileContext)"/> method
 /// to discover the constructor to be executed, along with the final set of arguments to be provided to it (see <see cref="ConstructorBinding"/>).
 ///
 /// The best available constructor is defined as the constructor with the most parameters for which arguments can be resolved from the
 /// <see cref="ICompileContext" /> at compile-time to the fewest number of <see cref="ITarget" /> objects whose <see cref="ITarget.UseFallback" />
 /// is false.
 ///
 /// *An extension point will be provided in the future which will allow the constructor resolution process to be overriden*
 /// </remarks>
 public ConstructorTarget(Type type, IDictionary <string, ITarget> namedArgs = null, IMemberBindingBehaviour memberBinding = null)
     : this(type, null, memberBinding, null, namedArgs)
 {
     // it's a post-check, but the private constructor sidesteps null types and ctors to allow the
     // public constructors to do their thing.
     if (type == null)
     {
         throw new ArgumentNullException(nameof(type));
     }
 }