DEV Community

Cover image for Simplifying Unit Conversions in .NET with UniVertAll
Majid Shahabfar
Majid Shahabfar

Posted on • Edited on

Simplifying Unit Conversions in .NET with UniVertAll

Unit conversions in .NET development can often be challenging, particularly when working with various measurement systems. UniVertAll package provides an all-in-one solution, featuring a fluent API and broad unit compatibility to streamline the conversion process. Supporting .NET versions 6 through 9, UniVertAll is built for seamless integration into contemporary .NET applications.

Key Features of UniVertAll:

  • Extensive Unit Support: UniVertAll covers a wide range of units across various categories, including length, mass, volume, power, temperature, pressure, and more. This broad support ensures that developers can handle diverse unit conversion scenarios with ease.
  • Fluent API: The library’s fluent API design promotes readability and ease of use. Developers can perform conversions using intuitive syntax, enhancing code clarity and maintainability.
  • Customizability: UniVertAll allows for the addition of user-defined units and conversion logic. This flexibility is particularly beneficial for domain-specific applications requiring specialized units.
  • Thread Safety: Engineered with concurrency in mind, UniVertAll is suitable for multi-threaded applications, ensuring reliable performance in complex environments.
  • NuGet Availability: The library is readily accessible via the NuGet Package Manager, facilitating easy integration into .NET projects.

Installation:

To incorporate UniVertAll into your project, install it via NuGet:

  • Using .NET CLI:

    dotnet add package UniVertAll

  • Using Package Manager: Search for UniVertAll in the NuGet Package Manager within Visual Studio and proceed with the installation.

Supported Units:

UniVertAll supports a diverse array of units across multiple categories:

Image description

Usage Examples:

UniVertAll’s fluent API enables seamless unit conversions. Below are examples demonstrating its functionality:

  • Length Conversion:
    using Univertall.Units.Length;

    double meters = 5000;
    double yards = meters.ConvertLength()
              .From(LengthUnit.Meter)
              .To(LengthUnit.Yard);
    Console.WriteLine($"{meters} Meters is equal to {yards} Yards.");
Enter fullscreen mode Exit fullscreen mode

Volume Conversion:

    using Univertall.Units.Volume;

    double liters = 3.78541;
    double gallons = liters.ConvertVolume()
              .From(VolumeUnit.Liter)
              .To(VolumeUnit.Gallon);
    Console.WriteLine($"{liters} Liters is equal to {gallons} Gallons.");

Enter fullscreen mode Exit fullscreen mode

Power Conversion:

    using Univertall.Units.Power;

    double watts = 746;
    double horsepower = watts.ConvertPower()
            .From(PowerUnit.Watt)
            .To(PowerUnit.MetricHorsepower);
    Console.WriteLine($"{watts} Watts is equal to {horsepower} Horsepower.");

Enter fullscreen mode Exit fullscreen mode

Extending with Custom Units:

UniVertAll is designed for extensibility, allowing developers to define custom units and conversions by inheriting from the BaseUnitConverter class. This flexibility accommodates domain-specific requirements.

    using Univertall.Base;

    public enum CustomUnit { UnitA, UnitB }

    public class CustomConverter : BaseUnitConverter<CustomUnit>
    {
        protected override double ToBaseUnit(double value, CustomUnit fromUnit)
        {
            return fromUnit switch
            {
                CustomUnit.UnitA => value * 10,  // Example: 1 UnitA = 10 Base Units
                CustomUnit.UnitB => value / 2,   // Example: 1 UnitB = 0.5 Base Units
                _ => throw new ArgumentOutOfRangeException(nameof(fromUnit), fromUnit, null)
            };
        }

        protected override double FromBaseUnit(double value, CustomUnit toUnit)
        {
            return toUnit switch
            {
                CustomUnit.UnitA => value / 10,  // Convert Base Unit to UnitA
                CustomUnit.UnitB => value * 2,   // Convert Base Unit to UnitB
                _ => throw new ArgumentOutOfRangeException(nameof(toUnit), toUnit, null)
            };
        }
    }

    // Usage
    var customConverter = new CustomConverter();
    double result = customConverter.Convert(5, CustomUnit.UnitA, CustomUnit.UnitB);
    Console.WriteLine($"5 UnitA is equal to {result} UnitB.");

Enter fullscreen mode Exit fullscreen mode

Conclusion:

UniVertAll is a robust and versatile unit conversion library tailored for .NET developers. With its extensive unit coverage, user-friendly fluent API, and customizable features, it’s an invaluable resource for applications that demand accurate and efficient unit conversions. Whether you're working on large-scale enterprise projects or personal endeavors, UniVertAll streamlines unit management, boosting your productivity.

Discover more or contribute to its development by visiting the UniVertAll GitHub Repository. The repository includes complete source code, detailed examples, and a wealth of resources to help you get up and running effortlessly.

UniVertAll transforms unit conversion in .NET from a challenge into a seamless experience. Explore its capabilities and discover how it can simplify your projects today!

Top comments (0)