Exemple #1
0
        /// <summary>
        ///   Provided values will be used to inject the into created object. See <see cref="ForParameter" /> for details
        /// </summary>
        public Tuner UsingParameters(params object[] values)
        {
            if (values == null || values.Length == 0)
            {
                throw new Exception("null");
            }

            foreach (var parameter in values)
            {
                if (parameter is IParameterValueBuildPlan buildPlan)
                {
                    buildPlan.Apply(UnitSequenceMatcher);
                }
                else if (parameter is IBuildPlan)
                {
                    throw new ArmatureException("IParameterValueBuildPlan or plain object value expected");
                }
                else
                {
                    UnitSequenceMatcher
                    .AddOrGetUnitSequenceMatcher(new LastUnitSequenceMatcher(new ParameterByValueMatcher(parameter), InjectPointMatchingWeight.WeakTypedParameter))
                    .AddBuildAction(BuildStage.Create, new SingletonBuildAction(parameter));
                }
            }

            return(this);
        }
Exemple #2
0
 /// <summary>
 ///   Instantiate a Unit using a constructor with the biggest number of parameters
 /// </summary>
 public Tuner UsingLongestConstructor()
 {
     UnitSequenceMatcher
     .AddOrGetUnitSequenceMatcher(new LastUnitSequenceMatcher(ConstructorMatcher.Instance))
     .AddBuildAction(BuildStage.Create, GetLongestConstructorBuildAction.Instance);
     return(this);
 }
Exemple #3
0
 /// <summary>
 ///   Instantiate a Unit using a constructor marked with <see cref="InjectAttribute" />(<paramref name="injectionPointId" />)
 /// </summary>
 public Tuner UsingInjectPointConstructor(object injectionPointId)
 {
     UnitSequenceMatcher
     .AddOrGetUnitSequenceMatcher(new LastUnitSequenceMatcher(ConstructorMatcher.Instance))
     .AddBuildAction(BuildStage.Create, new GetInjectPointConstructorBuildAction(injectionPointId));
     return(this);
 }
Exemple #4
0
 /// <summary>
 ///   Instantiate a Unit using constructor with exact set of parameters as provided in <paramref name="parameterTypes" />
 /// </summary>
 public Tuner UsingConstructorWithParameters(params Type[] parameterTypes)
 {
     UnitSequenceMatcher
     .AddOrGetUnitSequenceMatcher(new LastUnitSequenceMatcher(ConstructorMatcher.Instance))
     .AddBuildAction(BuildStage.Create, new GetConstructorByParameterTypesBuildAction(parameterTypes));
     return(this);
 }
Exemple #5
0
        /// <summary>
        ///   Specifies that unit of type passed into <see cref="TreatingTuner{T}.As(System.Type,object)"/> or <see cref="TreatingTuner{T}.As{TRedirect}"/> should
        ///   be created using reflection
        /// </summary>
        /// <returns></returns>
        public Tuner CreatedByReflection()
        {
            var sequenceMatcher = new StrictUnitSequenceMatcher(Match.Type(Type, Token));

            UnitSequenceMatcher
            .AddOrGetUnitSequenceMatcher(sequenceMatcher)
            .AddBuildAction(BuildStage.Create, CreateByReflectionBuildAction.Instance);
            return(new Tuner(sequenceMatcher));
        }
Exemple #6
0
        /// <summary>
        ///   Specifies that unit of type passed into <see cref="TreatingTuner{T}.As(System.Type,object)"/> or <see cref="TreatingTuner{T}.As{TRedirect}"/>
        ///   should be created using default creation strategy specified in <see cref="Default.CreationBuildAction" />
        /// </summary>
        public Tuner CreatedByDefault()
        {
            var sequenceMatcher = new StrictUnitSequenceMatcher(Match.Type(Type, Token));

            UnitSequenceMatcher
            .AddOrGetUnitSequenceMatcher(sequenceMatcher)
            .AddBuildAction(BuildStage.Create, Default.CreationBuildAction);
            return(new Tuner(sequenceMatcher));
        }
Exemple #7
0
        public Tuner CreatedByReflection()
        {
            var childMatcher = new WildcardUnitSequenceMatcher(Match.OpenGenericType(OpenGenericType, Token), UnitSequenceMatchingWeight.WildcardMatchingUnit - 1);

            UnitSequenceMatcher
            .AddOrGetUnitSequenceMatcher(childMatcher)
            .AddBuildAction(BuildStage.Create, CreateByReflectionBuildAction.Instance);

            return(new Tuner(childMatcher));
        }
