MVC and Android as front-ends WebApi as backend - asp.net-mvc

I am going to build an application with two different front-ends.
the first one is a website which I intend to build it with MVC5 and the second one is with Android platform.
Question:
what is the best practice to accomplish this goal?
should I make the WebApi in charge of working with the lower layers of my application and make call to it via Android and HttpClient (for MVC) or just make my website with MVC and find the specific needed APIs to be called by Android platform afterwards?
I feel something wrong with having a facade (MVC controllers) on top of another facade(WebAPI controllers) meanwhile I can not find any better architecture for these kind of software.

If to start with, you keep the classes with the functionality seperate from your MVC or WebAPI controller code, you can share this between the 2. It can be cleaner to put your Application / Business logic in a separate csproj all together, then reference from your MVC and WebAPI. This will also make it easier to test that code.
This then gives you the option of developing your app MVC first, test with users and get feedback (without spending time on APIs etc yet), and then build your API afterwards for android (using the same application code). You could also end up with both running on top of the API, which arguably would be a better long term place to be for maintenance and security. This evolutionary approach will tend to help you avoid over engineering your software.

Related

Adding WebAPI to an existing MVC5 application

I have an MVC5 application, which works normally. It has models, controllers, views, and all the other functionality. It is already a stand-alone app. I now want to create an API for this application. I don't know whether I should create a new WebAPI project in the same solution, or use my existing MVC project, to also include the API functionality there. Which is the appropriate way? Please note that later I want to use that API for creating a mobile application using Xamarin.
There is nothing wrong with using the same project, in many ways it makes the most sense as it will be easier when hosting the application if they live within the same web project.
I tend to keep my API controllers in a subfolder called "Api" which is neatly located within the Controllers folder of the web solution.
"Controllers > Api".
When i'm not sure with these kinds of things I tend to go with the simplest option, later on down the line if you find a good reason to locate the controllers in their own project for some reason this is easily achieved.
You mentioned that you will be using the API to develop an application using XAMARIN later on and this should not mean moving the API... mobile apps will not need any kind of reference to the API at all, they will simply be calling your application using standard HTTP requests. It will make no difference and again as you are going to need a host for your website, you might as well use the same host for your API too.
If you want use WebAPI not only for WebSite - SO sure, you have add new WebAPI project in solution
(+ it's better in OOP) - it be easy to change.

Mixing Angular and ASP.NET MVC/Web api?

I come from using ASP.NET MVC/Web API and now I am starting to use Angular but I am not clear on the proper way to mix them.
Once I am using Angular does the MVC sever side concepts still provide any value ? Or should I strictly be using Web API purely to get data for the angular HTTP calls ?
Any tips you have for a ASP.NET MVC guy transitioning to Angular would be helpful
Pure Web API
I used to be pretty hardcore with ASP.NET MVC but since I've met Angular I do not see one reason why I would use any server side content generation framework. Pure Angular/REST(WebApi) gives a richer and smoother result. It's much faster and allows you to build websites that come quite close to desktop applications, without any funky hacks.
Angular does have a little learning curve, but once your team has mastered it, you'll build much better websites in less time. Mainly this has to do with the fact that you don't have all these state(less) issues anymore.
For example imagine a wizard form with any traditional server side framework. Each page needs to be validated and submitted separately. Maybe the content of the page is dependent on values from a previous page. Maybe the user pressed the back button and is re-submitting an previous form. Where do we store the state of the client? All these complications do not exist when using Angular and REST.
So ... come over to the dark side ... we've got cookies.
Similar question
AngularJS is more associated with the single page application paradigm, and as such, doesn't benefit much from server-side technologies that render markup. There is no technical reason that precludes you using them together, but in a practical sense, why would you?
An SPA retrieves the assets it needs (JS, CSS, and HTML views) and runs on its own, communicating back to services to send or retrieve data. So, a server-side technology is still necessary for providing those services (as well as other means such as authentication and the likes), but the rendering parts are largely irrelevant and not particularly useful because it's a duplication of efforts, except MVC does it on the server side and Angular does it on the client. If you're using Angular, you want it on the client for best results. You can make Angular post HTML forms and retrieve partial views from MVC actions, but you'd be missing out on the best and easiest features of Angular and making your life harder.
MVC is pretty flexible and you could use it to service calls from an SPA application. However, WebAPI is more finely tuned and a bit easier to use for such services.
I've written a number of AngularJS applications, including a couple that migrated from pre-existing WebForms and MVC applications, and the ASP.NET aspect evolves towards a platform for delivering the AngularJS app as the actual client, and for hosting the application layer the client communicates to via REST (using WebAPI). MVC is a fine framework, but it usually finds itself without a job in these sorts of applications.
The ASP.NET application becomes another layer to the infrastructure, where its responsibilities are limited to:
Host the dependency container.
Wire the business logic implementations into the container.
Set up asset bundles for JS and CSS.
Host WebAPI services.
Enforce security, perform logging and diagnostics.
Interfacing with application caches for performance.
Another great thing about an SPA is it can increase bandwidth of your team. One group can blast out the services while the other lays in the client app. Since you can easily stub or mock REST services, you could have a fully working client app on mock services and swap out for the real ones when they're done.
You do have to invest up front on Angular, but it pays off big. Since you are already familiar with MVC, you have a leg-up on some of the core concepts.
It depends on the project you are working on.
If angularJS is something new for you I would rather pick a small low risk/pressure project to get started and ensure you learn how to do things in the right way (I have seen many projects using Angularjs wrong because of pressure, deadlines... lack of time to learn it in a proper way, e.g. using JQuery or accesing the DOM inside the controllers, etc...).
If the project is a green field one, and you have got some experience on AngularJS, it makes sense to abandon ASP.net MVC and in the server side go for pure REST/WebAPI.
If it's an existing project, you can pick up a complex subset of functionality and build that page as a separate angularJS app (e.g. your app is composed of a big bunch of standard simple / medium complexity Razor based pages but you need and advanced editor / page, that could be the target piece to build with AngularJS).
You can use Angular framework for front end development i.e to construct views. It provides you a robust architecture and once you learn you will find it's advantages over Asp.net MVC's razor view engine. To fetch data you have to use WebAPIs and now ASP.Net MVC project support both WebAPI and MVC controllers out of the box. You can refer below link start with Angular and ASP.Net MVC application development.
http://hive.rinoy.in/angular4-and-asp-net-mvc-hybrid-application/
There are two frameworks currently available for developing UI components for angular applications. I have used both these frameworks in one of the angular projects that I worked.
Material
https://material.angular.io/
PrimeNG
https://www.primefaces.org/primeng/#/

Best practice for exposing data using Web API along with building MVC4 website

I am having one client who wanted to have a website along with an app on iPhone and Android too. So, I am worried about how to structure my MVC solution architecture to best suit the requirement and re-use the business logic among all the platform using Web API.
Can any one suggest the architecture in bit detail to prefer in the above mentioned requirement.

Same Project Solution or New Project in Same Solution - Asp.net MVC / Web Api?

I am wondering what is the better way to go. I created a webapi project and am currently working on making my api.
In the future I want a full asp.net mvc 4 website and that could also contain forms to insert data into my database.
I am not sure if I should
a)
Make a new area in my web api project and build my website from there.
b)
Keep it in the same area and just make some new controllers and such in the web api project.
c) add a new asp.net mvc 4 project to my web api solution project.
Definitely two projects. In fact, I'd actually recommend three projects:
MVC website
Class library, for sharing your DAL/Service layers
Web API
Your MVC site shouldn't need to query your Web API, that's just going to create HTTP latency that's unnecessary. Both your MVC site and your Web API, are just "frontends" for your class library. They will both reference the class library and interact with the class library.
A Web API is only necessary if you're trying to provide third-party access or you're interfacing with a project in another language. If everything is .NET then just share the DLLs and call it a day.
K. Scott Allen ā€¸recently wrote a brilliant post on the Coexistence of ASP.NET MVC and WebAPI it covers the most common scenarios and when it's appropriate to use WebAPI with MVC or when you should just use MVC.
I would use that as your guide pick the solution that best meets your current needs. My advice is to keep it simple and if your requirements are simple then there is no reason not keep WebAPI and MVC in the same project - it works just fine. As your requirements change you can always split them up into different projects or solutions, but by then you will know exactly why you are doing so.
http://odetocode.com/blogs/scott/archive/2013/07/01/on-the-coexistence-of-asp-net-mvc-and-webapi.aspx
absolutely,
go through link http://efmvc.codeplex.com/
which is the best architecture to develop the big apps
may this one is help you...
another BEST one MVC N-Tier architecture
MVC ---------> WEB API (services)------ > here BL | DL(ORM) | DB)
which you create this in same solution and build the app...
Separate projects for the web api and the web interface will help split things up, but it does cause duplications. We went that way recently and it works well, but it caused a few problems.
Arguments for having a single project :
Since we don't have a domain name yet, we have our API on the 8080 port. We could use a directory binding to make the API accessible from a sub-directory of the web interface but we were worried about production only bugs about absolute path resolution.
Many settings are shared between the two projects so we have to copy them in both web.config files.
Arguments for having multiple projects :
They are easier to upgrade since they can have different dependencies and they can be built totally independently. For example, our API project uses a few more recent versions of some dependencies.
It forces you to extract all of your business logic into a separate library and makes it easier to think about both projects as separate sub-systems.
It is easier to setup the web interface to a separate machine if the load is too much. This is a concern for us, but that may not be your case.
If I had to make this decision again, I probably wouldn't bother with separate projects unless the system was extremely complex and I needed the additional structure. An argument can be made for both options, but I think the deployment headache it brings is not worth it.

