PandaTech.BaseConverter
4.0.2
See the version list below for details.
dotnet add package PandaTech.BaseConverter --version 4.0.2
NuGet\Install-Package PandaTech.BaseConverter -Version 4.0.2
<PackageReference Include="PandaTech.BaseConverter" Version="4.0.2" />
paket add PandaTech.BaseConverter --version 4.0.2
#r "nuget: PandaTech.BaseConverter, 4.0.2"
// Install PandaTech.BaseConverter as a Cake Addin #addin nuget:?package=PandaTech.BaseConverter&version=4.0.2 // Install PandaTech.BaseConverter as a Cake Tool #tool nuget:?package=PandaTech.BaseConverter&version=4.0.2
Pandatech.BaseConverter
Pandatech.BaseConverter
is a powerful library designed for seamless base conversion between base 10 and base 36
numeral systems. It addresses the common need in software development to obfuscate database primary keys stored as long
integers by converting them into base 36 encoded strings, enhancing data confidentiality and avoiding direct exposure of
entity IDs.
Features
- Bidirectional Conversion: Convert numbers between base 10 and base 36.
- Custom Character Set: Configure the base 36 character set for tailored encoding.
- DTO Integration: Simplify base 36 string usage in Data Transfer Objects (DTOs).
- Batch Conversion: Convert lists of numbers between base 10 and base 36 efficiently.
- Swagger Support: Integrate with Swagger for API documentation and testing.
- Validation: Ensure data integrity with robust validation for base 36 inputs.
- Error Handling: Clear and informative exceptions for invalid inputs.
Installation
Add Pandatech.BaseConverter
to your project via NuGet:
Install-Package Pandatech.BaseConverter
Basic Usage
Converting between Base 10 and Base 36
long number = 12345;
string base36Number = PandaBaseConverter.Base10ToBase36(number);
// Output: "9ix"
string base36Number = "9ix";
long? number = PandaBaseConverter.Base36ToBase10(base36Number);
// Output: 12345
long number = PandaBaseConverter.Base36ToBase10NotNull(base36Number);
// Output: 12345
var numbers = new List<long> { 12345, 67890 };
var base36Numbers = PandaBaseConverter.Base10ListToBase36List(numbers);
// Output: ["9ix", "1bqj"]
var base36Numbers = new List<string> { "9ix", "1bqj" };
var numbers = PandaBaseConverter.Base36ListToBase10List(base36Numbers);
// Output: [12345, 67890]
Advanced Usage
Customizing Base 36 Character Set
Customize the base 36 character set for your needs:
var builder = WebApplication.CreateBuilder(args);
var customCharset = "0123456789abcdefghijklmnopqrstuvwxyz";
builder.Services.ConfigureBaseConverter(customCharset);
DTO Integration
Decorate DTO properties to handle automatic base 36 encoding/decoding:
public class MyDto
{
[PropertyBaseConverter]
public long Id { get; set; }
}
Minimal API Parameter Binding
Use QueryBaseConverter
and PathBaseConverter
to automatically convert base 36 encoded parameters in minimal APIs:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var groupApp = app.MapGroup("minimal");
groupApp.MapGet("query", ([FromQuery] long id) => id)
.QueryBaseConverter("id");
groupApp.MapGet("path/{id}", (long id) => id)
.PathBaseConverter("id");
groupApp.MapPost("parameter", ([AsParameters] Parameter request) => request)
.QueryBaseConverter("Prop1", "Prop2");
app.Run();
public class Parameter
{
public long Prop1 { get; set; }
public long Prop2 { get; set; }
}
Controller Parameter Binding
Resolve base 36 encoded parameters in controller actions using [ParameterBaseConverter]
:
[ApiController]
[Route("api")]
public class ConverterController : ControllerBase
{
[HttpGet("query")]
public IActionResult GetFromQuery([ParameterBaseConverter, FromQuery] long id)
{
return Ok(id);
}
[HttpGet("path/{id}")]
public IActionResult GetFromPath([ParameterBaseConverter] long id)
{
return Ok(id);
}
}
Swagger Integration
Integrate with Swagger for enhanced API documentation:
builder.Services.AddSwaggerGen(
options =>
{
options.AddBaseConverterFilters();
}
);
Error Handling
Pandatech.BaseConverter
provides informative exceptions for handling various errors. Here's how you can manage them:
Supported Exceptions
UnsupportedCharacterException
: Thrown when the input contains characters not supported by the current base 36 character set.
try
{
var result = PandaBaseConverter.Base36ToBase10("invalid-input");
}
catch (UnsupportedCharacterException ex)
{
Console.WriteLine(ex.FullMessage);
// Output: "Message: Input contains unsupported characters. with Value: invalid-input"
}
InputValidationException
: Thrown when input validation fails, such as a negative number for base 10 to base 36 conversion.
try
{
var base36Number = PandaBaseConverter.Base10ToBase36(-12345);
}
catch (InputValidationException ex)
{
Console.WriteLine(ex.FullMessage);
// Output: "Message: Number must be non-negative. with Value: -12345"
}
BaseConverterException
: The base exception class for all custom exceptions in this library, providing aFullMessage
property that includes both the message and the associated value.
try
{
// Some operation that might fail
}
catch (BaseConverterException ex)
{
Console.WriteLine(ex.FullMessage);
}
These exceptions ensure that your applications can gracefully handle and debug errors related to base conversions.
Contributing
Contributions are welcome! If you have suggestions for improvements or encounter any issues, please feel free to open an issue or submit a pull request.
License
This project is licensed under the MIT License. Feel free to use, modify, and distribute it as per the license terms.
Thank you for choosing Pandatech.BaseConverter
for your base conversion needs!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Microsoft.AspNetCore.OpenApi (>= 8.0.6)
- Swashbuckle.AspNetCore.SwaggerGen (>= 6.6.2)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on PandaTech.BaseConverter:
Package | Downloads |
---|---|
PandaTech.IEnumerableFilters
This NuGet helps with filtering tables. |
|
Pandatech.EFCoreQueryMagic
Unlock the full potential of your Entity Framework Core applications with Pandatech.EFCoreQueryMagic. This innovative package empowers developers to seamlessly create dynamic, complex queries and filters for SQL tables without diving deep into the intricacies of LINQ or manual query construction. Designed to enhance productivity and maintainability, EFCoreQueryMagic automates the translation of front-end filter requests into optimized, ready-to-execute EF Core queries. Embrace the magic of streamlined data retrieval and manipulation, and elevate your applications to new heights of efficiency and performance. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
4.0.3 | 464 | 6/21/2024 |
4.0.2 | 116 | 6/20/2024 |
4.0.1 | 117 | 6/19/2024 |
4.0.0 | 122 | 6/18/2024 |
3.0.6 | 418 | 5/8/2024 |
3.0.5 | 152 | 5/6/2024 |
3.0.4 | 128 | 4/25/2024 |
3.0.3 | 506 | 3/20/2024 |
3.0.1 | 172 | 3/13/2024 |
3.0.0 | 120 | 3/13/2024 |
2.0.2 | 490 | 12/2/2023 |
2.0.1 | 132 | 11/29/2023 |
1.0.18 | 219 | 11/8/2023 |
1.0.17 | 117 | 11/8/2023 |
1.0.16 | 261 | 11/7/2023 |
1.0.15 | 102 | 11/6/2023 |
1.0.14 | 108 | 11/6/2023 |
1.0.12 | 321 | 10/23/2023 |
1.0.11 | 205 | 7/19/2023 |
1.0.10 | 162 | 7/18/2023 |
1.0.9 | 143 | 7/18/2023 |
1.0.8 | 146 | 7/17/2023 |
1.0.7 | 185 | 6/7/2023 |
1.0.6 | 159 | 6/7/2023 |
1.0.5 | 164 | 6/6/2023 |
1.0.4 | 144 | 6/6/2023 |
1.0.1 | 164 | 5/30/2023 |
1.0.0 | 219 | 4/13/2023 |
Uppercase set to default base36 chars