Asp.net MVC模型没有数据库/框架 [英] Asp.net mvc models without databases/framework
问题描述
是否有任何教程/由一个数据库(通过LINQ2SQL或实体框架)管理的有关如何在未模型创建一个asp.net MVC应用程序的例子。
我已经创建具有基于JSON API服务器的前端。我想用MVC 3或2,有大部分的MVC的特点仍然存在(如数据注释和验证)。
在任何教程或例子如何做到这一点?我试图寻找他们,但我发现所有的例子都是基于实体框架或LINQ。
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.
推荐答案
我同意大多数例子/教程有使用实体框架是。这是说这个过程是相似的:
I agree that most of the examples/tutorials out there are using entity framework. This being said the process would be similar:
- 创建模型类。
- 创建一个存储库与这些模型类的工作。这个仓库应该实现其中包含你需要像
的getUser
,SaveUser
等这些模型的所有操作界面..在执行您连接到远程的JSON API服务器来获取数据。 - 您创建一个控制器更是把库接口的构造。设置自定义控制器工厂,这样一个的DI框架可以提供你的控制器的实例。
- 定义视图和视图模型类。
- 控制器动作通过所提供的接口跟我们的资料库来获取模式,映射这些模型查看模型并将其返回到中显示相应的视图。
- Create your model classes.
- 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. - 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.
- Define views and view model classes.
- 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.
有用的工具:
- MvcContrib (很多有用的工具)
- AutoMapper (用于模型和视图模型之间的映射)
- FluentValidation.NET (用于验证模型)
- MvcContrib (many useful helpers)
- AutoMapper (for mapping between models and view models)
- FluentValidation.NET (for validating models)
这篇关于Asp.net MVC模型没有数据库/框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!