[英]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.