Last active
July 30, 2021 02:27
-
-
Save relyky/5add819326f40852938fea27d72da33f to your computer and use it in GitHub Desktop.
C#, Task, basic data, 非同步取得基本資料。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Dapper; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
/// 專門載入基本資料。如:下拉選單 Select 或 AutoComplete 的項目清單。 | |
/// C#9.0 | |
namespace YourProject.DB.BasicData | |
{ | |
/// <summary> | |
/// 專案代碼 | |
/// </summary> | |
public record ProjectCodeName | |
{ | |
public string ProCode { get; set; } | |
public string ProName { get; set; } | |
/// <summary> | |
/// 顯示名稱 | |
/// </summary> | |
public override string ToString() | |
{ | |
return $"{ProCode}-{ProName}"; | |
} | |
} | |
/// <summary> | |
/// Repository:專案代碼 | |
/// </summary> | |
public class ProjectCodeNameRepo | |
{ | |
ProjectCodeName[] _codeList = null; | |
/// <summary> | |
/// Task:載入專案代碼。 | |
/// ※一建構就立刻(非同步)執行。 | |
/// </summary> | |
Task<ProjectCodeName[]> LoadTask = Task.Run(() => | |
{ | |
using (var conn = DBHelper.YourDB.Open()) | |
{ | |
//# GO | |
var codeList = conn.Query<ProjectCodeName>(@"SELECT ProCode, ProName FROM ProjectTable(NOLOCK) ORDER BY ProCode Desc ").ToArray(); | |
return codeList; | |
} | |
}); | |
/// <summary> | |
/// 專案代碼 | |
/// </summary> | |
public ProjectCodeName[] CoceList | |
{ | |
get | |
{ | |
if (_codeList == null) | |
_codeList = LoadTask.Result; // 等 LoadTask 跑完並取值。 | |
return _codeList; | |
} | |
} | |
/// <summary> | |
/// AutoCompoete:SearchFunc:專案代碼 | |
/// </summary> | |
public async Task<IEnumerable<ProjectCodeName>> SearchFunc(string keyword) | |
{ | |
if (_codeList == null) | |
_codeList = await LoadTask; // 等 LoadTask 跑完並取值。 | |
return _codeList.Where(c => keyword == null || c.ProName.Contains(keyword) || c.ProCode.Contains(keyword)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment