This project is read-only.

[DI container] constructorParams in Locate(...)

Jan 6, 2014 at 6:46 PM
Hi,

I would like to create viewmodels using your DI container and Locate(). Of course, I have to somehow pass the model to the viewmodel.

I thought of doing it like this:
 [Export(typeof(ViewModel))]
    public class ViewModel: BaseViewModel
    {
        private Model _model = null;

        [ImportConstructor]
        public ViewModel(Model model)
        {
              _model = model;
        }

        [ActivationComplete]
        public void Initialize()
        {
            // do stuff 
        }
}

Somewhere in another ViewModel I call:

Model m = new Model(id, ...);
Container.LocateByType(new StyleMVVM.Data.TypeWrapper(typeof(ViewModel)), model);
I assumed that the ImportConstructor would be called using m as a paramter, as I have supplied it to Locate(). However, the import fails, as the DI container complains that there is not export strategy for Model. Obviously it isn't using the object I've passed as constructorParams.

Can you help me out here?
Thanks

SR
Jan 6, 2014 at 7:04 PM
Hi SR,

To be completely honest with you the Locate method with parameters is going to be going away. That's not to say you can't do what you want to do but in the next 2-3 months I'll be switching over to use Grace for DI injection and I chose specifically to leave that out.

That being said it doesn't really help you at this moment in time.

Let me do a little research this evening and come back with a recommendation. I think what will probably work the best for you is to stick your model inside the injection context then request your model but I need to do a little testing before I give you a code example of what I'm thinking

-Ian
Jan 7, 2014 at 1:14 AM
Hi SR,

After playing around a little bit I remembered there is an easier solution to all of this and that's importing Func<Model, ViewModel> into the ViewModel you want to do this creation in.

By default when you import a Func<TIn,TOut> the container will create a function that will take TIn and construct a TOut using the TIn.

Also in the latest version if you have only one constructor you don't need to use the ImportConstructor attribute, the default will be chosen automatically.

This is how I constructed my example
public class MainPageViewModel : PageViewModel
{
    private Func<TestDataModel, TestViewModel> viewModelFunc;

    public MainPageViewModel(Func<TestDataModel, TestViewModel> viewModelFunc)
    {
        this.viewModelFunc = viewModelFunc;
    }

    public void ClickHandler()
    {
        TestDataModel dataModel = new TestDataModel{ TestValue =  5};

        TestViewModel testViewModel = viewModelFunc(dataModel);
    }
}

public class TestViewModel : PageViewModel
{
    private TestDataModel dataModel;

    public TestViewModel(TestDataModel dataModel)
    {
        this.dataModel = dataModel;
    }
}

public class TestDataModel
{
    public int TestValue { get; set; }
}


I can send the full example you if you contact me directly. My email address is ipjohnson123 at yahoo.com

-Ian