4

I'm trying to understand modern web applications architecture. In ASP.NET MVC, all the business logic classes are in Model and Controller takes and guides user requests.If I am using it, is it possible to use Angular JS which itself is a MVC architecture but all the business logic is in controller and model is just POJO. Can Angular JS be used only with Web API 2 where it gets data from a RESTful service and it does all manipulation on the client side? Which architecture is most commonly used ?

CodeBeginner
  • 43
  • 1
  • 3
  • You can find [this question](http://stackoverflow.com/questions/29919834/asp-net-mvc-and-angularjs-together-asp-net-web-api) useful. To sum up: there is a lot of possible combinations, choose something that fit your needs. – Sergey Kolodiy Jul 07 '15 at 15:40
  • The way I see it, ASP.net is concerned with managing the interaction with the business logic of the application, Angular is concerned with managing the User Experience (the UI). These are not competing goals, and Angular can be used as a SPA with support of a server, or used as a new app on every page, to provide support to a server heavy workflow. – Claies Jul 07 '15 at 16:04

3 Answers3

1

In ASP.NET MVC, all the business logic classes are in Model and Controller takes and guides user requests

This is not true. Models (a.k.a. ViewModels) should be plain ("POCO") data containers in ASP.NET MVC. Business logic should come from a layer consumed by the controllers, but in smaller applications or before refactoring, the controller is a more appropriate choice than a (View)Model.

Can Angular JS be used only with Web API 2 where it gets data from a RESTful service and it does all manipulation on the client side? Which architecture is most commonly used?

No, you can use ASP.NET MVC with Angular JS as well by returning JsonResults from controller actions. That said, WebAPI is a better / more appropriate choice. Of course you can also use Angular with many other non-Microsoft tools that return JSON over http(s) (for example node, ruby, java, etc).

To more directly answer your actual question, the main difference between the two is that one is an MVC pattern for the server, and the other is an MVC pattern for the browser. Angular actually likes to call it an "MVW" pattern, where the "W" stands for "Whatever".

danludwig
  • 46,965
  • 25
  • 159
  • 237
  • That is a very sweeping statement "This is not true. Models should be plain ("POCO") data containers in ASP.NET MVC. Business logic should either come in controller actions, or in another layer that the controllers consume." ... Controllers should not contain business logic in my opinion and "models" should not always be plain poco objects... – BenjaminPaul Jul 07 '15 at 15:51
  • Ok I refered to this- https://msdn.microsoft.com/en-us/library/dd381412(v=vs.108).aspx – CodeBeginner Jul 07 '15 at 15:51
  • "In ASP.NET MVC, all the business logic classes are in Model and Controller takes and guides user requests" << This is actually more true in my opinion. – BenjaminPaul Jul 07 '15 at 15:53
  • @BenjaminPaul I do agree that controllers should not contain business logic, and that the logic more appropriately belongs in a layer consumed by controllers. However "models" in the context of a web-oriented MVC pattern like ASP.NET MVC should really also be "ViewModels", meant for passing data from a controller to a view. So really, neither the M, V, nor C layers should contain any business logic, only presentational data and logic. – danludwig Jul 07 '15 at 15:58
  • Thanks for clearing that up @danludwig ... I was merely pointing out that your statement can be misleading. – BenjaminPaul Jul 07 '15 at 15:59
  • @CodeBeginner that article is bad and misleading. I would advise you take any Microsoft opinionated articles with a grain of salt. They are geared to get people up and running quickly without good, solid software design principles. – danludwig Jul 07 '15 at 16:02
  • @danlufwig - Ok thanks ." logic more appropriately belongs in a layer consumed by controllers."-"neither the M, V, nor C layers should contain any business logic, only presentational data and logic." what would that be ? Could you suggest any good reference to understand it? – CodeBeginner Jul 07 '15 at 16:27
  • @CodeBeginner the best answer for your stated level of current knowledge would be that MVC style patterns can be applied to different parts of a web application. There are MVC (or MVVM, or MVW) patterns for the javascript code that runs in the browser (like AngularJS). There are also MVC patterns for the code that runs on the server (like .NET MVC and WebAPI). My comments about business logic are based on opinions that server-side MVC code should be geared toward delivery of presentation-related aspects of an application. But if you must put logic in one of these layers, put it in controllers. – danludwig Jul 14 '15 at 15:18
1

From my perspective, you're confusing two very different technologies; One is server-side (ASP.NET MVC) an one is Client Side (AngularJS).

The two can certainly be used hand-in-hand as neither are dependent on the other existing. And, IIRC, there is a scaffolding module on NuGet that helps bindings from one to the other (Create JS objects from your ASP.NET POCO objects).

With that being said, there is no reason you need to use both. You can one one or ther other, both, or bring in a different technology altogether (KnockoutJS, etc.)?

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
1

Like as other design patterns (Singleton, Factory, Factory method) MVC is also a design pattern that we can use in any technology like(Java, .net, PHP) or to any client site scripting. there is no difference between MVC in Angular JS and MVC in ASP.NET and Spring MVc

Mohit Dagar
  • 522
  • 6
  • 21