Where to place AuthenticateUser method in mvvm pattern - WPF app

Kalpana 286 Reputation points
2022-03-10T02:41:41.347+00:00

Hi

I am a beginner to the MVVM design. I am migrating a conventional WPF app ( with alot of code behinds, event triggers ) to this pattern.
However I am having difficulties in understanding certain features.

My question is, say I am doing a user login screen. I need the ID entered in the textbox to be validated with the db. Where does this authentication method go, to the Model or the view model. I have used ICommand to get the value in the viewmodel. Should I place the AuthenticateUser method in the model and do a call to the method or..should I do it in the view model itself.

Appreciate the advice. Thank you.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,689 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,408 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,306 Reputation points
    2022-03-10T06:59:42.447+00:00

    Hi,

    1. you have several Views: MainWindow, user login screen (dialog window) and so on.
    2. You have Model to load and save data. Your Model have method or property IsAuthenticated.
    3. ViewModel executes whole process. ViewModel check IsAuthenticated and call ShowDialog for user login screen.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Kalpana 286 Reputation points
    2022-03-10T07:17:17.067+00:00

    Thanks,

    I have done it something like this
    class MachineAttributes : INotifyPropertyChanged
    {

            private string _workerID;
            public string WorkerID
            {
                get { return this._workerID; }
                set
                {
                    // Implement with property changed handling for INotifyPropertyChanged
                    if (!string.Equals(this._workerID, value))
                    {
                        this._workerID = value;
                        OnPropertyChanged("WorkerID");
                    }
                }
            }
    
            private int _userexists;
            public int Userexists
            {
                get
                {
                    return _userexists;
                }
                set
                {
                    _userexists = value;
                }
            }
    
    
            #region INotifyPropertyChanged Members
            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            #endregion
    
    
            public bool CheckWorkerIDExist()
            {
                DBOperations dbselect = new DBOperations(Configvar.Connstringdb2);
    
                dbselect.SPcommand = @"IF (SELECT count(Password) FROM Users Where Password = '1001') > 0 
                                    Select 1 [Exists]
                                    else
                                    Select 0 [Exists]";
                dbselect.Sqlparam = new SqlParameter[] { new SqlParameter("@workeriId", DbType.String) { Value = _workerID } };
                _userexists = dbselect.ExecuteSPSelect1();
                if(_userexists != 0)
                {
                    return true;
                }
                return false;
    
            }
        }
    
    0 comments No comments