iT邦幫忙

2022 iThome 鐵人賽

DAY 17
0
Software Development

.NET Core與React組合開發技系列 第 17

.NET Core與React組合開發技_第17天_重構_從controller抽離業務邏輯

  • 分享至 

  • xImage
  •  

這邊可將DTO Model類別都移動至新增的Models目錄(自行額外新建)
用於將程式分門別類
https://ithelp.ithome.com.tw/upload/images/20220928/20107452TPOn31iCkQ.png
~\Models\ProductModel.cs

namespace MyReact1.Models
{
    public class ProductModel
    {
        public List<ProductListItem> Products { get; set; } = new List<ProductListItem>();

        public class ProductListItem
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
        }

    }
}

~\Models\ProductDetailsModel.cs

namespace MyReact1.Models
{
    public class ProductDetailsModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
    }
}

抽離Model DTO後的
~\Controllers\ProductController.cs

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyReact1.Models;

namespace MyReact1.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        public IActionResult List()
        {
            var model = new ProductModel
            {
                Products = new List<ProductModel.ProductListItem> {
                    new ProductModel.ProductListItem{Id = 1, Name = "The first", Description="product 1"}
                }
            };
            return Ok(model);

        }
        //  -> /api/Product/10
        [HttpGet("{id}")]
        public IActionResult Details(int id)
        {
            return Ok(new ProductDetailsModel
            {
                Id = id,
                Name = "A product",
                Description = "Description",
                Price = 1000
            });

        }

    }
}

可再另外擴增一個Services目錄
把業務邏輯額外封裝成一隻獨立職責的Service類別
https://ithelp.ithome.com.tw/upload/images/20220928/20107452Cq7IXRmhCF.png

~\Services\ProductService.cs

using MyReact1.Models;

namespace MyReact1.Services
{
    public class ProductService
    {
        public static ProductModel GetProductList()
        {
            return new ProductModel
            {
                Products = new List<ProductModel.ProductListItem> {
                    new ProductModel.ProductListItem{Id = 1, Name = "The first", Description="product 1"}
                }
            };
        }


        public static ProductDetailsModel GetDetails(int id)
        {

            return new ProductDetailsModel
            {
                Id = id,
                Name = "A product",
                Description = "Description",
                Price = 1000
            };
        }
    }
}

最終再去重構ProductController.cs
把業務邏輯全部改為用Service做方法存取

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyReact1.Models;
using MyReact1.Services;

namespace MyReact1.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        public IActionResult List()
        {
            ProductModel model = ProductService.GetProductList();
            return Ok(model);

        }
        //  -> /api/Product/10
        [HttpGet("{id}")]
        public IActionResult Details(int id)
        {
            return Ok(ProductService.GetDetails(id));

        }

    }
}


上一篇
.NET Core與React組合開發技_第16天_React整併axios呼叫api獲取產品詳細頁
下一篇
.NET Core與React組合開發技_第18天_EF Core模型觀念及配置
系列文
.NET Core與React組合開發技30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言