示例#1
0
    public void Absorb(IEnumerable <Type> types)
    {
        var specDescriptors =
            (from t in types
             let descriptor = CommandSpecDescriptor.TryGetFromType(t)
                              where descriptor.Exists
                              select descriptor.Require()
            );

        var descriptorDupes = from d in specDescriptors
                              group d by d.CommandName into nameGroup
                              where nameGroup.Count() > 1
                              select nameGroup.Key;
        var descriptorDupeList = rolist(descriptorDupes);

        if (descriptorDupeList.Count != 0)
        {
            throw new Exception($"Command names are required to be unique and {descriptorDupeList.Count} duplicates were found: " + string.Join("; ", descriptorDupeList));
        }

        var specDescriptorIndex = specDescriptors.ToDictionary(x => x.CommandName);

        var executors = Patterns(types).ToList();
        var execDupes = from x in executors
                        group x by x.SpecType into g
                        where g.Count() > 1
                        select g.Key;
        var execDupeList = rolist(execDupes);

        if (execDupeList.Count != 0)
        {
            throw new Exception($"Command names are required to be unique and {execDupeList.Count} duplicates were found: " + string.Join("; ", execDupeList));
        }

        var execDescriptorIndex = executors.ToDictionary(x => x.SpecType);

        foreach (var spec in specDescriptorIndex)
        {
            var specType = spec.Value.SpecType;
            DescribedSpecs[spec.Key] = spec.Value;
            if (execDescriptorIndex.ContainsKey(specType))
            {
                var execDescriptor = execDescriptorIndex[specType];
                DescribedPatterns[specType] = execDescriptor;
                PatternCache[specType]      = (ICommandExecutionService)Activator.CreateInstance(execDescriptor.PatternType, Context);
            }
        }
    }
示例#2
0
 public static IReadOnlyList <CommandSpecDescriptor> DescribeCommands(this Assembly a)
 => rolist((from t in a.GetTypes()
            let descriptor = CommandSpecDescriptor.TryGetFromType(t)
                                 where descriptor.IsSome()
                             select descriptor.Require()));