Exemple #8
0
        /// <summary>
        ///   Used to make a build plan for a unit only if it is building in a context of building <paramref name="type" /> with token <paramref name="token" />
        /// </summary>
        public SequenceTuner Building([NotNull] Type type, object token = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var unitSequenceMatcher = new WildcardUnitSequenceMatcher(Match.Type(type, token));

            return(new SequenceTuner(UnitSequenceMatcher.AddOrGetUnitSequenceMatcher(unitSequenceMatcher)));
        }
Exemple #9
0
        /// <summary>
        ///   Provided values will be injected into properties of the created object.  See <see cref="ForProperty" /> for details.
        ///   Also value can be a build plan returned by one of the method of the <see cref="Property" /> class, which specifies properties to inject dependencies.
        /// </summary>
        public Tuner InjectProperty(params object[] values)
        {
            UnitSequenceMatcher.AddBuildAction(BuildStage.Initialize, InjectIntoPropertiesBuildAction.Instance);

            foreach (var value in values)
            {
                if (value is IPropertyValueBuildPlan buildPlan)
                {
                    buildPlan.Apply(UnitSequenceMatcher);
                }
                else if (value is IBuildPlan)
                {
                    throw new ArmatureException("IPropertyValueBuildPlan or plain object value expected");
                }
                else
                {
                    UnitSequenceMatcher
                    .AddOrGetUnitSequenceMatcher(new LastUnitSequenceMatcher(new PropertyByValueMatcher(value), InjectPointMatchingWeight.WeakTypedParameter))
                    .AddBuildAction(BuildStage.Create, new SingletonBuildAction(value));
                }
            }

            return(this);
        }
Exemple #10
0
 /// <summary>
 ///   Register Unit as an eternal singleton <see cref="SingletonBuildAction" /> for details
 /// </summary>
 public void AsSingleton() => UnitSequenceMatcher.AddBuildAction(BuildStage.Cache, new SingletonBuildAction());
 public Tuner AsIs()
 {
     UnitSequenceMatcher.AddBuildAction(BuildStage.Create, Default.CreationBuildAction);
     return(new Tuner(UnitSequenceMatcher));
 }
 /// <summary>
 ///   When generic type belonging to class described by open generic type passed to <see cref="BuildPlansCollectionExtension.TreatOpenGeneric"/>
 ///   is requested to inject, object of generic type <paramref name="openGenericType"/> injected. Tune how it is created by subsequence tuner calls.
 /// </summary>
 public OpenGenericCreationTuner As(Type openGenericType, object token = null)
 {
     UnitSequenceMatcher.AddBuildAction(BuildStage.Create, new RedirectOpenGenericTypeBuildAction(openGenericType, token));
     return(new OpenGenericCreationTuner(UnitSequenceMatcher, openGenericType, token));
 }
Exemple #13
0
        /// <summary>
        ///   Used to add some details to build plan of any building unit in context of currently building one
        /// </summary>
        public Tuner TreatAll()
        {
            var unitSequenceMatcher = new AnyUnitSequenceMatcher();

            return(new Tuner(UnitSequenceMatcher.AddOrGetUnitSequenceMatcher(unitSequenceMatcher)));
        }
Exemple #14
0
        /// <summary>
        ///   Used to make a build plan for <typeparamref name="T" />.
        ///   How <typeparamref name="T" /> should be treated is specified by subsequence calls using returned object
        /// </summary>
        public TreatingTuner <T> Treat <T>(object token = null)
        {
            var unitSequenceMatcher = new WildcardUnitSequenceMatcher(Match.Type <T>(token));

            return(new TreatingTuner <T>(UnitSequenceMatcher.AddOrGetUnitSequenceMatcher(unitSequenceMatcher)));
        }
Exemple #15
0
        /// <summary>
        ///   Used to make a build plan for Unit of type <paramref name="type"/>.
        ///   How it should be treated is specified by subsequence calls using returned object.
        /// </summary>
        public TreatingTuner Treat([NotNull] Type type, object token = null)
        {
            var unitSequenceMatcher = new WildcardUnitSequenceMatcher(Match.Type(type, token));

            return(new TreatingTuner(UnitSequenceMatcher.AddOrGetUnitSequenceMatcher(unitSequenceMatcher)));
        }