ASP.NET MVC-什么是将对象存储在Post/put(架构)上的最佳方法
使用ASP Net MVC创建一个允许创建一个新项目的最佳实践是什么?其所有者是当前记录的用户?
// Entities
class User {
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
class Project {
[Key]
public int Id { get; set; }
public string Name { get; set; }
[Required]
public int OwnerId;
[ForeignKey("OwnerId")]
public User Owner? { get; set; }
}
// DTO / ModeView
class ProjectModelView {
public string Name;
}
class ProjectController : Controller {
private readonly ApplicationDbContext _context;
public ProjectController(ApplicationDbContext context) {
_context = context;
}
public IActionResult Create([Bind("Name")] Project project) {
return View();
}
// 1. Using the model directly
[HttpPost]
public IActionResult Create([Bind("Name")] Project project) {
project.Owner = Session.UserId;
if (ModelState.IsValid) {
_context.Projects.Add(project);
_context.SaveChanges();
return RedirectToAction(actionName: "Index");
}
return View(project);
}
// 2. Using a dto/model view (not sure if this is considerer a model view in this case)
[HttpPost]
public IActionResult Create(ProjectModelView project) {
if (ModelState.IsValid) {
_context.Projects.Add(new Project {
OwnerId = User,
Name = project.name
});
_context.SaveChanges();
return RedirectToAction(actionName: "Index");
}
return View(project);
}
}
我查找了ASP .NET文档,但找不到正确的答案。
哪个是更“像ASP”和正确的选项?有更好的方法吗?另外,在这种情况下,dto aviemodel是吗?
Using asp net mvc what's the best practice for creating an action that allows to create a new project whose owner is the current logged user?
// Entities
class User {
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
class Project {
[Key]
public int Id { get; set; }
public string Name { get; set; }
[Required]
public int OwnerId;
[ForeignKey("OwnerId")]
public User Owner? { get; set; }
}
// DTO / ModeView
class ProjectModelView {
public string Name;
}
class ProjectController : Controller {
private readonly ApplicationDbContext _context;
public ProjectController(ApplicationDbContext context) {
_context = context;
}
public IActionResult Create([Bind("Name")] Project project) {
return View();
}
// 1. Using the model directly
[HttpPost]
public IActionResult Create([Bind("Name")] Project project) {
project.Owner = Session.UserId;
if (ModelState.IsValid) {
_context.Projects.Add(project);
_context.SaveChanges();
return RedirectToAction(actionName: "Index");
}
return View(project);
}
// 2. Using a dto/model view (not sure if this is considerer a model view in this case)
[HttpPost]
public IActionResult Create(ProjectModelView project) {
if (ModelState.IsValid) {
_context.Projects.Add(new Project {
OwnerId = User,
Name = project.name
});
_context.SaveChanges();
return RedirectToAction(actionName: "Index");
}
return View(project);
}
}
I looked up the asp .net documentation and I couldn't find a correct answer.
Which is more "ASP like" and correct option? Is there better ways to do it? Also, is the dto a ViewModel in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用
存储库/服务模式
,带有n-tier Architecture
的togeter。n-tier架构
看起来像projectname.web/server
,具体取决于您是否像MVC Application
一样,则其.web
,如果只是Web API
,则.Server
这是带有
控制器,AutoMapper,Views
等的主要项目。,三个
class Libraries Project
然后
helper
逻辑和业务逻辑
的逻辑类型,也是dtos
或viewmodels
projectName.dataAccess
,数据访问是与database
直接连接的项目,这是我将我的repository文件夹
与一起使用的地方上下文
方法,例如put,get,wet,post
等。另外您将仅用于所有
实体/模型
您将要使用的所有这些连接如何?
项目需要
项目参考。
这就是它的发展
.models
>.dataAccess
>.business
>.web/server
它从底部到顶部,
web/server project
作为顶部,因为这是真实应用程序。
那么,如何将
存储库模式
在n-tier体系结构
中实现?我在
.dataAccess项目中创建
存储库文件夹
。在存储库中创建文件,如果您有模型和
Controller
,则为exempel, nateramployeecontroller.cs
andemployee.cs
然后在
存储库文件夹
中,要保持清洁 - 创建一个sub文件夹,简单地称为雇员
。在员工文件夹
中,创建2个文件。lospereeerepository.cs
和iemployeerepository.cs
在
loasheeerepository
中,您需要引用iemployeeerepository
,因此您的功能和方法将可用于其他文件。然后只需在存储库文件中创建所有上下文逻辑即可。
要在
.web/Server
项目和数据库之间保留另一层,我在.business Project
的内部创建一个文件夹,称为Service
。这是与repository
的原理相同的原理,创建一个名为雇员
的子文件夹,使用employservice.cs
和iemployeeservice.cs
代码>,将所有方法从存储库调用到服务,然后您将服务方法从iemployeeService
调用applyeecontroller
在.web> .web /server
项目,使用依赖项注入
。在那里,您可以从控制器中的DBContext完全隔离。
I use the
Repository/Service pattern
, togeter withN-tier architecture
.N-tier architecture
looks like thisProjectName.Web/Server
, depending if youre making like anmvc application
, then its.Web
, if just aweb api
, then.Server
This is the main project with
controllers, automapper, views
etc.Then three
class libraries projects
ProjectName.Business
, this projects is used to store most of thehelper
logic and diffrent kind ofbusiness logic
for your application, also theDTOs
orViewModels
ProjectName.DataAccess
, data access is the project with the direct connection to thedatabase
, this is the place where I use myrepository folder
with thecontext
method, likeput, get, post
etc. Also theDbContext
ProjectName.Models
, this project is pretty simple, it is just used for all theentities/models
you're going to useSo how is all this connected?
The projects need
project references.
This is how it will go
.Models
>.DataAccess
>.Business
>.Web/Server
It goes from the bottom to the top, with the
Web/Server project
as the top since this is thereal application.
So how do I implement the
repository pattern
into theN-Tier architecture
?I Create a
Repository folder
in the.DataAccess project.
Inside the repository create the files, for exempel if you have a model and
controller
calledEmployeeController.cs
andEmployee.cs
Then inside the
Repository folder
, to keep it clean - create a sub folder, simply calledEmployee
. Inside theEmployee folder
, create 2 files.EmployeeRepository.cs
andIEmployeeRepository.cs
Inside the
EmployeeRepository
you need a reference toIEmployeeRepository
so your functions and methods will be available to other files.And then just create all the context logic in the repository files.
To keep another layer between the
.Web/Server
project and the database, I Create a folder inside of the.Business project
, calledService
. This is the same principle as theRepository
, create a sub folder calledEmployee
, withEmployeeService.cs
andIEmployeeService.cs
, call all of the methods from the repository to the service, and then you call the service methods from theIEmployeeService
to theEmployeeController
inside of the.Web/Server
project, usingdependency injection
.And there you have it, complete isolation from the dbcontext in the controllers.
我可以建议您进行此改进,希望它有所帮助:
示例: https://medium.com/dotnet-hub/use-mediath-mediath-mediatr--------------和介绍者中的dotnet-how-to-mediatr-cqrs-aspnetcore-5076e2f2880c
示例: https://code-maze/automapper-net-net-core/
Check the actor role: https ://www.syncfusion.com/succinctly-free-ebooks/akka-net-succinctly/actors-inactors-in--asp-net-core
但是,我不确定当您进行时,您的问题仍然存在1和2的重构很少
I can suggest you this improvements, hope it helps:
Example: https://medium.com/dotnet-hub/use-mediatr-in-asp-net-or-asp-net-core-cqrs-and-mediator-in-dotnet-how-to-use-mediatr-cqrs-aspnetcore-5076e2f2880c
Example: https://code-maze.com/automapper-net-core/
Check the actor role: https://www.syncfusion.com/succinctly-free-ebooks/akka-net-succinctly/actors-in-asp-net-core
However I'm not sure that your question are still exists when you do a little refactoring from 1 and 2