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

时间:2019-05-30 09:03:11

标签: c# asp.net-core model-view-controller model asp.net-core-mvc

我有一个返回视图的方法,在该视图中我想显示一些东西的列表。为此,我需要一个包含列表的模型。

Microsoft的文档(https://docs.microsoft.com/en-us/aspnet/core/mvc/overview?view=aspnetcore-2.2)表示,选择并使用模型是控制器的责任,并且模型的责任是封装业务逻辑。话虽如此,但我不确定最佳做法:

控制器中的逻辑:

型号:

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

控制器:

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);
    }
}

模型中的逻辑

型号:

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; }
}

控制器:

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);
    }
}

任何形式的指导将不胜感激。

1 个答案:

答案 0 :(得分:1)

我建议您采用第一种方法

控制器中的逻辑:

型号:

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

控制器:

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);
    }
}

最佳实践是使用DI来初始化构造函数中的服务或依赖项值。如果您选择第二种方法,则必须像这样

将数据发送到构造函数
DepartmentViewModel viewmodel = new DepartmentViewModel(_work, _mapper);

如果您有很多模型,那不是理想的选择吗?