Home > Software design >  Calling a Command from a Separate Class File
Calling a Command from a Separate Class File

Time:01-10

So I have been at it for days, and for the life of me cannot find any documentation that fits my situation exactly here.

I have essentially set up a custom navigation service and would like to call the command from my ViewModel Class directly from my User Control.

I think I'm on the edge of having it here, but my lack of experience with C# is shooting me in the foot.

Here is the section of code from my Login.xaml.cs in question:

private LoginViewModel _loginViewModel;
        public Login(LoginViewModel loginViewModel)
        {
            _loginViewModel = loginViewModel;
        }
        private void GrantAccess()
        {
            int userAccess = Int16.Parse(User.Access);
            if (userAccess == 1)
            {
                MessageBox.Show("The bottom man");
            }
            if (userAccess == 2)
            {
                MessageBox.Show("The little boss");
            }
            if (userAccess == 3)
            {
                MessageBox.Show("The little big boss");
            }
            if (userAccess == 4)
            {
                {
                    _loginViewModel.NavigateMM1Command.Execute(null);
                }
            }

        }

and here is the command I'm trying to reference from the ViewModel:

public class LoginViewModel : BaseViewModel
    {
        public ICommand NavigateMM1Command { get; }
        public LoginViewModel(NavigationStore navigationStore)
        {
            NavigateMM1Command = new NavigateCommand<MM1ViewModel>(new NavigationService<MM1ViewModel>(navigationStore, () => new MM1ViewModel(navigationStore)));
        }
    }

Basically I've been going through tutorial after tutorial trying to apply what they teach to what I need and its worked for the most part but now _loginViewModel is throwing a null reference exception and I'm not sure why.

I have tried:

LoginViewModel loginViewModel = new loginViewModel();

but its asking me to pass a navigationStore argument through it and that feels wrong. Any help here will cure my temporary insanity XD

CodePudding user response:

You're receiving a Null Object Reference because navigationStore is null when LoginViewModel is being constructed.

That is, you have not configured a means to instantiate the type navigationStore when constructing LoginViewModel.


Dependency Injection (DI), or Invocation of Control (IoC) is bit more comprehensive a subject to cover in this answer.

Having said that, I'll provide code to review here. It represents a means to configure a service provider using a collection of type mappings.

In this complete, ConsoleApp example, we'll explicitly instantiate a ServiceCollection, add Service Types (specifying mapping where application), and Build the ServiceProvider; With that provider, we'll resolve and instantiate Login type using GetService -- instantiating all the types;

The Types are essentially mockups of the types you've specified, but I've modified some aspects (an made up notioned like what your Execute method and usage of NavigationStore was).

DemoNavTypes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleDemo.NavLoginDemo
{
    public interface ICommand 
    {
        void Execute(string? userName);
    }

    public interface INavigationStore {
        public bool this[string index] { get;set; }
    }

    public interface INavigationService {
        void GrantAccessToUser(string userName);
    }
    public interface INavigationViewModel { }

    internal class NavigationStore : INavigationStore
    {
        private Dictionary<string, bool> userAccessDict;

        public NavigationStore() { 
            userAccessDict = new Dictionary<string, bool>();
        }

        public bool this[string index] { 
            get => userAccessDict.TryGetValue(index, out var val) && val; 
            set => userAccessDict[index] = value; 
        }
    }

    internal class NavigationService : INavigationService
    {
        private readonly INavigationStore _navigationStore;

        public NavigationService(INavigationStore navigationStore) 
        {
            _navigationStore = navigationStore;
        }

        public void GrantAccessToUser(string? userName)
        {
            if (string.IsNullOrWhiteSpace(userName))
                throw new ArgumentException(nameof(userName));

            _navigationStore[userName!] = true;
        }
    }

    internal class NavigationCommand : ICommand
    {
        private readonly INavigationService _navigationService;
        public NavigationCommand(INavigationService navigationService) 
        {
            _navigationService = navigationService;
        }

        public void Execute(string? userName)
        {
            if (userName != null)
            {
                _navigationService.GrantAccessToUser(userName);
            }
        }
    }

    internal class User
    {
        public string? Name { get; set; }
        public string Access { get; set; } = "1";
    }

    public abstract class BaseViewModel
    {
        internal User User { get; set; } = new User();

        protected BaseViewModel() { }
    }

    internal class LoginViewModel : BaseViewModel, INavigationViewModel
    {
        private readonly ICommand _command;

        public LoginViewModel(ICommand command) : base()
        {
            _command = command;
        }

        internal ICommand NavigateMM1Command => _command;
    }

    internal class Login
    {

        private User User => _loginViewModel.User;

        private readonly LoginViewModel _loginViewModel;
        public Login(LoginViewModel loginViewModel) 
        { 
            _loginViewModel = loginViewModel;   
        }

        internal void SetAccess(int access)
        {
            SetAccess($"{access}");
        }
        internal void SetAccess(string access)
        {
            User.Access = access; 
        }

        internal void SetUserName(string userName) { User.Name = userName; }

        internal async Task GrantAccessAsync()
        {
            await Task.Yield();

            int userAccess = Int16.Parse(User.Access);
            switch (userAccess)
            {
                case 1:
                    Console.WriteLine("The bottom man");
                    break;
                case 2:
                    Console.WriteLine("The little boss");
                    break;
                case 3:
                    Console.WriteLine("The little big boss");
                    break;
                case 4:
                    _loginViewModel.NavigateMM1Command.Execute(User.Name);
                    break;
                default:
                    throw new NotImplementedException();
            }
        }

    }
}

Program.cs (using Microsoft.Extensions.DependencyInjection)

using Microsoft.Extensions.DependencyInjection;
using System.Collections.Immutable;
using System.ComponentModel.Design;
using System.Linq;
using ConsoleDemo.NavLoginDemo;

internal class Program
{

    private static async Task Main(string[] args)
    {
        var services = new ServiceCollection();
        var provder = ConfigureServices(services);

        var login = provder.GetService<Login>();

        if (login != null)
        {
            await login.GrantAccessAsync();
            login.SetAccess(2);
            await login.GrantAccessAsync();
            login.SetAccess(3);
            await login.GrantAccessAsync();
            login.SetUserName("James Bond");
            login.SetAccess(4);
            await login.GrantAccessAsync();
        }

    }

    private static IServiceProvider ConfigureServices(IServiceCollection services)
    {
        return services
            .AddScoped<INavigationStore, NavigationStore>()
            .AddScoped<INavigationService, NavigationService>()
            .AddScoped<ICommand, NavigationCommand>()
            .AddScoped<LoginViewModel>()
            .AddScoped<Login>()
            .BuildServiceProvider();
    }
}

Note

-- However, In your application, you'll probably already have a ServiceCollection instance in your Program.cs or Startup.cs file. And the ServiceProvider (or HostProvider) will be managed over by the application; So, you probably won't need to explicitly resolve (or GetService<T>) -- just add the Service Type (mappings) in ServiceCollection. Those parameter types will be instantiated and 'injected' into the constructor of Type that is itself being instantiated.

  • Related