With 6.0.0

Extensions to make c# easier to use when doing functional code. Copy update expressions. Helper extensions.

There is a newer prerelease version of this package available.
See the version list below for details.
Install-Package With -Version 6.0.0
dotnet add package With --version 6.0.0
<PackageReference Include="With" Version="6.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add With --version 6.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: With, 6.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install With as a Cake Addin
#addin nuget:?package=With&version=6.0.0

// Install With as a Cake Tool
#tool nuget:?package=With&version=6.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

with

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#).

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"}) );
        // ...
    }
}

Why shouldn't you use this library

The immutable data support in this library is done as an extensions to the language using the expression support in c#. A different way to add these things to c# would be to write some sort of roslyn extension in order to extend the language in a way that can be generated at compile time. This is done for instance in language ext codegen project. An approach such as that could be useful depending on your requirements.

On the .net platform there is already a language that allows you to write immutable first code in a terse and helpful way, f#, you can find out more on: f# for fun and profit. Many programmers prefer to work in c#/java why this library or codegen makes more sense.

with

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#).

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"}) );
        // ...
    }
}

Why shouldn't you use this library

The immutable data support in this library is done as an extensions to the language using the expression support in c#. A different way to add these things to c# would be to write some sort of roslyn extension in order to extend the language in a way that can be generated at compile time. This is done for instance in language ext codegen project. An approach such as that could be useful depending on your requirements.

On the .net platform there is already a language that allows you to write immutable first code in a terse and helpful way, f#, you can find out more on: f# for fun and profit. Many programmers prefer to work in c#/java why this library or codegen makes more sense.

Release Notes

Trimmed down to what should be essential.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
6.1.0-beta-1 261 3/12/2020
6.0.0 446 12/25/2019
6.0.0-alpha-1 259 12/22/2019
5.1.4 329 12/22/2019
5.1.3 316 12/15/2019
5.1.2 451 11/22/2019
5.1.1 249 11/17/2019
5.1.0 222 11/16/2019
5.1.0-alpha-2 204 11/15/2019
5.1.0-alpha-1 232 11/13/2019
5.0.2 858 8/23/2018
5.0.1 505 8/23/2018
5.0.0 498 8/21/2018
4.3.1 860 4/11/2018
4.3.0 791 11/8/2017
4.2.0 514 11/2/2017
4.1.2 549 11/1/2017
4.1.1 556 10/3/2017
4.1.0 536 10/3/2017
4.0.0 538 9/18/2017
3.0.0 886 9/29/2016
2.0.0 781 6/11/2016
1.0.8 1,117 2/1/2016
1.0.7 703 10/12/2015
1.0.6 778 9/26/2015
1.0.5 652 9/25/2015
1.0.3 713 9/25/2015
1.0.2 672 9/24/2015
1.0.1 688 9/24/2015
1.0.0 739 9/18/2015
0.4.19 688 9/9/2015
0.4.18 697 8/19/2015
0.4.17 703 6/27/2015
0.4.16 795 3/22/2015
0.4.15 650 3/22/2015
0.4.14 666 3/21/2015
0.4.13 646 3/20/2015
0.4.12 641 3/20/2015
0.4.11 620 3/18/2015
0.4.10 641 3/18/2015
0.4.9 627 3/18/2015
0.4.8 663 3/12/2015
0.4.7 655 3/11/2015
0.4.6 693 3/11/2015
0.4.5 662 3/11/2015
0.4.4 646 3/10/2015
0.4.3 773 1/6/2015
0.4.2 1,028 12/19/2014
0.4.1 683 12/18/2014
0.4.0 682 12/18/2014
0.3.0 664 12/8/2014
0.2.0 1,028 11/16/2014
0.1.1 1,395 11/8/2014
0.1.0 1,099 11/6/2014
0.0.6 1,140 11/5/2014
0.0.5 912 11/3/2014
0.0.4 1,462 11/2/2014
0.0.3 792 10/20/2013
0.0.2 746 10/18/2013
0.0.1 669 10/17/2013