0
votes

I am learning to use MVVM Light and I am making a program which uses database-first model with Entity Framework.

I have a DataGrid which is bound to a ViewModel, which gets data from a Repository.I am trying to make a button which when clicked opens a window, that is bound to its own View Model and lets you add a record to the DataGrid by adding it to the repository, that has an event, called when an item is added so it can update the DataGridViewModel.

Everything here is fine. I open the form, type in the data, press add and the record gets added to the database and the DataGrid gets refreshed automatically by the repository event.

The problem comes when I open more than one "Add Record" windows. When I type something in one of the windows, it appears in the other one.

I am aware that this is happening because both of the instances of the "Add Record" window are bound to the same instance of the View Model.

Is there some way to resolve this problem so every window can be independent? I know this does not look like much of a problem at the moment, but in the future I am planning to use that same window with the same view model to edit records (is that a good approach and can you recommend me some way of passing the record id or the record itself from the DataGrid window to the Add/Edit Record window?).

Thanks in advance! Tell me if I have missed something and I will add it:)

Edit: Ok this is what I came up with:

public EditParentViewModel EditParent
    {
        get
        {
            EditParentViewModel editParentViewModelInstance = new EditParentViewModel(ServiceLocator.Current.GetInstance<IParentsRepository>(),
                ServiceLocator.Current.GetInstance<IChildrenRepository>(),
                ServiceLocator.Current.GetInstance<IBailiffsRepository>());
            return editParentViewModelInstance;
        }
    }

It is working but is this a correct way to supply the repositories to the View Model?

1
Is the view model singleton? It's ok to create a new view model for each dialog instance - Joao
Yes, It is singleton. I am using the Simple IOC MVVM Light provides, so I am not sure how I should create a separate instance of viewmodel for each instance of the window - Phoenix

1 Answers

0
votes

Counting that you are using ViewModelLocator, you should have something like this:

public class ViewModelLocator
{
    public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        SimpleIoc.Default.Register<AddRecordViewModel>();
    }

    public AddRecordViewModel AddRecordViewModel
    {
        get { return ServiceLocator.Current.GetInstance<AddRecordViewModel>(); }
    }
}

Just return a new view model every time:

public class ViewModelLocator
{
    public ViewModelLocator()
    {
    }

    public AddRecordViewModel AddRecordViewModel
    {
        get { return new AddRecordViewModel(); }
    }
}

That way your form always gets a new view model instead of getting the current instance allocated on your ServiceLocator.