Base solution for your next web application
Open Closed

How use services and DTO in Controllers / Views #156


User avatar
0
martin hascak created

Hello, thanks for your awsome framework. I have one question about how to use services and DTO in controllers / Views. I have service named ProjectAppService for CRUD operation (select, insert, update, delete). I want to create controllers actions for this services.

Soo sample: ProjectController for Edit

public ActionResult  Edit(int id)
        {
            **UpdateProjectOutput** project = _projectAppService.FindProjectById(id);

            if (project == null)
            {
                return HttpNotFound();
            }
            return View(project);
        }
}


        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Detail(**[b]UpdateProjectInput** project)
        {
            if (ModelState.IsValid)
            {
                _projectAppService.UpdateProject(project);
                return RedirectToAction("Index");
            }

            return View(project);
        }

Because method FindProjectById retrun UpdateProjectOutput type I must use it in razor Viev like this:

@model InfSample.Projects.Dtos.UpdateProjectOutput

But in edit method i need to use UpdateProjectInput ?

What DTO I must use in the view of Product Detail ? Input / output ?

I´m little confused with this approach, how can I do this think correctly, thanks for help !


1 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    For edit modes, you can use a double way dto, like ProductEditDto. You can get from app service and pass to view. Then you can use same model to update the product. So, you can use single DTO in 2 different method (Say, GetProductForEdit and UpdateProduct). In this case don't add Input/Output postfix and implement IDoublyWayDto. At least, I do it like that :) But, DTOs are flexible, you can create they upon your needs.