如何构建一个新的 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 - 网络服务
- 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 项目中使用 asp 身份并在单独的程序集中使用 dapper 进行其他数据访问是否是个好主意?
- 如果 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
库并包含 Entity Franework DatabaseContext
以及一些存储库.我使用代码优先方法,我的 DatabaseContext
是从 IdentityDbContext
继承的.因此,它为我提供了一个很好的数据库结构,其中包含用于 Users
和 Roles
的表和其他 ASP.NET Identity 内容以及表示来自 的自定义业务实体的表Common
项目,因此我可以轻松地将自定义实体与 Identity 对象连接起来.
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 类和实体框架数据库上下文非常小且易于管理,您可以将其保存在单个项目中;否则,最好远离默认的项目模板,并为每个主要应用模块引入层.
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屋!