c# – Function is called without a reason

I have an wpf project with mvvm architecture. I initialize the ViewModels in my App.xaml.cs like this

public partial class App : Application
{
    private const string ConnectionString = "Example";
    
    private readonly AfferoCibiDBContextFactory DbFactory;
    private readonly NavigationStore navigationStore;

    private readonly IMealService mealService;
    private readonly IOrderService orderService;
    private readonly IUnitOfWork unitOfWork;

    public App()
    {           
        DbFactory = new AfferoCibiDBContextFactory(ConnectionString);
        unitOfWork = new UnitOfWork(DbFactory.CreateDbContext());
        navigationStore = new NavigationStore();
        mealService = new MealService(unitOfWork);
        orderService = new OrderService(unitOfWork);
    }
    protected override void OnStartup(StartupEventArgs e)
    {
        using (AfferoCibiDBContext afferoCibiDBContext = DbFactory.CreateDbContext())
        {
            afferoCibiDBContext.Database.Migrate();
        }

        navigationStore.CurrentViewModel = new AdminOrCustomerLogInViewModel(
            new NavigationService(navigationStore, CreateCustomerOrderingViewModel, CreateAdminOrCustomerLogInViewModel),
            new NavigationService(navigationStore, CreateAdminLiginViewModel, CreateAdminOrCustomerLogInViewModel),
            new NavigationService(navigationStore, CreateHelpViewModel, CreateAdminOrCustomerLogInViewModel));

        MainWindow = new MainWindow()
        {
            DataContext = new MainWindowViewModel(navigationStore)
        };
        MainWindow.Show();
        base.OnStartup(e);
    }
    private AdminOrCustomerLogInViewModel CreateAdminOrCustomerLogInViewModel()
    {
        return new AdminOrCustomerLogInViewModel(new NavigationService(navigationStore, CreateCustomerOrderingViewModel, CreateAdminOrCustomerLogInViewModel),
            new NavigationService(navigationStore, CreateAdminLiginViewModel, CreateAdminOrCustomerLogInViewModel),
            new NavigationService(navigationStore, CreateHelpViewModel, CreateAdminOrCustomerLogInViewModel));
    }
    private HelpViewModel CreateHelpViewModel()
    {
        return new HelpViewModel(new NavigationBackService(navigationStore));
    }
    private FulfillingOrdersViewModel CreateFulfillingOrdersViewModel()
    {
        return FulfillingOrdersViewModel.LoadViewModel(new NavigationService(navigationStore, CreateHelpViewModel, CreateFulfillingOrdersViewModel),
            new NavigationService(navigationStore, CreateAdminCorrectionsViewModel, CreateFulfillingOrdersViewModel), orderService);
    }
    private AdminCorrectionsViewModel CreateAdminCorrectionsViewModel()
    {
        return AdminCorrectionsViewModel.LoadViewModel(new NavigationService(navigationStore, CreateFulfillingOrdersViewModel, CreateAdminCorrectionsViewModel),
            new NavigationService(navigationStore, CreateHelpViewModel, CreateAdminCorrectionsViewModel), mealService);
    }
    private AdminLogInViewModel CreateAdminLiginViewModel()
    {
        return new AdminLogInViewModel(new NavigationService(navigationStore, CreateAdminCorrectionsViewModel, CreateAdminLiginViewModel),
            new NavigationService(navigationStore, CreateHelpViewModel, CreateAdminLiginViewModel));
    }
    private CustomerOrderingViewModel CreateCustomerOrderingViewModel()
    {
        return CustomerOrderingViewModel.LoadViewModel(new NavigationService(navigationStore, CreateCustomerListOfOrdersViewModel, CreateCustomerOrderingViewModel),
            new NavigationService(navigationStore, CreateHelpViewModel, CreateCustomerOrderingViewModel), mealService, orderService);
    }
    private CustomerListOfOrdersViewModel CreateCustomerListOfOrdersViewModel()
    {
        return CustomerListOfOrdersViewModel.LoadViewModel(new NavigationService(navigationStore, CreateHelpViewModel, CreateCustomerListOfOrdersViewModel), 
            new NavigationService(navigationStore, CreateCustomerOrderingViewModel, CreateCustomerListOfOrdersViewModel), orderService);
    }
}

I know I should use dependency injection.

The problem is when I load the CustomerOrderingViewModel which can navigate to CustomerListOfOrdersViewModel the method CreateCustomerListOfOrdersViewModel() isn’t and it shouldn’t but when I try to navigate to CustomerListOfOrdersViewModel the method CreateCustomerOrderingViewModel() is called which causes two load command to occur the same time.

this is the implementation of the methods that load the view models

CustomerOrderingViewModel:

        public static CustomerOrderingViewModel LoadViewModel(NavigationService customerListOfOrders, 
        NavigationService helpNavigationService, 
        IMealService mealService, 
        IOrderService orderService)
    {
        CustomerOrderingViewModel viewModel = new CustomerOrderingViewModel(customerListOfOrders, helpNavigationService, mealService, orderService);
        viewModel.LoadMealsCommand.Execute(null);

        return viewModel;
    }

And CustomerListOfOrdersViewModel:

public static CustomerListOfOrdersViewModel LoadViewModel(NavigationService helpNavigationService, NavigationService customerOrderingNavigationService, 
        IOrderService orderService)
    {
        CustomerListOfOrdersViewModel viewModel = new CustomerListOfOrdersViewModel(helpNavigationService, customerOrderingNavigationService, orderService);
        viewModel.LoadOrdersCommand.Execute(null);

        return viewModel;
    }

the NavigationService takes funcs as parameters

public class NavigationService
{
    private readonly NavigationStore navigationStore;
    private readonly Func<BaseViewModel> nextViewModel;
    private readonly Func<BaseViewModel> currentViewModel;

    public NavigationService(NavigationStore navigationStore, Func<BaseViewModel> nextViewModel, Func<BaseViewModel> currentViewModel)
    {
        this.navigationStore = navigationStore;
        this.nextViewModel = nextViewModel;
        this.currentViewModel = currentViewModel;
    }


    public void Navigate()
    {
        navigationStore.CurrentViewModel = nextViewModel();
        navigationStore.PrevViewModel = currentViewModel();
    }

}

Why is CreateCustomerOrderingViewModel() called when CreateCustomerListOfOrdersViewModel() is executed but CreateCustomerListOfOrdersViewModel() isn’t called when CreateCustomerOrderingViewModel() is executed.

Leave a Comment