2

I have seen lots of blog about MVVM vs MVC and blogs that says thay MVVM is like MVC extension in Windows.

I have one design issue, I have made one win application that uses MVVM , now i have to create a same application in Web, so i decided to MVC pattern in Asp.net, but now again i am stuck at point.

how to reuse ViewModels in MVC? because ViewModel has imports namespace System.Windows.Input.

Is there any alternative soultions?? My primary requirement is reusing application logic?

I have used http://waf.codeplex.com/ WpfApplication framework as refrence.

Thanks in advance.

tereško
  • 58,060
  • 25
  • 98
  • 150
Miraj Baldha
  • 297
  • 1
  • 2
  • 13

2 Answers2

5

The fact is that the two aren't interchangeable. Nor are they supposed to be (in a practical sense). Yes, you could create ViewModels that could also be reused in ASP.NET MVC. But the amount of abstraction you would have to incorporate would make the exercise useless.

You should not waste your time worrying about this incompatibility, working hard to reduce/eliminate it.

What you should do is move truly reusable code that has no dependencies on either WPF or ASP.NET into reusable libraries. This code is what should be reused in both applications. Your ViewModels and Controllers belong with the application frameworks they were designed to service.

2

Your business logic should be in your Models, not in your View Models. So it's the Models you want to reuse, nothing else. View Models are specific to your Windows application, and are equivalent to Controllers which are specific to a web application.

Tim Rogers
  • 21,297
  • 6
  • 52
  • 68
  • 1
    @Will Sorry that should be "Business logic". In which case, I'd agree with your post - i.e. reuse business logic, but don't try and reuse application logic. – Tim Rogers Jul 25 '11 at 13:54
  • So far this is the best advice I've found on this topic, but how do you handle sharing the model accross different platforms without duplicating their definitions to handle things like validation and serialization with data annotations and attributes? I hoped to define these in one place (model) and reuse them can't find any info on the best way to do this. any ideas or resources on this topic? – SelAromDotNet Nov 27 '12 at 17:05
  • @Josh Not sure quite what you're after, but to share the model, put it in its own project and reuse it. Data annotations and serialization attributes are designed to be platform-independent: i.e. the rules for a valid model will be the same whatever, so declare them on your model. Where and how you validate depends on the technology, so trigger validation from your host application. – Tim Rogers Nov 27 '12 at 18:48
  • what I did originally was put model and viewmodel classes in a portable library to use in win8 and wp7/8. worked like a charm! but now I want to reuse as much of this for my asp.net mvc version of the project. I'm guessing I can reuse my models, but they also have the inotifyproperty stuff (for databinding) is there any danger in resuing them for the web? – SelAromDotNet Nov 27 '12 at 19:33
  • @Josh `INotifyPropertyChanged` is not technology-specific: it's just a pattern for raising change events. So reuse away. – Tim Rogers Nov 27 '12 at 19:37
  • thanks for your input, I'm going to play around with it, if it works out i'll report my results in a blog post. many thanks again to all invovled in this thread! – SelAromDotNet Nov 27 '12 at 20:44