Search code examples
c#wpfmvvmdependency-injectionunity-container

How to show child window using Unity framework with a viewmodel of two constructors?


I'm learning MVVM and I get really rough times understanding some of his concept. My question is how to show a child window which his view model accept to constructors one for adding new object which initialize the object with new object and an other for modifie the object which passed with the second constructor. and I don't know if my way using Unity is the right way. So basically I have two questions:

  1. Is the way I use unity right?
  2. How to open child window that his view model have tow constructors, one for edite mode and an other for adding mode?

this my code in app.xaml.cs:

public partial class App : Application
{
    public static readonly UnityContainer container = new UnityContainer();

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        container.RegisterType<IMemberRepository, MemberRepository>();

        container.RegisterType<ShellViewModel>();

        var window = new Shell
        {
            DataContext = container.Resolve<ShellViewModel>()
        };

        window.ShowDialog();
    }

}

and this is my code in SellViewModel for open child window:

public ShellViewModel()
{
    NewMember = new DelegateCommand(ShowMethod);
}

private void ShowMethod()
{
    var child = new AddMemberView
    {
        DataContext = App.container.Resolve<AddMemberViewModel>()
    };
    child.ShowDialog();
}

And this constructor in my child window:

public AddMemberViewModel(IMemberRepository repo, Member member = null)
{
    _repo = repo;
    SaveCommand = new DelegateCommand(SaveMember, CanSave);
    if (member == null)
        _Member = new Member();
    else
        _Member = member;
}

Edit: her I using only one constructors but in both cases how to passed the Member object in my case?


Solution

  • I think there is a misunderstanding of the usage of unity container, I suspect that you expect the unity container to act like a Factory pattern. Actually it is more like the Singleton pattern (not exactly the same but closer than Facotry). I propose in your context to change a little bit your way to instantiate the view (and view model)

    private void ShowMethod()
    {
        AddMemberViewModel vm = App.container.Resolve<AddMemberViewModel>();
        vm.Member = new Member(); // Replace here with your Member object you want 
                                  // to edit
        var child = new AddMemberView
        {
            DataContext = vm
        };
        child.ShowDialog();
    }
    

    You can also forget about registering this specific view model in the container (as it seems that it should not have a unique instance across your application) and use it lile this

    private void ShowMethod()
    {
        IMemberRepository repo = App.container.Resolve<IMemberRepository>();
    
        // Replace 'new Member()' here with your Member object you want to edit
        AddMemberViewModel vm = new AddMemberViewModel(repo, new Member())
    
        var child = new AddMemberView
        {
            DataContext = vm
        };
        child.ShowDialog();
    }