Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC with Knockout and Web API: does it make sense?

does it make sense to use KnockoutJS Viewmodels in combination with ASP.NET MVC3 or 4? Because it is not very DRY, isn't it? I have to write models for EF, Viewmodels for the MVC Views and Viewmodels for Knockout... and i lose a lot of magic. Automatic client-side validations for example.

Does it make sense to use MVC at all if one sticks with the MVVM Pattern?

like image 982
Matze-Berlin Avatar asked Mar 23 '12 11:03

Matze-Berlin


People also ask

Should I use Web API or MVC?

You should use Web API over ASP.Net MVC if you would want your controller to return data in multiple formats like, JSON, XML, etc. Also, specifying the data format in Web API is simple and easy to configure. Web API also scores over ASP.Net MVC in its ability to be self-hosted (similar to WCF).

Which of the following architecture is implemented by knockout?

Knockout. js is a JavaScript implementation of the MVVM pattern with templates. There are several advantages of Knockout. js and its MVVM architecture design.

What is Knockout JS in asp net?

Knockout. Js (simply KO) is a powerful JavaScript library which allows developers to bind DOM elements with any data model like array, Json etc.


2 Answers

With Knockout Mapping, you can automatically generate a KO view model from your MVC view model.

This is a proper pattern: your models are raw entities, your data. Your views are the UI. And your view models are your models adapted to that specific view.

like image 144
Judah Gabriel Himango Avatar answered Nov 08 '22 02:11

Judah Gabriel Himango


This may be an unpopular answer, but I don't use ko.mapping to translate my C# POCOs into JS viewmodels. Two reasons, really.

The first is a lack of control. ko.mapping will turn everything into an observable if you let it. This can result in a lot of overhead for fields that just don't need to be observable.

Second reason is about extensibility. Sure, ko.mapping may translate my C# POCOS into JS objects with observable properties. That's fine until the point you want a JS method, which at some point, you invariably will.

In a previous project, I was actually adding extra methods to ko.mapped objects programmatically. At that point, I questioned whether ko.mapping was really creating more problems than it solves.

I take on board your DRY concerns, but then, I have different domain-focused versions of my POCOs anyway. For example a MyProject.Users.User object served up by a UserController might be very different from a MyProject.Articles.User. The user in the Users namespace might contain a lot of stuff that is related to user administration. The User object in the Articles namespace might just be a simple lookup to indicate the author of an article. I don't see this approach as a violation of the DRY principle; rather a means of looking at the same concept in two different ways.

It's more upfront work, but it means I have problem-specific representations of User that do not pollute each others' implementations.

And so it is with Javascript view models. They are not C# POCOs. They're a specific take on a concept suited to a specific purpose; holding and operating on client side data. While ko.mapping will initially give you what seems to be a productivity boost, I think it is better to hand-craft specific view-models designed for the client.

btw, I use exactly the same MVC3/KnockoutJS strategy as yourself.

like image 38
Paul Alan Taylor Avatar answered Nov 08 '22 01:11

Paul Alan Taylor