如何构建一个新的ASP MVC应用程序? [英] How to structure a new ASP MVC app?
问题描述
我需要开始一个新的mvc项目,并且一如既往,我对ASP身份存在疑问,永远都不知道该把它放在哪里!
I need to start a new mvc project and as always I have issues about asp identity, never know where to put it!
我打算这样组织解决方案:
I plan to organize solution like this:
- ProjectWebUI-具有ASP身份框架的mvc应用程序(由具有身份验证的Internet模板制成)
- ProjectDataAccessLayer-带有使用
dapper
作为数据库访问技术的存储库类 - ProjectWebAPI-Web服务
- ProjectWebUI - mvc app with asp identity framework (made from internet template with authentication)
- ProjectDataAccessLayer - with repository classes that use
dapper
as database access technology - ProjectWebAPI - web service
但是我有点困惑,在开始编码之前,我需要经验丰富的人的建议(因为到目前为止,我所有的项目只是一个可以访问数据的项目):
But I have a little confusion and before start coding I need advice from someone more experienced (as until now all my projects were just one project with data access in it):
- 在WebUI项目中使用标准Entity Framework进行数据访问并使用dapper在单独的程序集中进行其他数据访问的ASP身份是否是个好主意?
- 如果asp身份位于WebUI项目中,我是否会遇到一些问题,无法获得对WebAPI项目的身份验证访问?
推荐答案
这就是我组织近期项目之一的方式:
That's how I organized one of my recent projects:
-
Common
-这是解决方案中的核心项目.它包含所有域实体以及从ASP.NET Identity Framework的IdentityUser
类继承的ApplicationUser
类.通常,在新的ASP.NET MVC项目模板中可以找到此类.我决定将其放入核心库中,因为它代表了更高层和抽象级别可能需要的公共实体.因此,Common
引用Microsoft.AspNet.Identity.Core
和Microsoft.AspNet.Identity.EntityFramework
程序集.
Common
— this is a core project in the solution. It contains all the domain entities along with theApplicationUser
class that inherits fromIdentityUser
class from ASP.NET Identity Framework. Normally this class is found in a new ASP.NET MVC project template; I decided to put it into the core library because it represents a common entity that may be required for higher layers and levels of abstraction. Because of this,Common
referencesMicrosoft.AspNet.Identity.Core
andMicrosoft.AspNet.Identity.EntityFramework
assemblies.
DataAccess
-此项目引用Common
库,其中包含实体Franework DatabaseContext
以及一些存储库.我使用代码优先方法,并且我的DatabaseContext
是从IdentityDbContext<ApplicationUser>
继承的.因此,它为我提供了一个很好的数据库结构,其中包含用于Users
和Roles
的表以及其他ASP.NET Identity内容以及代表Common
项目中的自定义业务实体的表,因此我可以轻松地连接我的自定义实体带有身份对象.
DataAccess
— this project references Common
library and contains Entity Franework DatabaseContext
as well as some repositories. I use Code First approach and my DatabaseContext
is inherited from IdentityDbContext<ApplicationUser>
. So it gives me a nice database structure with tables for Users
and Roles
and other ASP.NET Identity stuff as well as tables that represent my custom business entities from the Common
project, so I can easily connect my custom entities with Identity objects.
WebApi
-这是使用DataAccess
和Common
库的REST
服务.所有的授权和身份验证工作都在这里使用令牌身份验证来完成.
WebApi
— this is a REST
service that uses DataAccess
and Common
libraries. All the authorization and authentication job is done here using token authentication.
Web
—这只是我的REST
服务的Web客户端.
Web
— this is just a web client for my REST
service.
因此,要回答您的问题:如果ASP.NET Identity类和Entity Framework数据库上下文非常小且易于管理,则可以将其保留在一个项目中.否则,最好不要使用默认的项目模板,并为每个主要应用程序模块引入层.
So, to answer your question: you can keep your ASP.NET Identity classes and Entity Framework database context inside a single project if it is really small and easy to manage; otherwise, it would be better to step away from the default project template and introduce layers for each major application module.
这篇关于如何构建一个新的ASP MVC应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!