Skip to content

Instantly share code, notes, and snippets.

@jame2408
jame2408 / ChatGpt.cs
Last active June 26, 2023 02:25
連線 ChatGPT 範例程式碼(C#)
using Newtonsoft.Json;
namespace API.SDK;
public interface IChatGpt
{
Task<Result> Call(string message, string model = "text-davinci-003");
}
public class ChatGpt : IChatGpt
@jame2408
jame2408 / Populate_IConfiguration_for_Unit_Tests.cs
Last active September 5, 2022 07:27
單元測試中,注入 Configuration 至被測試對象(SUT)中。
// From: https://stackoverflow.com/a/55497919
using Microsoft.Extensions.Configuration;
var myConfiguration = new Dictionary<string, string>
{
{"Key1", "Value1"},
{"Nested:Key1", "NestedValue1"},
{"Nested:Key2", "NestedValue2"}
};
@jame2408
jame2408 / NestedConditionals_原始程式.cs
Created December 24, 2017 12:54
NestedConditionals - 程式碼調整前與調整後
using System;
namespace CleanCode.NestedConditionals
{
public class Customer
{
public int LoyaltyPoints { get; set; }
}
public class Reservation
@jame2408
jame2408 / LongParameterList_原始程式.cs
Created March 12, 2017 07:59
LongParameterList - 程式碼調整前與調整後
using System;
using System.Collections.Generic;
namespace CleanCode.LongParameterList
{
public class LongParameterList
{
public IEnumerable<Reservation> GetReservations(
DateTime dateFrom, DateTime dateTo,
User user, int locationId,
@jame2408
jame2408 / Comments_原始程式.cs
Created March 5, 2017 12:03
Comments - 程式碼調整前與調整後
using System;
using System.Collections.Generic;
using System.Net.Mail;
namespace CleanCode.Comments
{
public class Comments
{
private int _pf; // pay frequency
private DbContext _dbContext;
@jame2408
jame2408 / MagicNumbers_原始程式.cs
Created March 5, 2017 06:30
MagicNumbers - 程式碼調整前與調整後
namespace CleanCode.MagicNumbers
{
public class MagicNumbers
{
public void ApproveDocument(int status)
{
if (status == 1)
{
// ...
}
@jame2408
jame2408 / VariableDeclarationsAtTheTop_原始程式.cs
Created March 4, 2017 13:42
VariableDeclarationsAtTheTop - 程式碼調整前與調整後
namespace CleanCode.VariableDeclarationsAtTheTop
{
public class PayCalculator
{
private PayFrequency _payFrequency;
public PayCalculator(PayFrequency payFrequency)
{
_payFrequency = payFrequency;
@jame2408
jame2408 / OutputParameters_原始程式.cs
Last active March 4, 2017 13:40
OutputParameters - 程式碼調整前與調整後
using System;
using System.Collections.Generic;
namespace CleanCode.OutputParameters
{
public class OutputParameters
{
public void DisplayCustomers()
{
int totalCount = 0;
@jame2408
jame2408 / 刪除完全重複資料.sql
Last active January 4, 2017 06:38
使用CTE,刪除完全重複資料
/*
刪除完全重複資料
*/
--step I:建立資料
declare @temptable table
(
name nvarchar(10),
tel varchar(10)
);
@jame2408
jame2408 / DATEADD與DATEDIFF的應用.sql
Last active September 26, 2022 06:25
DATEADD 與 DATEDIFF 日期應用
/*
DATEADD與DATEDIFF的應用:
1. DATEDIFF:起始日與迄日之間差異了幾天(Day)/月(MONTH)/季(QUARTER)/年(YEAR)
- 常見用法:DATEDIFF(Day/Week/MONTH/QUARTER/YEAR, 起日, 迄日)
- 起日/迄日:
- 日期部份若放為『0』或『''』,代表資料庫最小時間1900-01-01 00:00:00.000。
- 同理,若為『-1』,代表1899-12-31 00:00:00.000。
2. DATEADD:增加/減少天數(Day)/月(MONTH)/季(QUARTER)/年(YEAR),範例如下:
- 加一個月 DATEADD(MONTH, 1 ,GETDATE())