Open Source Projects

Common code shared between our projects, available for all to use.

Published: 21-02-2025
  • Initial Release

Toodle.CommonExtensions

NuGet Version NuGet Downloads

A collection of useful extension methods for caching, enum handling, geographic calculations, JSON serialization, and string manipulation.

Installation

Install the package via NuGet:

dotnet add package Toodle.CommonExtensions

Features

Cache Extensions

The CacheExtensions class provides methods for generating cache keys:

Fast Cache Keys

using Toodle.CommonExtensions.Cache;

var person = new Person { Id = 1, Name = "John" };
string key = person.ToCacheKeyFast(); // Returns "Person_[hash]"
string keyWithPrefix = person.ToCacheKeyFast("App1"); // Returns "App1_Person_[hash]"

Note: Hash codes from ToCacheKeyFast are not guaranteed to be consistent across application restarts.

Stable Cache Keys

using Toodle.CommonExtensions.Cache;

var person = new Person { Id = 1, Name = "John" };
string key = person.ToCacheKeyStable(); // Returns "Person_[hash]"
string keyWithPrefix = person.ToCacheKeyStable("App1"); // Returns "App1_Person_[hash]"

Uses FNV-1a hash algorithm to ensure consistency across application restarts.

Enum Extensions

The EnumExtensions class provides methods for working with enums:

using Toodle.CommonExtensions;

public enum Status { Active, Inactive, Pending }

// Get all enum values sorted alphabetically
List<Status> allStatuses = EnumExtensions.GetEnumValues<Status>();

// Get enum values excluding specific items
List<Status> filteredStatuses = EnumExtensions.GetEnumValues<Status>(
    new[] { Status.Inactive }
);

Geography Helper

The GeographyHelper class provides methods for geographic calculations:

using Toodle.CommonExtensions.Helpers;

// Calculate distance between London and Paris
double distance = GeographyHelper.GetDistance(
    51.5074, -0.1278,  // London coordinates
    48.8566, 2.3522    // Paris coordinates
);
// Returns distance in meters using the Haversine formula

JSON Extensions

The JsonExtensions class provides methods for JSON serialization:

using Toodle.CommonExtensions.Json;

var person = new Person { Name = "John", Age = 30 };

// Basic serialization
string json = person.ToJson();

// Serialization with custom options
var options = new JsonSerializerOptions
{
    WriteIndented = true
};
string prettyJson = person.ToJson(options);

String Extensions

The StringExtensions class provides string manipulation methods:

Converting to Initials

using Toodle.CommonExtensions.String;

string initials = "hello world".ToInitials(); // Returns "HW"
string multipleWords = "United Kingdom".ToInitials(); // Returns "UK"
string multipleSpaces = "  multiple   spaces   ".ToInitials(); // Returns "MS"

Removing Non-Alphabetic Characters

using Toodle.CommonExtensions.String;

string cleaned = "Hello123World!".RemoveNonAlphabeticCharacters(); // Returns "HelloWorld"
string numbersOnly = "12345".RemoveNonAlphabeticCharacters(); // Returns ""
string withSpaces = "Hello World!".RemoveNonAlphabeticCharacters(); // Returns "HelloWorld"
Published: 18-02-2025
  • 2025-02-18: Initial Release

Toodle.PageOptimizer

NuGet Version NuGet Downloads

A metadata, breadcrumb and resource manager for ASP.NET Core websites.

Installation

dotnet add package Sixpoints.PageOptimizer

Usage

  1. Add the service in Program.cs:
builder.Services.AddMetaData(
    siteName: "My Website",
    baseUrl: "https://example.com",
    locale: "en-US"
);
  1. Add the tag helper in _ViewImports.cshtml:
@addTagHelper *, MetaData.AspNetCore
  1. Use the tag helper in your views:
<head>
    <meta-data-tags />
</head>
  1. Use the service in your controllers:
public class ArticleController : Controller
{
    private readonly IMetaDataService _metaDataService;

    public ArticleController(IMetaDataService metaDataService)
    {
        _metaDataService = metaDataService;
    }

    public IActionResult Article(string slug)
    {
        _metaDataService
            .SetMetaTitle("Article Title")
            .SetMetaDescription("Article description")
            .AddBreadCrumb("Category", "/category")
            .AddBreadCrumb("Article Title")
            .SetCanonicalUrl($"/article/{slug}");

        return View();
    }
}