Пример #1
0
        private static void SetImplementationOfCalculatorCommandObjects(out ICalculationCommandReceiver receiver, out ICommandInvoker invoker)
        {
            var commands = new List <ICalculatorCommand>();

            var tmpReceiver = new TwoOperandsCalculator(new SingleNumberOutput(), new SingleNumberInput(9));

            var assemblies = new[]
            {
                Assembly.GetAssembly(typeof(ICalculatorCommand)),
            };

            foreach (var assembly in assemblies)
            {
                assembly.GetTypes()
                .Where(type => type.BaseType == typeof(BaseCommand))
                .ToList().ForEach(commandType => commands.Add((ICalculatorCommand)Activator.CreateInstance(commandType, new object[] { tmpReceiver })));
            }

            receiver = tmpReceiver;
            invoker  = new CalculatorRemote(new CommandList(commands));
        }
Пример #2
0
        private void RegisterCalculator()
        {
            var commands = new List <ICalculatorCommand>();
            ICalculationCommandReceiver calculator = new TwoOperandsCalculator(new SingleNumberOutput(), new SingleNumberInput(9));
            var assemblies = new[]
            {
                Assembly.GetAssembly(typeof(ICalculatorCommand)),
            };

            foreach (var assembly in assemblies)
            {
                assembly.GetTypes()
                .Where(type => type.BaseType == typeof(BaseCommand))
                .ToList().ForEach(commandType => commands.Add((ICalculatorCommand)Activator.CreateInstance(commandType, new object[] { calculator })));
            }

            // Register an instance of the calculator helps to preserve internal states when VM is recreated like when the language changes.
            this.iocContainer.RegisterInstance(calculator);

            ICommandInvoker calculatorRemote = new CalculatorRemote(new CommandList(commands));

            this.iocContainer.RegisterInstance(calculatorRemote);
        }