简体   繁体   中英

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. 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. 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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM