Skip to content

Instantly share code, notes, and snippets.

View karenpayneoregon's full-sized avatar
🎯
Focusing

Karen Payne karenpayneoregon

🎯
Focusing
View GitHub Profile
@karenpayneoregon
karenpayneoregon / GreaterThanAttribute.cs
Created October 10, 2025 14:17
Data Annotation validation attribute [GreaterThan("8/1/2025")]
using System.ComponentModel.DataAnnotations;
/// <summary>
/// Specifies a validation attribute that checks if the value of a property or field
/// is greater than a specified minimum value.
/// </summary>
/// <remarks>
/// This attribute is typically used to validate that a <see cref="DateTime"/> value
/// is greater than the defined <see cref="Minimum"/> value. It is applied to properties
/// or fields and ensures that the validation logic is enforced.
@karenpayneoregon
karenpayneoregon / FluentValidationExtensions.cs
Last active October 3, 2025 22:19
FluentValidation log validation errors as json
using System.Text.Json;
using FluentValidation.Results;
namespace TODO.LanguageExtensions;
/// <summary>
/// Provides extension methods for FluentValidation to simplify validation and error handling.
/// </summary>
public static class FluentValidationExtensions
{
@karenpayneoregon
karenpayneoregon / RuleBuilderExtensions.cs
Created October 1, 2025 14:45
FluentValidation extension for nullable DateOnly
using FluentValidation;
namespace FluentWebApplication1.Validators;
public static class RuleBuilderExtensions
{
/// <summary>
/// Adds a validation rule to ensure that a nullable <see cref="DateOnly"/> property is not null
/// and falls within a valid date range.
/// </summary>
@karenpayneoregon
karenpayneoregon / readme.md
Last active September 25, 2025 17:40
Add page indicator for ASP.NET Core projects

Add the code to an ASP.NET Core project to indicate the current page.

@karenpayneoregon
karenpayneoregon / Program.cs
Last active September 24, 2025 17:20
Happy birthday sample
using Spectre.Console;
using System.Text;
namespace BirthDay;
internal partial class Program
{
static void Main(string[] args)
{
// 1) Force UTF-8 so emojis aren’t mangled into question marks
Console.OutputEncoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
@karenpayneoregon
karenpayneoregon / .filenesting.json
Last active September 5, 2025 19:55
C# partial properties
{
"help": "https://go.microsoft.com/fwlink/?linkid=866610",
"root": true,
"dependentFileProviders": {
"add": {
"fileToFile": {
"add": {
"Client.Sets.cs": [
"Client.cs"
]
public static partial class Helpers
{
public static string NextValue1(string sender, int incrementBy = 1)
{
var index = sender.Length - 1;
while (index >= 0 && char.IsDigit(sender[index]))
index--;
if (index == sender.Length - 1)
return sender + incrementBy.ToString();
@karenpayneoregon
karenpayneoregon / DemoCode.cs
Created August 31, 2025 09:46
C# CommaDelimitedStringCollection
AnsiConsole.MarkupLine("[yellow]Comma delimited full month names[/]");
CommaDelimitedStringCollection result1 = [];
result1.AddRange(DateTimeFormatInfo.CurrentInfo.MonthNames[..^1]);
Console.WriteLine($"{result1}");
AnsiConsole.MarkupLine("[yellow]Using an int array[/]");
int[] items = [1, 2, 3];
CommaDelimitedStringCollection result2 = new();
result2.AddRange(items.ToStringArray());
@karenpayneoregon
karenpayneoregon / DateTimeHelpers.cs
Created August 25, 2025 13:50
Date Time Helpers
public static class DateTimeHelpers
{
/// <summary>
/// Generates a list of dates representing the next week's dates starting from the upcoming Sunday.
/// </summary>
/// <returns>A list of <see cref="DateOnly"/> objects representing the dates of the next week.</returns>
public static List<DateOnly> NextWeeksDates()
{
var start = DateTime.Now;
var nextSunday = DateOnly.FromDateTime(start).Next(DayOfWeek.Sunday);
@karenpayneoregon
karenpayneoregon / SetupLogging.cs
Created August 14, 2025 13:52
EF Core slow query Interceptor
using Serilog;
using static System.DateTime;
namespace TODO.Classes;
public class SetupLogging
{
public static void Development()
{