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 / launchsettings.json
Created February 12, 2021 04:06
dotnet Watch in Visual Studio
"Watch": {
"commandName": "Executable",
"executablePath": "dotnet.exe",
"workingDirectory": "$(ProjectDir)",
"commandLineArgs": "watch run",
"launchBrowser": true,
"launchUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
@mirmostafa
mirmostafa / Consumer.csproj
Created August 27, 2021 16:33
C# Source Generator Template
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup>
@mirmostafa
mirmostafa / Startup.cs
Created August 30, 2021 15:35
DbContext Pool Service
public void ConfigureServices(IServiceCollection services)
{
//....
services.AddDbContextPool<S2ReadDbContext>(options =>
{
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
options.UseSqlServer(Configuration.GetConnectionString("S2ReadConnection"));
options.EnableSensitiveDataLogging();
@mirmostafa
mirmostafa / DisposeAsync.Method.cs
Last active September 9, 2021 17:55
IAsyncDisposable Pattern
public virtual ValueTask DisposeAsync()
{
try
{
Dispose();
return default;
}
catch (Exception exc)
{
return new ValueTask(Task.FromException(exc));
@mirmostafa
mirmostafa / Person.cs
Created August 30, 2021 15:42
Constructor & Extension Deconstruct
class Person {
public Person (string first, string last) => (this.First, this.Last) = (first, last);
}
public static class Extensions {
public static void Deconstruct(this Person p, out string first, out string last) => (first, last) = (p.First, p.Last);
}
@mirmostafa
mirmostafa / MockDbContext.cs
Created August 30, 2021 15:45
In-Memory Mock DbContext for UnitTesting
var options = new DbContextOptionsBuilder<MyDbContext>
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
var context = new MyDbContext(options);
context.Database.EnsureCreated();
@mirmostafa
mirmostafa / InterpolatedString.cs
Last active April 21, 2022 09:05
From SQL Interpolated String
var dbResult = await this._readDbContext.ProductCategories.FromSqlInterpolated($@"
SELECT TOP 1 *
FROM [biz].[ProductCategories] t
WHERE NOT EXISTS (SELECT 1
FROM [biz].[ProductCategories]
WHERE ParentProductCategoryId = t.ProductCategoryId)
AND t.ProductCategoryId = {parameters.CategoryId} AND MerchantId = {merchantId}")
.AsNoTracking().FirstOrDefaultAsync();
@mirmostafa
mirmostafa / WebApi.Program.cs
Created September 4, 2021 06:24
Setup logger
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddDebug().AddConsole();
logging.AddFilter("", LogLevel.Information)
.AddFilter("Microsoft.AspNetCore.Routing.EndpointMiddleware", LogLevel.Warning)
.AddFilter("Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker", LogLevel.Warning)
.AddFilter("Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor", LogLevel.Warning);
@mirmostafa
mirmostafa / project.csproj
Last active December 14, 2021 17:48
.NET and C# Latest Version Project
<PropertyGroup>
<Nullable>enable</Nullable>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>Preview</LangVersion>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
</PropertyGroup>
@mirmostafa
mirmostafa / .cmd
Created September 9, 2021 12:41
Installing EF Tool
dotnet tool install --global dotnet-ef