Skip to content

Instantly share code, notes, and snippets.

View HamidMolareza's full-sized avatar

Hamid Molareza HamidMolareza

View GitHub Profile
@HamidMolareza
HamidMolareza / Factory.cs
Created December 3, 2024 09:23
Factory Design Pattern example in C# for exporting Person data to JSON or text formats with flexible factory implementation.
using System.Text.Json;
// Client Code
public static class Program {
public static void Main() {
// Sample Data
var people = new List<Person> {
new("John", "Doe"),
new("Jane", "Smith"),
new("Alice", "Johnson")
@HamidMolareza
HamidMolareza / SimpleFactory.cs
Created December 3, 2024 09:13
Simple factory design pattern in C#
using System.Text.Json;
// Client Code
public static class Program {
public static void Main(string[] args) {
// Sample Data
var people = new List<Person> {
new("John", "Doe"),
new("Jane", "Smith"),
new("Alice", "Johnson")
@HamidMolareza
HamidMolareza / quera-294.py
Created October 24, 2024 11:50
A temporary solution for problem 294.
#https://quera.org/problemset/294
#This solution is failed in test case 8.
import math
# Read coefficients a, b, and c
a = float(input())
b = float(input())
c = float(input())
@HamidMolareza
HamidMolareza / docker-compose.yml
Created June 29, 2024 15:39
docker-compose template file for ASP + SqlServer
services:
api:
image: asp-api
build:
context: .
dockerfile: Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Production
- ConnectionStrings__Default=Server=db,1433;Database=AspApi;User Id=sa;Password=thisIsMssql@Password;TrustServerCertificate=True;
ports:
@HamidMolareza
HamidMolareza / dockerfile
Created June 29, 2024 15:37
dockerfile template for ASP project + test
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["./src/Api/ApiApp.csproj", "./"]
@HamidMolareza
HamidMolareza / sample-using-iprogress.cs
Last active March 31, 2025 16:58
Sample for using `IProgress` in long-running tasks in C#
/*
`IProgress<T>` in C# is an interface provided by the .NET Framework to report progress in an asynchronous operation. It allows a producer (usually a background task) to communicate progress updates to a consumer (often the UI thread or another monitoring task).
This setup ensures that the progress reporting mechanism is thread-safe and that the consumer of the progress updates can safely update the UI or other state based on the progress.
*/
// var progress = new Progress<(long count, List<string> files)>(ReportProgress);
var progress = new Progress<(long count, List<string> files)>();
progress.ProgressChanged += (_, data) => {
Console.WriteLine($"{data.count} files read. Last file: {data.files.Last()}");
@HamidMolareza
HamidMolareza / server-sent-event-sample.cs
Last active June 24, 2024 21:15
Server-Sent Events (sse) sample as minimal API in ASP
app.MapGet("/sse/connect", async (HttpContext context, CancellationToken ct) => {
context.Response.Headers.Append("Content-Type", "text/event-stream");
context.Response.Headers.Append("Cache-Control", "no-cache");
context.Response.Headers.Append("Connection", "keep-alive");
var result = new SSEResult();
for (var i = 0; i < 10; i++) {
if (ct.IsCancellationRequested) return;
result.DateTime = DateTime.Now;
@HamidMolareza
HamidMolareza / EF_SetDeleteFilter.cs
Created May 3, 2024 17:48
Global Entity Framework filter for soft deleting
using System.Linq.Expressions;
using ConsoleApp.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
namespace ConsoleApp.Data;
public class AppDbContext : DbContext {
protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
@HamidMolareza
HamidMolareza / INotifyPropertyChanged.cs
Last active June 24, 2024 21:16
Implement INotifyPropertyChanged in C#
using System.ComponentModel;
using System.Runtime.CompilerServices;
var person = new Person {
FirstName = "Hamid"
};
person.PropertyChanged += HandlePersonPropertyChanged;
person.FirstName = "Mohammad";
return;
@HamidMolareza
HamidMolareza / MySingleton.cs
Last active December 28, 2023 03:34
The best way to define a Thread-Safe singleton class in C#
public sealed class MySingleton
{
private static readonly Lazy<MySingleton> instance = new Lazy<MySingleton>(() => new MySingleton());
private MySingleton()
{
// Private constructor to prevent instantiation.
}
public static MySingleton Instance => instance.Value;