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 / IsNullOrEmpty-SingleMethod.cs
Created July 30, 2022 08:26
IsNullOrEmpty, using pattern matching
public static bool IsNullOrEmpty([NotNullWhen(false)] this string? str) => str?.Length is null or 0;
public static bool IsNullOrEmpty([NotNullWhen(false)] this string? str) => str is null or { Length: 0 };
@mirmostafa
mirmostafa / Program.cs
Created July 23, 2022 04:28
Use Autofac in Blazor server-side
using Autofac;
using Autofac.Extensions.DependencyInjection;
using HanyCo.Infra;
using Library.Cqrs;
using Library.Mapping;
namespace BlazorApp;
@mirmostafa
mirmostafa / 1_Monad.cs
Created July 21, 2022 08:57
Simple Monad Design Pattern in C#
public class Monad<TValue>
{
private readonly TValue? _value;
public Monad(TValue? value)
=> this._value = value;
public Monad<TResult?> AnyWay<TResult>(in Func<TValue?, TResult?> func)
=> new(func(this._value));
@mirmostafa
mirmostafa / PredicateBuilder.cs
Created April 20, 2022 15:09
Dynamically Composing Expression Predicates
using System.Linq.Expressions;
namespace Library.Data.Linq;
//! Reference: http://www.albahari.com/nutshell/predicatebuilder.aspx
public static class PredicateBuilder
{
public static Expression<Func<T, bool>> True<T>() => f => true;
public static Expression<Func<T, bool>> False<T>() => f => false;
@mirmostafa
mirmostafa / ImageHelpers.cs
Created March 29, 2022 06:16
Resize the image to the specified width and height.
/// <summary>
/// Resize the image to the specified width and height.
/// </summary>
/// <param name="image">The image to resize.</param>
/// <param name="width">The width to resize to.</param>
/// <param name="height">The height to resize to.</param>
/// <returns>The resized image.</returns>
public static Bitmap ResizeImage(Image image, int width, int height)
{
//a holder for the result
@mirmostafa
mirmostafa / Directory.Packages.props
Created March 19, 2022 21:03
Common .csproj file enabled all the features by now. Also cane be used in `Directory.Packages.props` file to be apply to all the projects
<Project>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>preview</LangVersion>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
<GenerateRequiresPreviewFeaturesAttribute>true</GenerateRequiresPreviewFeaturesAttribute>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<PropertyGroup>
@mirmostafa
mirmostafa / SystemHostsFileManager.cs
Created March 14, 2022 13:47
C:\Windows\System32\drivers\etc\hosts file manager
public static class SystemHostsFileManager
{
private static readonly string _filePath = @"C:\Windows\System32\drivers\etc\hosts";
public static IEnumerable<(string Ip, string Site, string? Description)> ReadRules() =>
from rule in File.ReadAllText(_filePath).Split(Environment.NewLine)
where !string.IsNullOrEmpty(rule) && !rule.StartsWith("#")
let description = rule.IndexOf("#") > 0 ? rule[(rule.IndexOf("#") + 1)..] : null
let pair = rule.Split(" ")
let ip = pair[0].Trim()
@mirmostafa
mirmostafa / XmlSerializer.cs
Created March 3, 2022 08:34
XML Serializer
public static class XmlSerializer
{
public static void Serialize<T>(T? o, string path!!, bool indent = true)
{
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
using var writer = new StreamWriter(path);
using var xmlWriter = System.Xml.XmlWriter.Create(writer, new System.Xml.XmlWriterSettings { Indent = indent });
serializer.Serialize(xmlWriter, o);
}
@mirmostafa
mirmostafa / 0 - Extensions.cs
Last active February 13, 2022 14:41
Functional Programming
puvlic static class Extensions
{
public static Func<TResult> Compose<TResult>(this Func<TResult> create!!, params Func<TResult, TResult>[] funcs!!)
{
var result = () =>
{
var value = create();
foreach (var func in funcs)
{
value = func(value);
@mirmostafa
mirmostafa / QueryableHelper.cs
Last active February 1, 2022 06:43
IQueryable To Paging List Async
public record PagingParams(in int PageIndex = 0, in int? PageSize = null);
public record PagingResult<T>(IReadOnlyList<T> Result, in long TotalCount);
public static async Task<PagingResult<T>> ToListPagingAsync<T>(this IQueryable<T> query, PagingParams? paging, CancellationToken cancellationToken = default)
{
var total = await query.CountAsync(cancellationToken: cancellationToken);
if (paging is null or { PageSize: null or 0 })
{
var dbNoPagingResult = await query.ToListAsync(cancellationToken: cancellationToken);