SkyoceanHome
Home
Privacy
ダウンロード
SkyShop
Updated Information
チュートリアルコード集
References
記事編集
日付
ジャンルのカテゴリ―名
名前を選択して下さい。
Contexts
Controllers
Databases
Htmls
JavaScripts
Models
MVCTutorials
Others
Styles
Views
タイトル
内容の要旨
<p data-bm="57" style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 4px; padding: revert; -webkit-line-clamp: revert; color: rgba(0, 0, 0, 0.8); font-family: roboto, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" data-pasted="true">今回は <strong style="font-weight: 700;">ASP.NET Core MVC の Controller 内で実際に使える形</strong> にし、<br><code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">Payment</code> の生成処理をサービスメソッドに分離して再利用可能にします。</p><pre data-priority="2" data-bm="58" style="background: none 0% 0% / auto repeat scroll padding-box border-box rgb(249, 249, 249); border: 1px solid rgba(0, 0, 0, 0.1); border-radius: 8px; overflow: hidden; color: rgba(0, 0, 0, 0.8); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div data-bm="62" style="display: flex; height: 28px; flex-direction: row; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid rgba(0, 0, 0, 0.1); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(243, 249, 254);"><h5 style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 0px; padding: revert; font-style: normal !important; font-variant: normal !important; font-weight: 400 !important; font-stretch: 100% !important; line-height: 18px; font-family: roboto, helvetica, sans-serif !important; font-optical-sizing: auto !important; font-size-adjust: none !important; font-kerning: auto !important; font-feature-settings: normal !important; font-variation-settings: normal !important; font-language-override: normal !important; font-size: 16px; display: inline-block; color: rgb(11, 73, 127);">Csharp</h5><div style="height: 28px;"><button type="button" tabindex="" title="Copy code" style="box-sizing: border-box; margin: 0px; padding: 0px; font: 400 13px / 20px roboto, helvetica, sans-serif; background-color: rgb(225, 240, 253); border: 1px solid rgba(0, 0, 0, 0); display: grid; color: rgb(0, 0, 0) !important; grid-auto-flow: column; align-items: center; cursor: pointer; text-decoration: none !important; border-radius: 9999px; block-size: 28px; gap: 2px; padding-inline: 10px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px; min-inline-size: 0px; max-inline-size: 100%; white-space: nowrap; text-overflow: ellipsis; overflow: clip; max-block-size: calc(1lh); padding-inline: 2px;">Copy code</div></button></div></div><code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255); display: block; white-space: pre-wrap; overflow-wrap: anywhere; padding: 8px 16px 16px;"><span style="color: rgb(215, 58, 73);">using</span> System; <span style="color: rgb(215, 58, 73);">using</span> System.Collections.Generic; <span style="color: rgb(215, 58, 73);">using</span> Microsoft.AspNetCore.Mvc; <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(215, 58, 73);">class</span> <span style="color: rgb(111, 66, 193);">Payment</span> { <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">int</span> PaymentId { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(106, 115, 125);">// DB側で自動生成</span> <span style="color: rgb(215, 58, 73);">public</span> DateTime Posted { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">bool</span> InOut { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">int</span>? SubjectNameId { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">string</span> ItemName { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">string</span> PaymentName { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">int</span>? PaymentTypeId { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">int</span>? PaymentMethodId { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">int</span>? IncomeCategoryId { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">int</span>? IncomeTypeId { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">decimal</span> Amount { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(215, 58, 73);">interface</span> <span style="color: rgb(111, 66, 193);">IPaymentService</span> { List<Payment> <span style="color: rgb(111, 66, 193);">GeneratePayments</span>(); } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(215, 58, 73);">class</span> <span style="color: rgb(111, 66, 193);">PaymentService</span> : <span style="color: rgb(111, 66, 193);">IPaymentService</span> { <span style="color: rgb(215, 58, 73);">private</span> <span style="color: rgb(215, 58, 73);">static</span> <span style="color: rgb(227, 98, 9);">int</span>? GetId(Dictionary<<span style="color: rgb(227, 98, 9);">string</span>, <span style="color: rgb(227, 98, 9);">int</span>> dict, <span style="color: rgb(227, 98, 9);">string</span> key) { <span style="color: rgb(215, 58, 73);">return</span> dict.TryGetValue(key, <span style="color: rgb(215, 58, 73);">out</span> <span style="color: rgb(215, 58, 73);">var</span> <span style="color: rgb(215, 58, 73);">value</span>) && <span style="color: rgb(215, 58, 73);">value</span> != <span style="color: rgb(0, 92, 197);">0</span> ? <span style="color: rgb(215, 58, 73);">value</span> : (<span style="color: rgb(227, 98, 9);">int</span>?)<span style="color: rgb(0, 92, 197);">null</span>; } <span style="color: rgb(215, 58, 73);">public</span> List<Payment> <span style="color: rgb(111, 66, 193);">GeneratePayments</span>() { <span style="color: rgb(227, 98, 9);">int</span> thisYear = DateTime.Now.Year; <span style="color: rgb(227, 98, 9);">int</span> previousMonth = DateTime.Now.Month - <span style="color: rgb(0, 92, 197);">1</span>; <span style="color: rgb(215, 58, 73);">if</span> (previousMonth == <span style="color: rgb(0, 92, 197);">0</span>) { previousMonth = <span style="color: rgb(0, 92, 197);">12</span>; thisYear--; } <span style="color: rgb(106, 115, 125);">// 各Id辞書</span> <span style="color: rgb(215, 58, 73);">var</span> subjectNameDict = <span style="color: rgb(215, 58, 73);">new</span> Dictionary<<span style="color: rgb(227, 98, 9);">string</span>, <span style="color: rgb(227, 98, 9);">int</span>> { { <span style="color: rgb(3, 47, 98);">"家計"</span>, <span style="color: rgb(0, 92, 197);">1</span> }, { <span style="color: rgb(3, 47, 98);">"事業"</span>, <span style="color: rgb(0, 92, 197);">2</span> } }; <span style="color: rgb(215, 58, 73);">var</span> paymentTypeDict = <span style="color: rgb(215, 58, 73);">new</span> Dictionary<<span style="color: rgb(227, 98, 9);">string</span>, <span style="color: rgb(227, 98, 9);">int</span>> { { <span style="color: rgb(3, 47, 98);">"予算"</span>, <span style="color: rgb(0, 92, 197);">10</span> }, { <span style="color: rgb(3, 47, 98);">"支出"</span>, <span style="color: rgb(0, 92, 197);">20</span> } }; <span style="color: rgb(215, 58, 73);">var</span> paymentMethodDict = <span style="color: rgb(215, 58, 73);">new</span> Dictionary<<span style="color: rgb(227, 98, 9);">string</span>, <span style="color: rgb(227, 98, 9);">int</span>> { { <span style="color: rgb(3, 47, 98);">"現金"</span>, <span style="color: rgb(0, 92, 197);">100</span> }, { <span style="color: rgb(3, 47, 98);">"銀行振込"</span>, <span style="color: rgb(0, 92, 197);">200</span> } }; <span style="color: rgb(215, 58, 73);">var</span> incomeCategoryDict = <span style="color: rgb(215, 58, 73);">new</span> Dictionary<<span style="color: rgb(227, 98, 9);">string</span>, <span style="color: rgb(227, 98, 9);">int</span>> { { <span style="color: rgb(3, 47, 98);">"給与"</span>, <span style="color: rgb(0, 92, 197);">300</span> }, { <span style="color: rgb(3, 47, 98);">"副業"</span>, <span style="color: rgb(0, 92, 197);">400</span> } }; <span style="color: rgb(215, 58, 73);">var</span> incomeTypeDict = <span style="color: rgb(215, 58, 73);">new</span> Dictionary<<span style="color: rgb(227, 98, 9);">string</span>, <span style="color: rgb(227, 98, 9);">int</span>> { { <span style="color: rgb(3, 47, 98);">"定期"</span>, <span style="color: rgb(0, 92, 197);">500</span> }, { <span style="color: rgb(3, 47, 98);">"臨時"</span>, <span style="color: rgb(0, 92, 197);">600</span> } }; <span style="color: rgb(106, 115, 125);">// 複数のPaymentデータ定義</span> <span style="color: rgb(215, 58, 73);">var</span> paymentData = <span style="color: rgb(215, 58, 73);">new</span>[] { <span style="color: rgb(215, 58, 73);">new</span> { SubjectKey = <span style="color: rgb(3, 47, 98);">"家計"</span>, TypeKey = <span style="color: rgb(3, 47, 98);">"予算"</span>, MethodKey = <span style="color: rgb(3, 47, 98);">"現金"</span>, CategoryKey = <span style="color: rgb(3, 47, 98);">"給与"</span>, IncomeTypeKey = <span style="color: rgb(3, 47, 98);">"定期"</span>, Amount = <span style="color: rgb(0, 92, 197);">268000</span>m, ItemName = <span style="color: rgb(3, 47, 98);">"今月予算"</span> }, <span style="color: rgb(215, 58, 73);">new</span> { SubjectKey = <span style="color: rgb(3, 47, 98);">"事業"</span>, TypeKey = <span style="color: rgb(3, 47, 98);">"支出"</span>, MethodKey = <span style="color: rgb(3, 47, 98);">"銀行振込"</span>, CategoryKey = <span style="color: rgb(3, 47, 98);">"副業"</span>, IncomeTypeKey = <span style="color: rgb(3, 47, 98);">"臨時"</span>, Amount = <span style="color: rgb(0, 92, 197);">150000</span>m, ItemName = <span style="color: rgb(3, 47, 98);">"事業支出"</span> } }; <span style="color: rgb(215, 58, 73);">var</span> payments = <span style="color: rgb(215, 58, 73);">new</span> List<Payment>(); <span style="color: rgb(215, 58, 73);">foreach</span> (<span style="color: rgb(215, 58, 73);">var</span> data <span style="color: rgb(215, 58, 73);">in</span> paymentData) { payments.Add(<span style="color: rgb(215, 58, 73);">new</span> Payment { Posted = <span style="color: rgb(215, 58, 73);">new</span> DateTime(thisYear, previousMonth, <span style="color: rgb(0, 92, 197);">1</span>), InOut = <span style="color: rgb(0, 92, 197);">true</span>, SubjectNameId = GetId(subjectNameDict, data.SubjectKey), ItemName = data.ItemName, PaymentName = <span style="color: rgb(0, 92, 197);">null</span>, PaymentTypeId = GetId(paymentTypeDict, data.TypeKey), PaymentMethodId = GetId(paymentMethodDict, data.MethodKey), IncomeCategoryId = GetId(incomeCategoryDict, data.CategoryKey), IncomeTypeId = GetId(incomeTypeDict, data.IncomeTypeKey), Amount = data.Amount }); } <span style="color: rgb(215, 58, 73);">return</span> payments; } } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(215, 58, 73);">class</span> <span style="color: rgb(111, 66, 193);">PaymentsController</span> : <span style="color: rgb(111, 66, 193);">Controller</span> { <span style="color: rgb(215, 58, 73);">private</span> <span style="color: rgb(215, 58, 73);">readonly</span> IPaymentService _paymentService; <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(111, 66, 193);">PaymentsController</span>(IPaymentService paymentService) { _paymentService = paymentService; } <span style="color: rgb(215, 58, 73);">public</span> IActionResult <span style="color: rgb(111, 66, 193);">Index</span>() { <span style="color: rgb(106, 115, 125);">// サービスからPaymentリストを取得</span> <span style="color: rgb(215, 58, 73);">var</span> payments = _paymentService.GeneratePayments(); <span style="color: rgb(106, 115, 125);">// Viewに渡す</span> <span style="color: rgb(215, 58, 73);">return</span> View(payments); } } </code></pre><h3 data-bm="59" style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: revert; padding: revert; display: inline-block; font: 400 20px / 22px roboto, helvetica, sans-serif; color: rgba(0, 0, 0, 0.8); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">改良点</h3><ol data-bm="60" style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: revert; padding: revert; color: rgba(0, 0, 0, 0.8); font-family: roboto, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><li style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin-top: revert; margin-right: revert; margin-bottom: 8px; margin-left: revert; padding: revert;"><p style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 4px; padding: revert; -webkit-line-clamp: revert;"><strong style="font-weight: 700;">MVC構造対応</strong><br><code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">PaymentService</code> に生成処理を分離し、<code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">PaymentsController</code> から呼び出す形に変更。</p></li><li style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin-top: revert; margin-right: revert; margin-bottom: 8px; margin-left: revert; padding: revert;"><p style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 4px; padding: revert; -webkit-line-clamp: revert;"><strong style="font-weight: 700;">依存性注入(DI)対応</strong><br><code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">IPaymentService</code> インターフェースを用意し、ASP.NET Core の DI コンテナに登録すればテストや拡張が容易。</p></li><li style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin-top: revert; margin-right: revert; margin-bottom: 8px; margin-left: revert; padding: revert;"><p style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 4px; padding: revert; -webkit-line-clamp: revert;"><strong style="font-weight: 700;">再利用性向上</strong><br><code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">GeneratePayments()</code> メソッドは他のコントローラーやバッチ処理でも利用可能。</p></li><li style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin-top: revert; margin-right: revert; margin-bottom: 8px; margin-left: revert; padding: revert;"><p style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 4px; padding: revert; -webkit-line-clamp: revert;"><strong style="font-weight: 700;">View連携</strong><br>コントローラーの <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">Index</code> アクションで生成した <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">payments</code> をビューに渡す。</p></li></ol><p data-bm="61" style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 4px; padding: revert; -webkit-line-clamp: revert; color: rgba(0, 0, 0, 0.8); font-family: roboto, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">この形なら、実際の ASP.NET Core MVC プロジェクトに組み込んで動かせます。<br>次はこの <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">PaymentService</code> を <strong style="font-weight: 700;">DBから辞書データを取得する形</strong> に変えることもできますが、それも作りますか?<br>そうすれば完全に動的な ID 取得になります。</p><p data-f-id="pbf" style="text-align: center; font-size: 14px; margin-top: 30px; opacity: 0.65; font-family: sans-serif;">Powered by <a href="https://www.froala.com/wysiwyg-editor?pb=1" title="Froala Editor">Froala Editor</a></p>
コピーする
添付画像
画像を変更
Back to List
//検索文:Editor内でHTMLコードをコピーさせる