Does Silverlight 3 Change the MVC vs. Silverlight question?

I'm starting a new project soon and investigating whether or not to use MVC, Silverlight or both. This question Silverlight vs. ASP.NET MVC was a good starting point. But looking over the SL3 beta, it looks like there are two things that change the landscape just a bit. First, the navigation feature which is a direct feature of Silverlight and now there is the .NET RIA Services. While not actually part of Silverlight, it's fairly integrated and seems to bridge the gap between client and server operations. An area, in my opinion where MVC is strong and Silverlight (being a client side technology) was not. I understand that you can still use both, but should you and why?
Simply put, no.
Silverlight and silverlight on the desktop are more like winforms/wpf applications that just happen to run on the web. They're stateful and act very differently from a web site. Silverlight apps can only be run on machines that allow silverlight to run. In a business network setup they may have group policies setup so that silverlight cannot be installed. Then you've got a problem.
ASP.NET MVC however is a stateless type of design where pretty much anyone with a web browser can get to regardless of platform. You have much more freedom with your site using ASP.NET MVC because you don't have a dependency on the end-user having any sort of runtime installed.
I think each has their place and it's pretty much obvious. Silverlight is great for creating a rich experience where applicable. I doubt you'll see too much silverlight popping up replacing web sites.
When we built .NET RIA services we looked to enable developers to basically build out rapid RIA's 100% and as a result we targeted the approach in terms of taken over the burden of ASP.NET WebForms etc. To use the two in a hybrid fashion is possible, but it's a question of which piece do you want to take over the heavy lifting.
If you're Silverlight solution is basic in terms of implementation - ie datagrid of data only - then ASP.NET MVC is a better fit as this way you're not having to worry about deep linking etc (as that comes free inside a native HTML based approach).
If however you're building out a larger RIA solution within Silverlight and are using ASP.NET in ways that are specific, then ok thats fine, but you're going to have to remember that with every page refresh comes a penalty that is of course a page refresh and secondly you need to ensure the said Silverlight instance can find its way back to where it was before you hit the refresh (that or loads in a place that enables the user to find its way forward).
It can be somewhat very tricky despite the goodness we baked into RIA Services.
Scott Barnes / Rich Platforms Product Manager / Microsoft.
I think that both technologies have their places - I think Silverlight will be used mainly for special purpose applications and asp.net MVC will be mainly for public and general purpose style webpages and web applications.
As to which one you want to use, well that depends entirely on your needs and your audience.
Silverlight is coming along nicely even for business applications (Navigation, RIA Services, Data Form etc...) But ya the big problem is the plugin.
Personally, I'm sick and tired of HTML so i'm focusing efforts on Silverlight.
It also depends if you're working on a site for fun or not.
A lot of websites built for businesses and organizations have a "read only" public facing front end. That is, information (inventory, catalogs, corporate news) are presented from a rich database, but the average end user has no ability to edit them.
The natural pattern I have been seeing is to use ASP.NET MVC for the "front end" presentation but to use the Silverlight / RIA functionality to avoid having to write all the CRUD stuff int he administration back end. It is a great fit. The administration userbase is usually much smaller than the total audience and issues of download time, Silverlight penetration and so on are not nearly as problematic.
However the Silverlight RIA / ASP.NET MVC story is a very fuzzy one right now. There isn't much information discussing how to use RIA Services to provide access to a shared repository project of the style you build for MVC projects. link text This makes integrating them difficult right now. You could build both your MVC and RIA layers on top of a common EF or Linq2Sql base but it makes sense to share the intelligence of the Repository classed you build for MVC.
Ideally, we will get good documentation of connecting RIA Services to a Repository style class layer, independent of what the Repository uses to talk to the database underneath and then the rapid development story will be complete.
I dont think silverlight changes the asp.net MVC story.
MVC lets asp.net be more web standard friendly, and forces projects into a more testable, decoupled structure. The later was the sole responsbility of the architect/developer in webforms.
"Personally, I'm sick and tired of HTML so i'm focusing efforts on Silverlight."
Amen to that. javascript too.
I've been developing web apps for over a decade (Classic asp then jumped to .Net as soon as it was in beta). MCTS x2 and MCPD x2 in asp.net.
While i think ASP.Net MVC is great additon for web site developers, i do think web forms with a strict MVP pattern in UI layer is often still better for enterprise / B2b web applications. (dashboard, administration, reporting etc.)
That all said as i move into more into the integration space, SOA, biztalk, wcf etc.
I really am over the giving a sh1t weather this css property is applied validly in "IE version what ever". or "firefox version whats next" and over dealing with the sometimes great sometime want to shoot myself control hierachy in web forms.
Silverlight 3 is really starting to become relevant for LOB applications, i see it as the major compeditor to webforms, not MVC.

Resources