Question First MVVM applicaiton - still a lot in ViewModel Button - advice sought

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
140
Programming Experience
Beginner
So I completed my first MVVM application.

I know it isn't great, but quite proud of actually managing to separate it all out and have it still function. It is a pretty simple string manipulation application - so not overly proud...besides I think Caliburn.Micro is doing most of the work.

I wondered if I could get some advice on the button press in the ViewModel, because it is still quite busy - not sure whether this is too busy or fine for what it is? There is no code behind my View, which I think is right and I make use of Models to do all the work - except cleaning the strings/arrays that are returned/created.

I do clean them in the Models, but because a lot of the time I am working with Regex and sometimes I am passing a string, and sometimes I am passing an Array, and while I do clean the Array when it is created as an Array in the Model, the further work on the array/string seems to be adding more empty lines - so they need cleaning again.

Any thoughts or advice from the code below, is this ok, is this too much - quite new to MVVM so any advice gratefully received.

This is in a NameViewModel, which supports a NameView which has no code behind.

C#:
public void btnNames()
        {
            strNamesOut = "";

            string noShortNames = "";
            NameIdentifier ni = new NameIdentifier(); // This is a separate model

            // Takes a string of names from  excel copied into a TextBox
            noShortNames = strNamesIn;

// this sends the data around a couple of models then sends it back
            var cleanedNames = ni.ShortNames(noShortNames);

// this removes all the duplicates and empty strings from the created array
// this model requires an array as each name is processed
// the above model and this model are used by different ViewModels some need cleaning but not variants
            string[] varNames = cleanedNames.Split('\r', '\n');
            varNames = varNames.Where(x => !string.IsNullOrEmpty(x.Trim())).ToArray();
            varNames = varNames.Distinct().ToArray();
            var variantNames = ni.Variants(varNames);

// a string is returned from the above
// since these two models are shared by different ViewModels and only this one requires Finishing
// I couldn't just send it from ni.Variants
            var finishedNames = ni.Finishing(variantNames);

// again cleans the created array - I clean above to reduce the string/array being processed
            string[] cleanNames = finishedNames.Split('\r', '\n');
            cleanNames = cleanNames.Where(x => !string.IsNullOrEmpty(x.Trim())).ToArray();

// could make this a variable but didn't see the point of creating a string builder for it.
// I do need the names output with a line break
            foreach (var finalNames in cleanNames)
            {
                strNamesOut += finalNames + Environment.NewLine;
            }
        }

Apologies, if this should be in a different place in the forum, I didn't see anything for MVVM in particular. Sorry, wasn't looking under ASP. NET....
 
Last edited:
How much of that work should really live inside the Model vs. what is meant to be in the ViewModel where it translates stuff to/from the View for the Model?
 
How much of that work should really live inside the Model vs. what is meant to be in the ViewModel where it translates stuff to/from the View for the Model?

I went through the first VIewModel, to see if I could make adjustments in the Models.

My code now looks like this - I added an if statement, a progress bar, and I used sb.Append after some testing demonstrated that this was far faster.

C#:
if (noShortNames != null)
            {
                (sender as BackgroundWorker).ReportProgress(i = 20);

                var cleanedNames = ni.ShortNames(noShortNames);
                (sender as BackgroundWorker).ReportProgress(i = 40);

                var variantNames = ni.Variants(cleanedNames);
                (sender as BackgroundWorker).ReportProgress(i = 60);

                var finishedNames = ni.PrivFinishing(variantNames);
                (sender as BackgroundWorker).ReportProgress(i = 80);
                
                foreach (var finalNames in finishedNames)
                {
                    sb.Append(finalNames + Environment.NewLine);
                }
                strNamesOut = sb.ToString();
                (sender as BackgroundWorker).ReportProgress(i = 100);
            }

Which is a lot cleaner.

Now just the other two ViewModels to go which is causing me a little of a headache but I should get there.

Thanks SkyDiver.
 
Back
Top Bottom