Unit Service
Overview
IUnitService is the registry of measurement units used across the application. It lets the app store every numeric value in the International System of Units (SI) internally, while displaying and parsing it in whatever unit the user prefers (meters vs. nautical miles, °C vs. K, and so on).
The service is built from two abstractions:
IUnit— a physical quantity (for example Distance, Temperature, Frequency). It exposes the set of available units for that quantity, the SI unit, and the user's currently selected unit.IUnitItem— a concrete unit of measure within a quantity (for example meter or nautical mile). It knows how to convert to and from SI, and how to parse, validate and format values.
Concept
Each IUnit groups several IUnitItems. One of them is the SI unit (InternationalSystemUnit), and one is the user's current choice (CurrentUnitItem, persisted per quantity). Values flow through the service in SI and are converted only at the edges — when shown to or entered by the user:
Many quantities also ship convenience extension methods, so common lookups stay short:
Built-in Quantities
The default registration covers a wide range of quantities, including Altitude, Angle, Bearing, Distance, Velocity, Frequency, Temperature, Voltage, Amperage, Power, Capacity, Data Rate, Data Size, Latitude, Longitude, Time Span and more.
Defining a Custom Quantity
A quantity is a class deriving from UnitBase<TConfig>; its units derive from UnitItemBase. UnitItemBase accepts a multiplier that converts an SI value to the displayed unit. FromSi multiplies by this value, while ToSi divides by it. The SI unit uses 1.0:
Registration
The service and the built-in quantities are registered by the core services, so in a normal app you do not register them yourself. A module that adds its own quantity registers it directly through the units builder — the service and the defaults are already in place. Items are keyed by the quantity id, so a unit resolves only the items that belong to it:
If you compose the service registration yourself, pass a configure delegate to RegisterUnitService and call RegisterDefault() inside it to keep the built-in quantities:
API
IUnitService
Represents a registry of all registered measurement quantities.
Property | Type | Description |
|---|---|---|
|
| All registered quantities, keyed by their |
Indexer | Type | Description |
|---|---|---|
|
| Resolves a quantity by id, or |
|
| Resolves a specific unit item within a quantity. |
Method | Return Type | Description |
|---|---|---|
|
| Resolves a quantity and casts it to |
IUnitService.this[string unit]
Parameter | Type | Description |
|---|---|---|
|
| The quantity identifier. |
IUnitService.this[string unit, string item]
Parameter | Type | Description |
|---|---|---|
|
| The quantity identifier. |
|
| The unit item identifier. |
IUnitService.GetRequiredUnitOfType<TUnit>
Parameter | Type | Description |
|---|---|---|
|
| The quantity identifier. |
IUnit
Represents a physical quantity that groups several units of measure.
Property | Type | Description |
|---|---|---|
|
| Display name of the quantity. |
|
| Description of the quantity. |
|
| Unique identifier of the quantity. |
|
| Units of measure available for this quantity. |
|
| The user's currently selected and persisted unit of measure. |
|
| The SI unit for this quantity. |
|
| Icon associated with the quantity. |
Indexer | Type | Description |
|---|---|---|
|
| Resolves an item, falling back to the SI unit. |
IUnit.this[string unitItemId]
Parameter | Type | Description |
|---|---|---|
|
| The unit item identifier. |
IUnitItem
Represents a concrete unit of measure that handles conversion, parsing, validation and formatting.
Property | Type | Description |
|---|---|---|
|
| Unique identifier of the unit item. |
|
| Display name of the unit item. |
|
| Description of the unit item. |
|
| Symbol appended to formatted values. |
|
| Whether this item is the SI unit of its quantity. |
Method | Return Type | Description |
|---|---|---|
|
| Determines whether text is a valid value in this unit. |
|
| Validates text as a value in this unit. |
|
| Parses text as a value in this unit. |
|
| Parses text as a value in this unit and converts it to SI. |
|
| Formats a value expressed in this unit. |
|
| Converts an SI value to this unit and formats it. |
|
| Formats a value and appends its unit symbol. |
|
| Converts and formats an SI value, then appends its unit symbol. |
|
| Converts a value from SI to this unit. |
|
| Converts a value from this unit to SI. |
IUnitItem.IsValid
Parameter | Type | Description |
|---|---|---|
|
| The text to validate. |
IUnitItem.ValidateValue
Parameter | Type | Description |
|---|---|---|
|
| The text to validate. |
IUnitItem.Parse
Parameter | Type | Description |
|---|---|---|
|
| The text to parse. |
IUnitItem.ParseToSi
Parameter | Type | Description |
|---|---|---|
|
| The text to parse. |
IUnitItem.Print
Parameter | Type | Description |
|---|---|---|
|
| The value expressed in this unit. |
|
| The numeric format string, or |
IUnitItem.PrintFromSi
Parameter | Type | Description |
|---|---|---|
|
| The value expressed in SI. |
|
| The numeric format string, or |
IUnitItem.PrintWithUnits
Parameter | Type | Description |
|---|---|---|
|
| The value expressed in this unit. |
|
| The numeric format string, or |
IUnitItem.PrintFromSiWithUnits
Parameter | Type | Description |
|---|---|---|
|
| The value expressed in SI. |
|
| The numeric format string, or |
IUnitItem.FromSi
Parameter | Type | Description |
|---|---|---|
|
| The value expressed in SI. |
IUnitItem.ToSi
Parameter | Type | Description |
|---|---|---|
|
| The value expressed in this unit. |