Skip to content

Extensions and classes to make immutable c# easier to use. Obsolete with introduction of records in C# 9.

License

Notifications You must be signed in to change notification settings

wallymathieu/with

Repository files navigation

with Build Status Build status NuGet

With is a small library written in c# intended for alternative constructions in c# to do things that may look clumsy in regular code.

Why is this library small? Parts of the library has been removed as c# has evolved (and my understanding of what can be useful in c#).

The main reason for this library has been met by C# 9. You probably want to use that instead of using this library.

What can we learn from "With"

Having access to expressions can help with doing extensions to a language in a relatively simple way.

Examples

Working with immutable data

If you need to get a copy of a readonly object but with some other value set in the new instance, you can use With. This is very similar to f# copy and update record expression. The main abstraction is called a lens. Lenses answers the question "How do you read and update immutable data". It may help to think about them as properties for immutable data that you can combine and compose. For further reading see the Basic lens operation part of the wiki

Simplest example

using With;
using With.Lenses;
...
public class CustomerNameChangeHandler
{
    // start with initializing the lens expression once (main cost is around parsing expressions)
    private static readonly DataLens<Customer, string> NameLens =
        LensBuilder<Customer>
            .Of(m => m.Name)
            .Build();
    public void Handle()
    {
        // fetch customer, say:
        var customer = new Customer(id:1, name:"Johan Testsson");
        // get a new instance of that customer but with changed name:
        var changedNameToErik = CustomerNameLens.Set(customer, "Erik Testsson");
        // ...
    }
}

Settings several properties at the same time

using System;
using With;
using With.Lenses;
...
public class CustomerChangeHandler
{
    // start with initializing the lens expression once (main cost is around parsing expressions)
    private static readonly DataLens<Customer, (int, string, IEnumerable<string>)> CustomerIdNamePreferencesLens =
        LensBuilder<Customer>
            .Of(m => m.Id)
            .And(m => m.Name)
            .And(m => m.Preferences)
            .Build();
    public void Handle()
    {
        // fetch customer, say:
        var customer = new Customer(id:1, name:"Johan Testsson");
        // get a new instance of that customer but with id, name and preferences changed:
        var change = CustomerIdNamePreferencesLens.Set(customer, (NextId(), "Erik Testsson", new []{"Swedish fish"}) );
        // ...
    }
}

Performance impact of working with immutable types (by using With) in c#

To generate use the Timings project.

BenchmarkDotNet=v0.12.0, OS=Windows 10.0.19033
Intel Core i7-8650U CPU 1.90GHz (Kaby Lake R), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=3.0.100
  [Host]     : .NET Core 2.1.13 (CoreCLR 4.6.28008.01, CoreFX 4.6.28008.01), X64 RyuJIT
  DefaultJob : .NET Core 2.1.13 (CoreCLR 4.6.28008.01, CoreFX 4.6.28008.01), X64 RyuJIT
Method Mean Error StdDev
Using_static_prepered_copy_expression 478.1 ns 9.39 ns 14.06 ns
Hand_written_method_returning_new_instance 457.3 ns 11.15 ns 10.95 ns
Language_ext_generated 476.0 ns 9.42 ns 14.38 ns

As can be seen there is a slight penalty to use the different approaches. The naive hand written version has similar performance why this library might be good enough when you have access to reflection and expression compile on the platform.

The language ext approach has some disadvantages that you might be OK with, for instance that it complicates your build and makes it more dependent on specific build environment (though that could be fixed by contributing to dotnet codegen).

Why shouldn't you use this library

You probably want to use C# 9 instead where the with has been integrated into the language.

Nuget

https://www.nuget.org/packages/With

About

Extensions and classes to make immutable c# easier to use. Obsolete with introduction of records in C# 9.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published