Controller.ExecuteCommand C# (CSharp) Method

ExecuteCommand() public method

public ExecuteCommand ( IMessage note ) : void
note IMessage
return void
    public virtual void ExecuteCommand(IMessage note)
    {
        Type commandType = null;
        List<IView> views = null;
        lock (mSyncRoot)
        {
            if (mCommandMap.ContainsKey(note.Name))
            {
                commandType = mCommandMap[note.Name];
            }
            else
            {
                views = new List<IView>();
                foreach (var de in mViewCmdMap)
                {
                    if (de.Value.Contains(note.Name))
                    {
                        views.Add(de.Key);
                    }
                }
            }
        }
        if (commandType != null)
        {
            object commandInstance = Activator.CreateInstance(commandType);
            if (commandInstance is ICommand)
            {
                ((ICommand)commandInstance).Execute(note);
            }
        }
        if (views != null && views.Count > 0)
        {
            for (int i = 0; i < views.Count; ++i)
            {
                views[i].OnMessage(note);
            }
            views = null;
        }
    }

Usage Example

        //important!!!!!!!!!!!!
        //we are sorry we didn't implement the mvvm for the main window as we should, we didn't have enough time to fix it.
        //we are aware of this mistake
        //but we implemented the mvvm for the other modules
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new ViewModels.MainWindowViewModel();

            ComunicationClient client     = ComunicationClient.GetClient(8000);
            Controller         controller = new Controller(LogModel.getModel(), SettingsModel.getModel());

            try
            {
                client.ConnectToServer();
                string[] strs = { };
                client.CommandReceived += delegate(object senderObj, CommandReceivedEventArgs args)
                {
                    App.Current.Dispatcher.Invoke((Action) delegate // <--- HERE
                    {
                        JsonCommand jCommand = args.JsonCommand;
                        controller.ExecuteCommand(jCommand.CommandID, jCommand.Args, jCommand.JsonData);
                    });
                };
                client.sendCommand((int)CommandsEnum.GetConfigCommand, strs);
                client.sendCommand((int)CommandsEnum.LogsCommand, strs);
            }
            catch
            {
                this.Background = Brushes.Gray;
                this.IsEnabled  = false;
            }
        }
All Usage Examples Of Controller::ExecuteCommand