Skip to content

Instantly share code, notes, and snippets.

@rozputnii
rozputnii / ClearHtml.cs
Created May 15, 2023 23:27
.NET Clear string from html
public static string CleanHtml(this string html)
{
if (string.IsNullOrWhiteSpace(html))
return html;
var buffer = new char[html.Length];
var output = buffer.AsSpan();
var inTag = false;
var length = 0;
@rozputnii
rozputnii / Comparer.cs
Created November 17, 2020 09:54
Comparer
internal sealed class EntityComparer<TSource, TDestination>
{
private readonly TSource source;
private readonly TDestination destination;
public EntityComparer(
TSource source,
TDestination destination)
{
this.source = source;
public static class ThrowHelper
{
/// <summary>
/// Throws <see cref="ArgumentNullException" /> if value is null.
/// </summary>
/// <exception cref="ArgumentNullException"></exception>
public static void ThrowIfNull<T>(Expression<Func<T>> propertyExpression)
{
if (propertyExpression.Compile().Invoke() is not null)
{
@rozputnii
rozputnii / .Net Standart. JsonExtensions
Created September 25, 2018 10:19
.Net Standart. JsonExtensions for serialize/deserialize any types
public static class JsonExtensions
{
private static readonly JsonSerializer JsonSerializer = JsonSerializer.CreateDefault();
/// <summary>
/// Serializes the specified instance and writes the JSON structure
/// </summary>
/// <param name="value">The value of <typeparam name="T"></typeparam> to serialize.</param>
/// <typeparam name="T">The type of the object to serialize.</typeparam>
public static string ToJson<T>(this T value)
@rozputnii
rozputnii / EF Core. Override SaveChanges
Created September 25, 2018 09:53
.Net Core EF - Override DbContext.SaveChanges and apply additional checks
public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
OnBeforeSaving();
return base.SaveChanges(acceptAllChangesOnSuccess);
}
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess,
CancellationToken cancellationToken = new CancellationToken())
{
OnBeforeSaving();
@rozputnii
rozputnii / EF Core. Soft delete query filter
Last active September 25, 2019 11:26
.Net Core EF - Apply Soft Delete query filter for the entity types
/// <summary>
/// Apply filter for hide soft deleted entities which implement the <see cref="ISoftDeletable" />
/// </summary>
/// <param name="modelBuilder"><see cref="ModelBuilder" /></param>
public static void ApplySoftDeleteFilter(this ModelBuilder modelBuilder)
{
bool IsSoftDeletable(ITypeBase t) =>
t != null && t.HasClrType() && typeof(ISoftDeletable).IsAssignableFrom(t.ClrType);
var softDeletableTypes = modelBuilder.Model.GetEntityTypes()
@rozputnii
rozputnii / EF Core. Apply entities configurations from Assembly
Last active September 25, 2018 09:55
.Net Core EF - Search and apply entity configs in current assembly using ModelBuilder
public static void ApplyConfigurationsFromAssembly(this ModelBuilder modelBuilder, Assembly assembly)
{
var interfaceType = typeof(IEntityTypeConfiguration<>);
var methodApplyConfiguration = typeof(ModelBuilder)
.GetMethods()
.SingleOrDefault(i => i.IsGenericMethod
&& i.Name == nameof(ModelBuilder.ApplyConfiguration)
&& i.GetParameters().Any(p => p.ParameterType.Name == interfaceType.Name));
using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime.Serialization.Formatters.Binary;
public static class ReadWriteMemoryMappedFile
{
// allocated memory for this memory mapped file (bytes)
private const int MMF_MAX_SIZE = byte.MaxValue;
var ftpRequest = (FtpWebRequest) WebRequest.Create($"{host}/{path}");
ftpRequest.Credentials = new NetworkCredential(username, password);
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
@rozputnii
rozputnii / ConsoleEventDelegate
Created July 20, 2016 21:27
ConsoleEventDelegate
using System;
using System.Runtime.InteropServices;
using System.Threading;
class Program
{
static void Main(string[] args)
{
handler = new ConsoleEventDelegate(ConsoleEventCallback);
SetConsoleCtrlHandler(handler, true);