Oneiro.NewType.Core
9.1.2
dotnet add package Oneiro.NewType.Core --version 9.1.2
NuGet\Install-Package Oneiro.NewType.Core -Version 9.1.2
<PackageReference Include="Oneiro.NewType.Core" Version="9.1.2" />
<PackageVersion Include="Oneiro.NewType.Core" Version="9.1.2" />
<PackageReference Include="Oneiro.NewType.Core" />
paket add Oneiro.NewType.Core --version 9.1.2
#r "nuget: Oneiro.NewType.Core, 9.1.2"
#:package Oneiro.NewType.Core@9.1.2
#addin nuget:?package=Oneiro.NewType.Core&version=9.1.2
#tool nuget:?package=Oneiro.NewType.Core&version=9.1.2
NewType Core
A new type for primitives.
Oneiro.NewType.Core is a library that provides a way to create strongly-typed wrappers around primitive types. This can be useful for adding type safety and domain-specific logic to your code.
Features
- Strongly-typed wrappers around primitive types.
- Custom validation rules.
- Type conversion support.
- Use of
NewTypefor aspnet core query parameters.
Installation
To install NewType.Core, add the following package to your project:
dotnet add package Oneiro.NewType.Core
Usage
Creating a New Type
To create a new type, inherit from the NewType<T, TNewType> class, where T
is the underlying type and TNewType is the new type you are creating.
It is recommended to make your new type class sealed to prevent inheritance.
public sealed record WrappedInt : NewType<int, WrappedInt> {
private WrappedInt(int value) : base(value) { }
}
If using the dotnet standard version, you can use the NewType class instead of the record keyword.
public sealed class WrappedInt : NewType<int, WrappedInt> {
private WrappedInt(int value) : base(value) { }
}
Using the New Type
You can create instances of your new type using the From method:
var wrappedInt = WrappedInt.From(42);
You can also use the Default property to get the default value of your new type:
var defaultWrappedInt = WrappedInt.Default;
For aspnet core, you can use the NewType in query parameters
for both controller based and minimal APIs.
[HttpGet]
public IActionResult Get([FromQuery] WrappedInt wrappedInt) {
return Ok(wrappedInt.GetValue());
}
The functionality is provided by the use of TryParse and TypeConverter.
Validation
You can add custom validation rules to your new type by inheriting from ValidatedNewType<T, TNewType>:
public sealed class ValidatedInt : ValidatedNewType<int, ValidatedInt> {
public ValidatedInt(int value) : base(value) {
RuleFor("Positive", x => x > 0, "Value must be positive");
}
}
Type Conversion
To enable type conversion, use the NewTypeConverter class:
[TypeConverter(typeof(NewTypeConverter<int, WrappedInt>))]
public sealed class WrappedInt : NewType<int, WrappedInt> {
public WrappedInt(int value) : base(value) { }
}
Examples
Creating and Using a New Type
var wrappedInt = WrappedInt.From(42);
Console.WriteLine(wrappedInt.GetValue()); // Output: 42
Validation
var validatedInt = ValidatedInt.From(-1);
if (validatedInt.HasErrors()) {
var errors = validatedInt.GetErrors();
foreach (var error in errors) {
Console.WriteLine($"{error.Name}: {error.ErrorMessage}");
}
}
Optionally, you can throw errors caused during the type creation by calling validatedInt.ThrowIfErrors().
validatedInt.ThrowIfErrors();
Type Conversion
Explicit casting is support for converting between the new type and the underlying type.
The optional use of the provided TypeConverter class allows for conversions.
var converter = TypeDescriptor.GetConverter(typeof(WrappedInt));
var wrappedInt = (WrappedInt)converter.ConvertFrom(42);
var intValue = (int)converter.ConvertTo(wrappedInt, typeof(int));
License
This project is licensed under the MIT License. See the LICENSE file for more details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Memory (>= 4.6.0)
-
net6.0
- No dependencies.
NuGet packages (3)
Showing the top 3 NuGet packages that depend on Oneiro.NewType.Core:
| Package | Downloads |
|---|---|
|
Oneiro.NewType.Newtonsoft
Easily serialize and deserialize types in Oneiro.NewTypes.Core using JSON.NET |
|
|
Oneiro.NewType.Json
Easily serialize and deserialize types in Oneiro.NewTypes.Core using System.Text.Json |
|
|
Oneiro.NewType.FluentValidation
Entensions to support validation of new types using fluent validations |
GitHub repositories
This package is not used by any popular GitHub repositories.