ASP.NET - 单一的解决方案,MVC和的WebAPI项目,每一个单独的模型? [英] ASP.NET - Single Solution, MVC and WebApi projects, Separate Models for Each?

查看:259
本文介绍了ASP.NET - 单一的解决方案,MVC和的WebAPI项目,每一个单独的模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经得到了多的MVC Web项目的解决方案,现在增加了面向客户的项目的WebAPI。

We've got a solution with multiple MVC web projects and now adding a client-facing WebApi project.

该API将是什么是可通过任何网络项目(虽然它可能会扩大随着时间的推移更多)一个更精简版本,所以我们得出一个决策点,如何处理模型。

The API will be a much more scaled-down version of what is available through any of the web projects (though it will likely expand much more over time), so we've come to a decision-making point for how to handle the Models.

什么是在不同的项目处理模式的最佳实践?

这是我的理解,在一个项目的WebAPI模型会,例如,使用的都是没有意义的一个MVC Web应用程序的某些属性的属性。并且还作为一个例子,显示属性是无意义的的WebAPI,但非常有用的视角。

It's my understanding that a Model in a WebApi project will, for example, use certain property attributes which are meaningless to an MVC web application. And also as an example, the Display attribute is meaningless to the WebApi, but very useful in a View.

这使我相信我应该在的WebAPI要创建一套独立的模型,也想知道如果我失去了一些东西。

This leads me to believe I should be creating a separate set of Models for the WebApi, but also wondering if I'm missing something.

据我了解,这可能是,可能会导致一系列的意见的问题,所以我主要是找什么被认为是行业最佳实践。

I understand that this could be a question that may lead to a range of opinions, so I'm mostly looking for what are considered industry best practices.

推荐答案

在我的解决方案在那里我有网页API和MVC的Web应用程序,我有以下结构

In my solution where i have Web API and MVC web app, I have the below structure

模式:我的实体/业务对象。这些由实体框架从我的数据库中创建的。这是因为我的数据库结构基本相同。我Repositary方法(数据访问)返回类的实例的任一单个实例/集合。我的数据访问的项目是已在其他地方,如我的Web API项目等被提到一个单独的类库。

Models : My Entity/Business objects. These are created by Entity framework from my database. These are almost same as my db structure. My Repositary methods (for data access) returns either a single instance /collection of instances of this classes. My data access project is a separate class library which has been referred in other places like my web api project etc..

的Web API的ViewModels :中的ViewModels(POCO类)具体到Web API接口/操作方法

Web API ViewModels : The Viewmodels (POCO class) specific to the Web API interfaces/action methods.

MVC网页应用的ViewModels :中的ViewModels(POCO类)具体到我的剃须刀的看法。 我连几继承了这些从Web API的ViewModels 并根据需要增加额外的属性。

MVC Web app ViewModels : The Viewmodels (POCO class) specific to my razor views. I had even inherited few of these from the Web API Viewmodels and added additional properties as needed.

这篇关于ASP.NET - 单一的解决方案,MVC和的WebAPI项目,每一个单独的模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