Proper initialization of MVVM instance - constructor model parameter?

Leonard Harris 6 Reputation points
2021-02-09T05:33:40.167+00:00

Question regarding the proper use of MVVM in Xamarin as I have seen many ways to do this but say I have a view model - FooViewModel and a model - Foo.

If I were to initialize a viewmodel instance is it wrong to do something like this:

public class FooViewModel : BindableBase { public string SomeProperty { get { return _someProperty ; } set { SetProperty(ref _someProperty, value); } } private string _someProperty = string.Empty; }
then when I create new viewmodel instances I can do this:

new FooViewModel(){SomeProperty = Foo.MyPropery}

or this better
`
public Foo Foo { get; set; }
public FooViewModel(Foo foo)
{
Foo = foo;
}

    public string SomeProperty => Foo.MyPropery;

`

then when I create the new ViewModel instance I pass the model into the constructor.

From other peoples comments I have heard its better to keep the constructor for dependency injection only. I feel this is fairly trivial and both methods works but what's best approach and if it depends can it please be explained why. I'm just trying to standardize my method for creating viewmodels and do things that make sense.

Developer technologies .NET Xamarin
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.