Skip to content

Commit d519cd1

Browse files
committed
Improve UnitsNet messaging and Modular discoverability
1 parent 372daea commit d519cd1

4 files changed

Lines changed: 41 additions & 22 deletions

File tree

Docs/extending-with-custom-units.md

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
# Extending with Custom Units
22

3+
## Recommended prototype: UnitsNet.Modular
4+
5+
[UnitsNet.Modular](../UnitsNet.Modular/README.md) is our prototype for a better way to add
6+
application-specific quantities and units. It is a proof of concept and currently in pre-release,
7+
but it generates strongly typed quantity structs, unit enums, conversions, parsing, formatting, and
8+
metadata from your JSON definitions at compile time.
9+
10+
Start with [Add custom quantities](../UnitsNet.Modular/README.md#add-custom-quantities), or explore the
11+
[UnitsNet.Modular samples in GitHub Codespaces](https://codespaces.new/angularsen/UnitsNet?devcontainer_path=.devcontainer%2Funitsnet-modular%2Fdevcontainer.json&quickstart=1).
12+
13+
`UnitsNet` and `UnitsNet.Modular` are alternative implementations and cannot be referenced together
14+
in the same consumer project. If you need to keep using the established `UnitsNet` package, the
15+
runtime approach below remains available.
16+
17+
## Secondary approach: runtime custom quantities in UnitsNet
18+
319
This article is for when you want to add your own custom quantities and units at runtime, not included in the UnitsNet nuget.
420

521
To add new quantities or units to the `UnitsNet` nuget, please see [Adding a New Unit](adding-a-new-unit.md).
622

7-
## Disclaimer: This is highly experimental and incomplete
23+
### Disclaimer: This is highly experimental and incomplete
824

925
You miss out on the statically generated code for members like `Length.FromMeters(1)` and `myLength.Meters`.
1026
Conversion methods like `myLength.As()` and `myLength.ToUnit()` currently only support their respective unit enums, in this case `LengthUnit`.
1127

12-
## Can I add a custom unit to an existing quantity in UnitsNet?
28+
### Can I add a custom unit to an existing quantity in UnitsNet?
1329

1430
Currently, no.
1531

@@ -21,14 +37,14 @@ Since UnitsNet is so statically typed, your options are limited to:
2137
1. Submit a pull request to [add a new unit](adding-a-new-unit.md) to the UnitsNet nuget
2238
2. Build your own custom version of UnitsNet
2339

24-
## Why add a custom quantity?
40+
### Why add a custom quantity?
2541

2642
Good question.
2743

2844
In its current state, the support for custom quantities and units is limited and provides limited integration with the existing units and code.
2945
We consider it exploratory, to see what is possible, and we welcome ideas on how it can be improved.
3046

31-
### Key benefits
47+
#### Key benefits
3248

3349
- Reuse functionality that operates on `IQuantity`
3450
- Dynamically convert to unit with `.As(Enum)`
@@ -38,14 +54,14 @@ We consider it exploratory, to see what is possible, and we welcome ideas on how
3854
- Also allows you to dynamically convert between your custom units and the built-in units, such as `CustomLengthUnit.ElbowToThumb` to `LengthUnit.Meter`.
3955
- Reuse `QuantityParser` and `UnitParser` to parse quantity strings like "5 cm" and "cm" for your own quantities and units
4056

41-
### What could be better
57+
#### What could be better
4258

4359
- Source generators via nuget, if possible [Using source generators #902](https://github.com/angularsen/UnitsNet/issues/902)
4460
- String-based lookup instead of enum-based for quantity methods like `As()` and `ToUnit()`, required for [XP One nuget per quantity #1181](https://github.com/angularsen/UnitsNet/pull/1181)
4561

4662
Got more ideas? Create a discussion or issue.
4763

48-
## Units.NET structure
64+
### Units.NET structure
4965

5066
Units.NET roughly consists of these parts:
5167
* Quantities like `Length` and `Force`
@@ -54,9 +70,9 @@ Units.NET roughly consists of these parts:
5470
* [JSON files for defining units, conversion functions and abbreviations](quantity-and-unit-definition-schema.md)
5571
* `CodeGen` console app to generate C# code based on JSON files
5672

57-
## Example: Custom quantity `HowMuch` with units `HowMuchUnit`
73+
### Example: Custom quantity `HowMuch` with units `HowMuchUnit`
5874

59-
### Sample output
75+
#### Sample output
6076
```
6177
GetDefaultAbbreviation(): sm, lts, tns
6278
Parse<HowMuchUnit>(): Some, Lots, Tons
@@ -67,15 +83,15 @@ Convert 10 tons to:
6783
10 tns
6884
```
6985

70-
### Map unit enum values to unit abbreviations
86+
#### Map unit enum values to unit abbreviations
7187

7288
```c#
7389
UnitAbbreviationsCache.Default.MapUnitToDefaultAbbreviation(HowMuchUnit.Some, "sm");
7490
UnitAbbreviationsCache.Default.MapUnitToDefaultAbbreviation(HowMuchUnit.Lots, "lts");
7591
UnitAbbreviationsCache.Default.MapUnitToDefaultAbbreviation(HowMuchUnit.Tons, "tns");
7692
```
7793

78-
### Lookup unit abbreviations from enum values
94+
#### Lookup unit abbreviations from enum values
7995

8096
```c#
8197
Console.WriteLine("GetDefaultAbbreviation(): " + string.Join(", ",
@@ -85,7 +101,7 @@ Console.WriteLine("GetDefaultAbbreviation(): " + string.Join(", ",
85101
));
86102
```
87103

88-
### Parse unit abbreviations back to enum values
104+
#### Parse unit abbreviations back to enum values
89105

90106
```c#
91107
Console.WriteLine("Parse<HowMuchUnit>(): " + string.Join(", ",
@@ -95,7 +111,7 @@ Console.WriteLine("Parse<HowMuchUnit>(): " + string.Join(", ",
95111
));
96112
```
97113

98-
### Convert between units of custom quantity
114+
#### Convert between units of custom quantity
99115

100116
```c#
101117
var unitConverter = UnitConverter.Default;
@@ -112,7 +128,7 @@ Console.WriteLine(Convert(HowMuchUnit.Lots)); // 100 lts
112128
Console.WriteLine(Convert(HowMuchUnit.Tons)); // 10 tns
113129
```
114130

115-
### Sample quantity
131+
#### Sample quantity
116132

117133
See the sample implementation in the test suite:
118134
- [HowMuchUnit.cs](https://github.com/angularsen/UnitsNet/blob/master/UnitsNet.Tests/CustomQuantities/HowMuchUnit.cs)

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
## Units.NET
88

9-
Add strongly typed quantities to your code and get merrily on with your life.
9+
Add strongly typed quantities and units to your code and get merrily on with your life.
1010

11-
No more magic constants found on Stack Overflow, no more second-guessing the unit of parameters and variables.
11+
No more magic constants found online or guessing the unit of variables.
1212

1313
### Changes
1414

@@ -18,6 +18,8 @@ New units will be backported to `maintenance/v5` until v6 becomes stable.
1818
[Upgrading from 5.x to 6.x](https://github.com/angularsen/UnitsNet/wiki/Upgrading-from-5.x-to-6.x)<br>
1919
[Upgrading from 4.x to 5.x](https://github.com/angularsen/UnitsNet/wiki/Upgrading-from-4.x-to-5.x)<br>
2020

21+
🧪 **Experimental:** Check out [UnitsNet.Modular](UnitsNet.Modular/README.md), which generates only the quantities and units your application needs.
22+
2123
### Overview
2224

2325
* [Overview](#overview)

UnitsNet.Modular/README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# UnitsNet Modular
22

3+
[![Open the samples in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/angularsen/UnitsNet?devcontainer_path=.devcontainer%2Funitsnet-modular%2Fdevcontainer.json&quickstart=1)
4+
35
[![UnitsNet.Modular CI](https://github.com/angularsen/UnitsNet/actions/workflows/unitsnet-modular-ci.yml/badge.svg)](https://github.com/angularsen/UnitsNet/actions/workflows/unitsnet-modular-ci.yml)
4-
[![Open the playground in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/angularsen/UnitsNet?devcontainer_path=.devcontainer%2Funitsnet-modular%2Fdevcontainer.json&quickstart=1)
56

67
Generate only the strongly typed quantities and units your application needs.
78

@@ -23,13 +24,13 @@ the complete catalog:
2324
- immutable runtime discovery and System.Text.Json support;
2425
- trimming and Native AOT-friendly generated code.
2526

26-
> **Experimental:** UnitsNet.Modular is an alpha proof of concept. Its API, package structure, and
27-
> compatibility guarantees may change as the architecture is evaluated.
27+
> **Experimental:** UnitsNet.Modular is a proof of concept and currently in pre-release. Its API,
28+
> package structure, and compatibility guarantees may change as the architecture is evaluated.
2829
2930
Want to try it without installing anything? Open the browser-based
30-
[UnitsNet.Modular playground](https://codespaces.new/angularsen/UnitsNet?devcontainer_path=.devcontainer%2Funitsnet-modular%2Fdevcontainer.json&quickstart=1).
31-
It builds a focused sample with the real source generator, opens the quantity selection and custom
32-
definition files, and keeps generated C# available for inspection.
31+
[UnitsNet.Modular samples](https://codespaces.new/angularsen/UnitsNet?devcontainer_path=.devcontainer%2Funitsnet-modular%2Fdevcontainer.json&quickstart=1).
32+
The Codespace builds all samples with the real source generator, opens the sample documentation and
33+
source files, and keeps generated C# available for inspection.
3334

3435
## Contents
3536

UnitsNet/UnitsNet.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Version>6.0.0-pre021</Version>
66
<Authors>Andreas Gullberg Larsen</Authors>
77
<Title>Units.NET</Title>
8-
<Description>Get all the common units of measurement and the conversions between them. It is light-weight and thoroughly tested.</Description>
8+
<Description>Add strongly typed quantities and units to your code and get merrily on with your life. No more magic constants found online or guessing the unit of variables.</Description>
99
<Copyright>Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com).</Copyright>
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1111
<RepositoryUrl>https://github.com/angularsen/UnitsNet</RepositoryUrl>

0 commit comments

Comments
 (0)