Skip to content

Instantly share code, notes, and snippets.

@osisdie
osisdie / 01_Cache_Test_In_Order.cs
Last active May 3, 2021 07:32
[c# .NET Standard 2.0] Run xUnit test cases in order
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using StackExchange.Redis;
using Xunit;
[assembly: CollectionBehavior(DisableTestParallelization = true)]
@osisdie
osisdie / 01_ObjectArrayToHtmlTable_Extension.cs
Created April 18, 2021 02:33
[c# .NET Standard 2.0] Convert a list of objects into Html table string
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
namespace Examples.Extensions
{
public static class ObjectArrayToHtmlTable_Extension
{
@osisdie
osisdie / 01_ParseNumberByNumberStyles_Test.cs
Last active February 16, 2021 03:27
[c# .NET Standard 2.0] To test string "1.0" can be number parsed correctly or not
public class ParseNumberByNumberStyles_Test
{
[Fact]
public void DefaultNumberParsing_TestCase()
{
{
var val = "1.0";
var isSuccess = int.TryParse(val, out int parsedNumber);
Assert.False(isSuccess);
}
@osisdie
osisdie / 01_CheckboxGroupNotEmptyValidation.cs
Created May 10, 2020 04:46
[c# .NET Standard 2.0] Custom ValidationAttribute to check a grouping checkbox checks at least an item
public class CheckboxGroupNotEmptyValidation : ValidationAttribute
{
readonly object TRUE = true;
string[] _alltheOtherProperty;
public CheckboxGroupNotEmptyValidation(params string[] alltheOthersProperty)
{
_alltheOtherProperty = alltheOthersProperty;
}
@osisdie
osisdie / 01_ServiceCollectionExtension.cs
Last active May 10, 2020 04:49
[c# .NET Standard 2.0] An IServiceCollection extension to automatically register all derived classes implements the IRepository interface
public static class ServiceCollectionExtension
{
public static IServiceCollection AddDerivedRepository(this IServiceCollection services, ServiceLifetime lifetime)
{
var scanAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
var interfaces = scanAssemblies.SelectMany(o => o.DefinedTypes
.Where(x => x.IsInterface)
.Where(x => x != typeof(IRepository)) // exclude super interface
.Where(x => typeof(IRepository).IsAssignableFrom(x))
);
@osisdie
osisdie / 01_MethodReturnType_Test.cs
Last active February 16, 2021 03:29
[c# .NET Standard 2.0] To test all kinds of methods' return types are IsGenerticType or not
public class MethodReturnType_Test
{
[Fact]
public void IsGenericType_ReturnFalse_TestCase()
{
var method1 = GetType().GetMethod(nameof(Method1ReturnVoid),
BindingFlags.Instance | BindingFlags.NonPublic
);
Assert.NotNull(method1);
Assert.Equal(typeof(void), method1.ReturnType);
@osisdie
osisdie / 01_ServiceCollectionExtension.cs
Last active April 26, 2020 11:38
[c# .NET Standard 2.0] Migrate Autofac.RegisterAssemblyTypes to Microsoft.Extensions.DependencyInjection.IServiceCollection
public static IServiceCollection RegisterAssemblyTypes<T>(this IServiceCollection services, ServiceLifetime lifetime, params Assembly[] assemblies) =>
services.RegisterAssemblyTypes<T>(lifetime: lifetime, predicates: null, assemblies: assemblies);
public static IServiceCollection RegisterAssemblyTypes<T>(this IServiceCollection services,
ServiceLifetime lifetime, List<Func<TypeInfo, bool>> predicates, params Assembly[] assemblies)
{
var inputAssemblies = assemblies.ToList();
scanAssemblies.SelectMany(x => x.GetReferencedAssemblies())
.Where(t => false == scanAssemblies.Any(a => a.FullName == t.FullName))
.Distinct()
@osisdie
osisdie / 01_IsAssignableFrom_Test.cs
Created April 26, 2020 06:21
[c# .NET Standard 2.0] System.Type and Reflection UnitTests
public class Interface_Test : TestBase
{
[Fact]
public void IsAssignable_Test()
{
Assert.True(typeof(IRepository).IsAssignableFrom(typeof(IRepository)));
Assert.True(typeof(IRepository).IsAssignableFrom(typeof(IEmployeeRepository)));
Assert.True(typeof(IRepository).IsAssignableFrom(typeof(RepositoryBase)));
Assert.True(typeof(IRepository).IsAssignableFrom(typeof(EmployeeRepository)));
Assert.True(typeof(IRepository).IsAssignableFrom(typeof(CustomerRepository)));
@osisdie
osisdie / 01_replace_docker_image_tag_by_sed.sh
Last active February 2, 2020 03:01
[bash] Change EKS deployment.yaml's image version on ECR with new tag via REGEX + sed
TAG_REGEX="[0-9]\+.[0-9]\+.[0-9]\+[.-]\(build\)\?[0-9]\+"
ECR_HOST="123456789.dkr.ecr.us-west-2.amazonaws.com"
ECR_REPO="hello-api"
target_file="02_deployment.yaml"
next_version="1.0.0.1"
# 1.0.0.0 => 1.0.0.1
sed -i "s/$(ECR_HOST)\/$(ECR_REPO):$(TAG_REGEX)/$(ECR_HOST)\/$(ECR_REPO):$(next_version)/" $target_file
cat $target_file
@osisdie
osisdie / 01_eks_azure-pipelines.yml
Created January 14, 2020 03:32
[.Net Core 3.1] Azure pipeline + private azure nuget feed + awscli + ecr + eks
# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://docs.microsoft.com/en-us/azure/devops/pipelines/ecosystems/dotnet-core?view=azure-devops
name: "Build API && Deploy"
trigger:
branches:
include:
- feat/*