Ejemplo n.º 1
0
        public void should_return_all_merged_actions()
        {
            var singletonAction = new SingletonBuildAction();

            // --arrange
            var matchString = new LastUnitSequenceMatcher(Match.Type <string>(null)).AddBuildAction(BuildStage.Cache, CreateByReflectionBuildAction.Instance);
            var matchAny    = new AnyUnitSequenceMatcher().AddBuildAction(BuildStage.Cache, singletonAction);

            var target = new BuildPlansCollection();

            target.Children.Add(matchString);
            target.Children.Add(matchAny);

            // --act
            var actual = target.GetBuildActions(new[] { Unit.OfType <string>() }.AsArrayTail());

            // --assert
            actual[BuildStage.Cache]
            .Should()
            .HaveCount(2)
            .And
            .Subject.Select(_ => _.Entity)
            .Should()
            .Equal(CreateByReflectionBuildAction.Instance, singletonAction);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///   Used to make a build plan for <paramref name="unitInfo"/>
        ///   How it should be treated is specified by subsequence calls using returned object.
        /// </summary>
        public static TreatingTuner Treat([NotNull] this BuildPlansCollection buildPlans, [NotNull] UnitInfo unitInfo)
        {
            if (buildPlans == null)
            {
                throw new ArgumentNullException(nameof(buildPlans));
            }

            var unitSequenceMatcher = new WildcardUnitSequenceMatcher(new UnitInfoMatcher(unitInfo));

            return(new TreatingTuner(buildPlans.AddOrGetUnitSequenceMatcher(unitSequenceMatcher)));
        }
Ejemplo n.º 3
0
        /// <summary>
        ///   Used to make a build plan for Unit of type <typeparamref name="T" />.
        ///   How <typeparamref name="T" /> should be treated is specified by subsequence calls using returned object.
        /// </summary>
        public static TreatingTuner <T> Treat <T>([NotNull] this BuildPlansCollection buildPlans, object token = null)
        {
            if (buildPlans == null)
            {
                throw new ArgumentNullException(nameof(buildPlans));
            }

            var unitSequenceMatcher = new WildcardUnitSequenceMatcher(Match.Type <T>(token));

            return(new TreatingTuner <T>(buildPlans.AddOrGetUnitSequenceMatcher(unitSequenceMatcher)));
        }
Ejemplo n.º 4
0
        /// <summary>
        ///   Used to make a build plan for a unit only if it is building in a context of building <paramref name="type" />.
        /// </summary>
        public static SequenceTuner Building([NotNull] this BuildPlansCollection buildPlans, Type type, object token = null)
        {
            if (buildPlans == null)
            {
                throw new ArgumentNullException(nameof(buildPlans));
            }

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

            return(new SequenceTuner(buildPlans.AddOrGetUnitSequenceMatcher(unitSequenceMatcher)));
        }
Ejemplo n.º 5
0
        /// <summary>
        ///   Used to add some details to build plan of any building unit. E.g. to specify what constructor to use, or register a dependency needed by any type
        ///   in the system. Usually used as a part of other build plan. See <see cref="Building{T}" /> for details.
        /// </summary>
        public static Tuner TreatAll([NotNull] this BuildPlansCollection buildPlans)
        {
            if (buildPlans == null)
            {
                throw new ArgumentNullException(nameof(buildPlans));
            }

            var unitSequenceMatcher = new AnyUnitSequenceMatcher();

            return(new Tuner(buildPlans.AddOrGetUnitSequenceMatcher(unitSequenceMatcher)));
        }
Ejemplo n.º 6
0
 public static void PrintBuildPlans(this BuildPlansCollection buildPlansCollection)
 {
     using (Enabled())
     {
         foreach (var unitSequenceMatcher in buildPlansCollection.Children)
         {
             using (Block(LogLevel.Info, unitSequenceMatcher.ToString()))
             {
                 Print(unitSequenceMatcher);
             }
         }
     }
 }
Ejemplo n.º 7
0
        private static TreatingTuner Treat(BuildPlansCollection buildPlans, UnitInfo unitInfo)
        {
            if (buildPlans == null)
            {
                throw new ArgumentNullException(nameof(buildPlans));
            }

            var unitMatcher = new MockUnitMatcher(new UnitInfoMatcher(unitInfo));

            var unitSequenceMatcher = new WildcardUnitSequenceMatcher(unitMatcher);

            return(new TreatingTuner(buildPlans.AddOrGetUnitSequenceMatcher(unitSequenceMatcher)));
        }
Ejemplo n.º 8
0
        /// <summary>
        ///   Used to make a build plan for all inheritors of <typeparamref name="T" />.
        ///   How <typeparamref name="T" /> should be treated is specified by subsequence calls using returned object.
        /// </summary>
        public static TreatingTuner <T> TreatInheritorsOf <T>([NotNull] this BuildPlansCollection buildPlans, object token = null)
        {
            if (buildPlans == null)
            {
                throw new ArgumentNullException(nameof(buildPlans));
            }

            var unitSequenceMatcher = new WildcardUnitSequenceMatcher(
                new BaseTypeMatcher(new UnitInfo(typeof(T), token)),
                UnitSequenceMatchingWeight.WildcardMatchingBaseTypeUnit);

            return(new TreatingTuner <T>(buildPlans.AddOrGetUnitSequenceMatcher(unitSequenceMatcher)));
        }
Ejemplo n.º 9
0
        /// <summary>
        ///   Used to make a build plan for whole class of open generic types.
        ///   How <paramref name="openGenericType" /> should be treated is specified by subsequence calls using returned object.
        /// </summary>
        public static TreatingOpenGenericTuner TreatOpenGeneric([NotNull] this BuildPlansCollection buildPlans, Type openGenericType, object token = null)
        {
            if (buildPlans == null)
            {
                throw new ArgumentNullException(nameof(buildPlans));
            }

            var unitSequenceMatcher = new WildcardUnitSequenceMatcher(
                Match.OpenGenericType(openGenericType, token),
                UnitSequenceMatchingWeight.WildcardMatchingOpenGenericUnit);

            return(new TreatingOpenGenericTuner(buildPlans.AddOrGetUnitSequenceMatcher(unitSequenceMatcher)));
        }
Ejemplo n.º 10
0
        /// <summary>
        ///   Used to override a build plan for <paramref name="type"/>
        ///   How it should be treated is specified by subsequence calls using returned object.
        /// </summary>
        public static TreatingTuner Override([NotNull] this BuildPlansCollection buildPlans, [NotNull] Type type, object token = null)
        {
            if (buildPlans == null)
            {
                throw new ArgumentNullException(nameof(buildPlans));
            }

            var newSequenceMatcher = new WildcardUnitSequenceMatcher(Match.Type(type, token));
            var oldSequenceMatcher = buildPlans.Children.Single(_ => _.Equals(newSequenceMatcher));

            buildPlans.Children.Remove(oldSequenceMatcher);

            return(new TreatingTuner(buildPlans.AddOrGetUnitSequenceMatcher(newSequenceMatcher)));
        }
Ejemplo n.º 11
0
        private static T Build <T>([NotNull] this Builder builder, [CanBeNull] object token, [CanBeNull] params object[] parameters)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            BuildPlansCollection sessionalBuildPlans = null;

            if (parameters != null && parameters.Length > 0)
            {
                sessionalBuildPlans = new BuildPlansCollection();
                sessionalBuildPlans
                .TreatAll()
                .UsingParameters(parameters);
            }

            var unitInfo    = new UnitInfo(typeof(T), token);
            var buildResult = builder.BuildUnit(unitInfo, sessionalBuildPlans);

            return(buildResult.HasValue
        ? (T)buildResult.Value
        : throw new ArmatureException("Can't build unit").AddData("unitInfo", unitInfo));
        }
Ejemplo n.º 12
0
 /// <summary>
 ///   Used to make a build plan for a unit only if it is building in a context of building <typeparamref name="T" />.
 /// </summary>
 public static SequenceTuner Building <T>(this BuildPlansCollection buildPlans, object token = null) => buildPlans.Building(typeof(T), token);