0

I am exploring ASP.Net web Api and I am trying to use to create a web page to basically provide option to edit and display some data from DB(MySQL) with some modifications. I also want to provide a REST Api that would be used by mobile app. Now I can do this with the ApiController(to provide REST api) and then the MVC Controller which renders the model(using View method). This works but there is lot of duplication between the API and the MVC controller. Is there a design pattern or something that avoids the redundancy?

I understand that API controller has to return JSON and the MVC controller has to return the Model for the view.

1 Answer 1

1

There are two general paths you can follow to remove duplication.

  1. Use js frameworks and rely on the rest API for all data. Knockout, breeze and countless other frameworks exist to support this, because it is how SPAs are made. You can get lost in all the frameworks, so when you find the ones you like, learn them and run with them. I'm using this structure on my latest web app with knockout and sammy (for routing)

  2. Fat model, skinny controller. How you actually handle this depends on the complexity of your data. MVC can have multiple layers. You could use the API's view as the model in the MVC's controller and view. You could present a DAL that handles queries and reference that from both controllers. Either way duplication is removed.

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

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.