3

Are there any tutorials/examples on how to create an asp.net mvc app without the model being managed by a database (through linq2sql or entity framework). I've to create a frontend for a server which has a json based api. I would like to use mvc 3 or 2 and have most of the features of mvc still in place (like data annotation and validation). Any tutorials or examples on how to do this? I tried to search them but all examples i find are based on entity framework or linq.

tereško
  • 58,060
  • 25
  • 98
  • 150
Toad
  • 15,593
  • 16
  • 82
  • 128

2 Answers2

2

I agree that most of the examples/tutorials out there are using entity framework. This being said the process would be similar:

  1. Create your model classes.
  2. Create a repository working with those model classes. This repository should implement an interface which contains all the operations you need with those models like GetUser, SaveUser, etc... In the implementation you connect to the remote JSON API server to fetch data.
  3. You create a controller which takes the repository interface in the constructor. Setup a custom controller factory so that a DI framework could provide instances of your controllers.
  4. Define views and view model classes.
  5. Controller actions talk to the repository via the provided interface to fetch models, maps those models to view models and returns them to the corresponding view to be shown.

Useful tools:

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I can follow you except for step 3. Would your suggested method also support validation to work (via data annotations on the model?) – Toad Nov 03 '10 at 14:43
  • @Toad, I prefer FluentValidation.NET for validation. I don't use DataAnnotations. – Darin Dimitrov Nov 03 '10 at 15:07
  • Does this also support client validation? – Toad Nov 03 '10 at 15:22
  • Yes, currently [it has limited support](http://www.jeremyskinner.co.uk/2010/02/06/fluentvalidation-1-2-beta-2-and-mvc2-rc2/) for simple rules like `NotNull` and `Length`. Custom validation functions are not supported. – Darin Dimitrov Nov 03 '10 at 15:46
0

MVC 3 has extra support for JSON which you might want to look into.

Or use the futures with MVC 2.

Todd Smith
  • 17,084
  • 11
  • 59
  • 78
  • 1
    if i read the blogpost correctly, it handles the case where json is posted from the frontend to the webserver. However i'm interested in the part after this... If the webserver wants to store this data using some 3rd part api instead of entityfw or via linq2sql. How does one go about it – Toad Nov 02 '10 at 19:17