Skip to content

Instantly share code, notes, and snippets.

@tocalai
tocalai / SQLEnlight.csv
Last active March 15, 2024 06:54
Rules list
SA# Title Note
SA0027 Avoid wrapping filtering columns within a function in the WHERE clause Performance concern
SA0081 Do not use DECIMAL or NUMERIC data types without specifying precision and scale Logical concern
SA0106 Avoid OR operator in queries Performance concern
SA0168 Possible division by zero not handled according the practice Logical concern
SA0251 Subquery used in expression not ensured to return a single value Logical concern
[Route("api/[controller]")]
public class FileController : ControllerBase
{
[HttpPost("uploadlarge")]
[DisableFormValueModelBinding]
public async Task<IActionResult> UploadLargeFileAsync()
{
try
{
if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
// files: suppose we have a list of file paths
// subDirectory: the sub-directory structure under root for storing
public async Task<HttpResponseMessage> UploadFilesAsync(List<string> files, string subDirectory)
{
string boundary = $"--{DateTime.Now.Ticks:x}";
using var content = new MultipartFormDataContent(boundary);
content.Headers.Add("ContentType", $"multipart/form-data, boundary={boundary}");
foreach (var filePath in files)
{
@tocalai
tocalai / MonitorServiceTest.cs
Last active February 9, 2022 06:03
Moq sample for testing MonitorService.
[TestClass]
public class MonitorServiceTest
{
private MonitorService _monitorService; // System under test (SUT)
private Mock<MailService> _mockMailService; // Test Double
private Mock<IOhterService> _mockOtherService; // Test Double
private IOptions<ConfigModel> _configModelOptions; // Configuration
[TestInitialize] // Setup
public void Initialize()
@tocalai
tocalai / HttpRequestHelper.cs
Created January 18, 2022 02:15
Http request help for retrieving result as string or as stream.
public class HttpRequestHelper
{
public static async Task<string> PostReturnString(RequestArgs query, string uri)
{
using (var httpClient = new HttpClient())
{
var response = await httpClient.PostAsync(new Uri(uri),
new StringContent(JsonConvert.SerializeObject(query), Encoding.UTF8, "application/json"));
var responseContent = await response.Content.ReadAsStringAsync();
COUNTRY COUNTRY CODE
Afghanistan 93
Albania 355
Algeria 213
American Samoa 1-684
Andorra 376
Angola 244
Anguilla 1-264
Antarctica 672
Antigua and Barbuda 1-268
@tocalai
tocalai / RequestMonitorEx.cs
Last active June 25, 2021 07:02
Mock class for demonstrate apply cache policy for controlling duplicated request.
// supposed we have class to monitor request
public class RequestMonitor {
private readonly Context _context;
private readonly IMemoryCache _cache;
public RequestMonitor (Context context, IMemoryCache cache) {
_context = context; // DI the context of Http request
_cache = cache;
}
@tocalai
tocalai / RequestMonitor.cs
Last active June 25, 2021 03:56
Mock class to demonstrate monitoring http request in.
// supposed we have class to monitor request
public class RequestMonitor {
private readonly Context _context;
public RequestMonitor (Context context) {
_context = context; // DI the context of Http request
}
public void OnRequested () {
// retrieved http request information
@tocalai
tocalai / ExcelExtenstion.cs
Last active May 27, 2021 01:41
Demonstrate how to export excel and format datetime column(s) style for human readable.
public static class ExcelExtension {
public static FileInfo Export<T> (string filepath, List<T> data) {
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
var output = new FileInfo (filepath);
if (output.Exists) output.Delete ();
if (!Directory.Exists (Path.GetDirectoryName (filepath))) Directory.CreateDirectory (Path.GetDirectoryName (filepath));
using (var excel = new ExcelPackage (output)) {
var ws = excel.Workbook.Worksheets.Add ("Sheet1"); // create sheet
@tocalai
tocalai / Program.cs
Created December 29, 2020 06:04
Demonstrate how to use service account to access Google Sheets doc.
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace SheetsQuickstart