0

I am trying to understand what is the point of declaring interface upon specific ViewModel. The only advantage I could think of is that we can specify common variables that we can use to our ViewModel that is used for design time purposes. This way we can be sure, that both (runtime & design time view models) will have the same variables with the same name.

Is there any other advantage to this?

1
  • 1
    Testing! Probably the most important reason for that type of abstraction, along with -and related to- Dependency Injection (not answering because Vlad pretty much covered all this) Commented Sep 3, 2015 at 15:03

2 Answers 2

4

One of the advantages to use interface is to use DI. This way you can specify in IoC container what concrete VM should be injected for that specific interface. Another advantage of using interface in VM is for unit tests when you need to mock your VM and instead of calling concrete VM you mock it with you mocking library (e.g. moq)

Sign up to request clarification or add additional context in comments.

Comments

1

One thing I need the interface for is that I have a list of different view models that have some common properties like a "caption" that can be used as the header content when they are displayed as a tab control. While this basically could be done without the interface (by using a list of object), it gives me more confidence that there will be less runtime errors.

Common interfaces like IDisposable etc. are also something one comes along more frequently.

Another thing is if the view needs to interact with the view model (e.g. notify when the user clicks the "close" button). An interface may provide methods that can be invoked by the view in this case.

As Vlad already mentioned: Using a interface will make it easier to mock it (but only if all properties are in the interface!).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.