简体   繁体   English

在 ASP.NET Core MVC 中实例化和初始化模型的最佳实践

[英]Best practice for instantiating and initialising a Model in ASP.NET Core MVC

I have a method that returns a view, and in that view I want to display a list of something.我有一个返回视图的方法,在该视图中我想显示一个列表。 In order to do this, I'll need a model with houses the list.为了做到这一点,我需要一个包含列表的模型。

Microsoft's documentation ( https://docs.microsoft.com/en-us/aspnet/core/mvc/overview?view=aspnetcore-2.2 ) says that it is the responsibility of the controller to select and work with the model(s) and that the responsibility of the model is to encapsulate business logic. Microsoft 的文档 ( https://docs.microsoft.com/en-us/aspnet/core/mvc/overview?view=aspnetcore-2.2 ) 说选择和使用模型是控制器的责任并且模型的职责是封装业务逻辑。 This being said, I'm unsure of the best practice:话虽如此,我不确定最佳实践:

Logic in the controller:控制器中的逻辑:

Model:模型:

public class DepartmentViewModel
{
    public IEnumerable<DepartmentDto> lstDepartments { get; set; }
}

Controller:控制器:

public class DepartmentController : Controller
{
    private readonly IUnitOfWork _work;
    private readonly IMapper _mapper;

    public DepartmentController(IUnitOfWork work, IMapper mapper)
    {
        _work = work;
        _mapper = mapper;
    }

    public async Task<IActionResult> Index(DepartmentViewModel viewmodel)
    {
        var lstAllDepartments = _work.DepartmentRepository.GetAll(); // All departments from the database.
        var lstDepartmentsForViewmodel = _mapper.Map<IEnumerable<Core.Entities.Department>, IEnumerable<DepartmentDto>>(lstAllDepartments); // Map to DTO.
        viewmodel.lstDepartments = lstDepartmentsForViewmodel;
        return View(viewmodel);
    }
}

Logic in the model:模型中的逻辑:

Model:模型:

public class DepartmentViewModel
{
    private readonly IUnitOfWork _work;
    private readonly IMapper _mapper;

    public DepartmentViewModel(IUnitOfWork work, IMapper mapper)
    {
        _work = work;
        _mapper = mapper;
        var lstAllDepartments = _work.DepartmentRepository.GetAll(); // All departments from the database.
        var lstDepartmentsForViewmodel = _mapper.Map<IEnumerable<Core.Entities.Department>, IEnumerable<DepartmentDto>>(lstAllDepartments); // Map to DTO.
        lstDepartments = lstDepartmentsForViewmodel;
    }

    public IEnumerable<DepartmentDto> lstDepartments { get; set; }
}

Controller:控制器:

public class DepartmentController : Controller
{
    private readonly IUnitOfWork _work;
    private readonly IMapper _mapper;

    public DepartmentController(IUnitOfWork work, IMapper mapper)
    {
        _work = work;
        _mapper = mapper;
    }

    public async Task<IActionResult> Index()
    {
        DepartmentViewModel viewmodel = new DepartmentViewModel(_work, _mapper);
        return View(viewmodel);
    }
}

Guidance of any kind would be most appreciated.任何形式的指导将不胜感激。

I would recommend you go for the first approach我建议你采用第一种方法

Logic in the controller:控制器中的逻辑:

Model:模型:

public class DepartmentViewModel
{
    public IEnumerable<DepartmentDto> lstDepartments { get; set; }
}

Controller:控制器:

public class DepartmentController : Controller
{
    private readonly IUnitOfWork _work;
    private readonly IMapper _mapper;

    public DepartmentController(IUnitOfWork work, IMapper mapper)
    {
        _work = work;
        _mapper = mapper;
    }

    public async Task<IActionResult> Index(DepartmentViewModel viewmodel)
    {
        var lstAllDepartments = _work.DepartmentRepository.GetAll(); // All departments from the database.
        var lstDepartmentsForViewmodel = _mapper.Map<IEnumerable<Core.Entities.Department>, IEnumerable<DepartmentDto>>(lstAllDepartments); // Map to DTO.
        viewmodel.lstDepartments = lstDepartmentsForViewmodel;
        return View(viewmodel);
    }
}

Best practise is using DI to init services or dependency value in your constructor.最佳实践是使用 DI 在构造函数中初始化服务或依赖项值。 If you go for second approach you have to send the data to the constructor like this如果您采用第二种方法,则必须像这样将数据发送到构造函数

DepartmentViewModel viewmodel = new DepartmentViewModel(_work, _mapper);

And that is not ideally what if you have a lot of model ?如果您有很多模型,那不是理想的情况怎么办?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC模型验证最佳实践 - ASP.NET MVC Model Validation best practice ASP.NET MVC 3 - 在视图中处理 Model 的最佳实践 - ASP.NET MVC 3 - Best practice for handling Model in View ASP.NET 核心与 HttpClientFactory:最佳实践? - ASP.NET Core with HttpClientFactory : best practice? 将枚举列表传递给外部 JS 文件的最佳实践? ASP.NET 核心MVC - Best practice for passing an enum list to an external JS file? ASP.NET Core MVC 在 EF Core 和 ASP.NET Core 中处理并发的最佳实践? - Best practice to handle concurrency in EF Core and ASP.NET Core? ASP.NET MVC 4,EF5,model 中的独特属性 - 最佳实践? - ASP.NET MVC 4, EF5, Unique property in model - best practice? 在 asp.net core 中使用 MultiFormDataContent 上传文件的最佳实践 - Best practice for Uploading files using MultiFormDataContent in asp.net core 用于CRUD ASP.NET Core的最佳实践设计DTO? - Best practice design DTOs for CRUD ASP.NET Core? ASP.Net Core-访问数据库的最佳实践? - ASP.Net Core - best practice for accessing database? 最佳实践:自定义asp.net core身份授权 - best practice: customize asp.net core identity authorization
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM