Skip to content

Instantly share code, notes, and snippets.

View mirmostafa's full-sized avatar
🧗‍♂️
Coding is my lifestyle. But I live in Iran 😢

Mohammad Mirmostafa mirmostafa

🧗‍♂️
Coding is my lifestyle. But I live in Iran 😢
View GitHub Profile
@mirmostafa
mirmostafa / async.ts
Last active September 9, 2021 17:49
How to use async/await in Typescript
##def:
private getMerchantConfigData() {
return new Promise((resolve, reject) => {
this.configService.get().subscribe(res => {
this.merchantConfigData = res;
resolve(true);
});
})
}
@mirmostafa
mirmostafa / Startup.cs
Last active September 12, 2021 11:41
Solve CORS Origin Problem: Server-side
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
//....
app.UseCors("AllowAnyOrigin");
app.UseCors("AllowOrigin");
app.UseCors("CorsPolicy");
app.UseCors(builder => builder
//.AllowAnyOrigin()
.SetIsOriginAllowed(origin => true)
.AllowAnyMethod()
@mirmostafa
mirmostafa / FastAddString.cs
Last active September 13, 2021 10:05
The fastest way to add a string to another.
public static string? Add(this string? s, int count, char add = ' ', bool before = false)
=> string.Create(s?.Length ?? 0, s, (span, value) =>
{
value.AsSpan().CopyTo(span);
(before ? span[..count] : span[count..]).Fill(add);
});
@mirmostafa
mirmostafa / StringHelper.cs
Created September 16, 2021 09:54
New string null check
public static bool IsNullOrEmpty([NotNullWhen(false)] this string? str) => str?.Trim() is null or { Length: 0 };
@mirmostafa
mirmostafa / get_col_info.sql
Last active September 19, 2021 07:20
Get column schema and information including description in SQL Server
SELECT *
FROM sys.extended_properties p
JOIN sys.columns c
ON p.major_id = c.object_id
AND p.minor_id = c.column_id
WHERE p.major_id = OBJECT_ID('<table_name>', 'table')
@mirmostafa
mirmostafa / TechnicalFundamentals.md
Last active October 4, 2021 13:41
دامنه‌هایی از برنامه‌نویسی که حتما باید مورد بررسی و آموزش قرار گیرند

Technologies

  • .Net 6.0
  • ORM Choose (E.F., Dapper, nHibernate, LLBGenPro...)
  • Web API
  • Visual Studio Tools and Features

Coding Style

  • C# Programming Features
    • Coding Features
    • New Features
  • IDE Code Clean-up and Code Style
@mirmostafa
mirmostafa / 1-Conditional.cs
Last active December 27, 2021 13:28
Pattern Matchings
if (randomShape is Circle { Diameter: 10, Radius: < 5 and > 2, Area: <= 15 } c)
{
Console.WriteLine($"This is my circle. Area: {c.Area}");
}
if (randomShape is not null)
{
}
if (randomShape is not Recatngle)
{
internal readonly record struct Person(string Name, int Age);
var people = new List<Person> { new("Ali", 20), new("Reza", 30), new("Mohammad", 40) };
var behzad = people.FirstOrDefault(x => x.Name == "Behzad", new("Behzad", 37));
output: Person { Name = Behzad, Age = 37 }
@mirmostafa
mirmostafa / WaitAsync.cs
Created October 11, 2021 17:00
.Net 6.0 Task.WaitAsync
var cts = new CancellationTokenSource();
var httpClient = new HttpClient();
var response = await httpClient.GetAsync("https://localhost:5001/downloadfile", cts.Token)
.WaitAsync(TimeSpan.FromMilliseconds(4200), cts.Token);
@mirmostafa
mirmostafa / build_main_on_push.yaml
Created October 13, 2021 21:39
Git Action to build "main" branch o push
name: Build main branch on push
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: windows-latest