Click here to Skip to main content
15,880,469 members

CompositeCommand with Feedback of ICommands

ChrisTopherus asked:

Open original thread
Hi,

in my masterthesis i decided to use some .net techniques. The appication is written in c#/wpf with the prism library regarding to architekture. Now i got a problem on the feedback of single commands, seperated executed in a composite command.

I want to import some huge files for my model so i need some kind of feedback. I have written an ICommand for each file and made it asynchronously running with the async/await operators. An example of these files look like this:


C#
class ImportMaterialFile : ICommand
{
    #region Members

    ImportFileViewModel materialFile;
    public ObservableCollection<ImportErrorViewModel> errors = new ObservableCollection<ImportErrorViewModel>();

    Int16 numberOfLines = 0;
    Int16 processedLines = 0;

    #endregion

    #region ICommand Members

    public bool CanExecute(object param)
    {
        var container = ServiceLocator.Current.GetInstance<IUnityContainer>();
        InterfaceViewModel interfaceViewModel = (InterfaceViewModel)container.Resolve<IInterfaceViewModel>();

        bool CanExecute = false;

        foreach (ImportFileViewModel importFile in interfaceViewModel.SapFeliosInterfaceFiles)
        {
            if (importFile.FileName.Split('.').First().Contains("MAT"))
            {
                materialFile = importFile;
                try
                {
                    StreamReader reader = new StreamReader(materialFile.FilePath);
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        numberOfLines++;
                    }
                    CanExecute = true;
                }
                catch (Exception ex)
                {
                    errors.Add(new ImportErrorViewModel(ex.ToString(), materialFile.FileName, 0));
                }
            }
        }

        return CanExecute;
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public async void Execute(object param)
    {
        await ImportAsync();
    }

    #endregion

    #region Private Methods

    private Task ImportAsync()
    {
        return Task.Run(() => Import());
    }

    private void Import()
    {
        var container = ServiceLocator.Current.GetInstance<IUnityContainer>();
        InterfaceViewModel interfaceViewModel = (InterfaceViewModel)container.Resolve<IInterfaceViewModel>();

        try
        {
            StreamReader streamReader = new StreamReader(materialFile.FilePath);
            String line;
            Int16 counter = 1;

            Material material;

            while ((line = streamReader.ReadLine()) != null)
            {
                try
                {
                    material = new Material();

                    //set properties from line

                    interfaceViewModel.ModelSapFeliosContainer.MaterialSet.Add(material);

                    counter++;
                    processedLines++;
                }
                catch (Exception ex)
                {
                    errors.Add(new ImportErrorViewModel(ex.ToString(), materialFile.FileName, counter));
                }
            }

            interfaceViewModel.ModelSapFeliosContainer.SaveChanges();
        }
        catch (Exception ex)
        {
            MessageBox.Show("error while reading MAT-File\n\n" + ex.ToString());
        }
    }

    #endregion
}


I have to execute three of these files in a compositecommand which looks like this.

C#
class ImportSapFeliosInterfaceFiles : CompositeCommand
    {
        #region Members

        ImportProcessingPointFile importProcessingPointFile = new ImportProcessingPointFile();
        ImportMaterialFile importMaterialFile = new ImportMaterialFile();
        ImportProductionOrderFile importProductionOrderFile = new ImportProductionOrderFile();
        ImportProductionOrderStepFile importProductionOrderStepFile = new ImportProductionOrderStepFile();

        ObservableCollection<ImportErrorViewModel> errors = new ObservableCollection<ImportErrorViewModel>();
        
        #endregion

        #region Construction

        public ImportSapFeliosInterfaceFiles()
        {
            this.RegisterCommand(importMaterialFile);
            this.RegisterCommand(importProductionOrderFile);
            this.RegisterCommand(importProductionOrderStepFile);
        }

        #endregion

        #region Properties

        public ObservableCollection<ImportErrorViewModel> GetErrors()
        {
            foreach (ImportErrorViewModel ie in importMaterialFile.errors)
            {
                errors.Add(ie);
            }

            foreach (ImportErrorViewModel ie in importProductionOrderFile.errors)
            {
                errors.Add(ie);
            }

            foreach (ImportErrorViewModel ie in importProductionOrderStepFile.errors)
            {
                errors.Add(ie);
            }

            return this.errors;
        }

        #endregion
    }


Now, my actual Problem is, that i want to have one View, giving me a feedback on whats going on. I cant figure out, how to to this. Is this the point where i have to use prisms eventaggregator? Or can i just signature my ICommands as an observableobject?

Some tips, ideas etc would be nice ;)
Tags: C#, WPF, PRISM

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900