RecordShims 0.1.1

dotnet add package RecordShims --version 0.1.1                
NuGet\Install-Package RecordShims -Version 0.1.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RecordShims" Version="0.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RecordShims --version 0.1.1                
#r "nuget: RecordShims, 0.1.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install RecordShims as a Cake Addin
#addin nuget:?package=RecordShims&version=0.1.1

// Install RecordShims as a Cake Tool
#tool nuget:?package=RecordShims&version=0.1.1                

Record Shims

What are records?

Records are immutable classes that hold PODS(Plain old data structures). They represent set of data fields at a given time. Immutability ensures that a record is reliable while in a multi-threaded environment. Records should not involve any complex business logic beyond data constraints. IE: don't hold a database connection in a record.

Why use Record Shims

Immutability in C# typically requires to a lot of boilerplate to get a new record with one or more changed. Furthermore, this usually leads to brittle functions with default arguments or avoiding immutability entirely and asking developers to be good citizens with the public setters.

This library aims to reduce that boilerplate for older projects that cannot upgrade to C# 8.0 when it is released and for any current projects that don't want to wait for C# 8.0 to be released.

This is done using Reflection and as such "high performance" is not the goal of this project. This library does aim to focus on:

  • Increase developer productivity due to reduction of boiler plate
  • Maintain a high degree of Readability
  • Encourage Code Maintainability with static type safety APIs and constraint validation.
  • Limit namespace pollution with extension methods

Note: If a non-invasive performance improvement can be made please make the suggestion or make a pull request.

Examples

void ExampleFunc()
{
    var original = new RecordExample("Record 1", 0);

    // With function is similar to C# 8.0 proposal syntax
    var newRecord = original.With(copy => copy
        .Mutate(r => r.Name, "Record 2")
        .AndMutate(r => r.Upvotes, r => r.UpVotes + 1));

    // changeset API provides a more typical fluent API chain.
    var anotherRecord = original.StartChangeSet()
        .Mutate(r => r.Name, "Record 3")
        .AndMutate(r => r.Upvotes, r => r.UpVotes + 1)
        .ToNewRecord(original);

    // using nameof bypasses expression walk performance hit but
    // incurs static type safety problem that may hinder maintainability.
    var yetAnotherRecord = original.StartChangeSet()
        .Mutate(nameof(original.Name), "Record 4")
        .AndMutate(nameof(original.Upvotes), 9001)
        .ToNewRecord(original);
}

class RecordExample : RecordBase<RecordExample> // Recordbase<T> implements IRecord<T>
{
    public string Name { get; }

    public int UpVotes { get; private set; }

    public RecordExample(string name, int upvotes)
    {
        Name = name;
        UpVotes = upvotes;
        ValidateNonVirtual(this);
    }

    public override void ThrowIfConstraintsAreViolated(RecordExample record)
    {
        ValidateNonVirtual(record);
    }

    private static ValidateNonVirtual(RecordExample record)
    {
        if(string.IsNullOrEmpty(record.Name))
        {
            throw new ArgumentNullException(nameof(Name));
        }

        if(record.UpVotes < 0)
        {
            throw new ArgumentOutOfRangeException("Must be non negative.");
        }
    }
}

How it Works

Please refer to the IRecord<TRecord> interface. Types that implement that interface are elligible for the extensions that take care of the API displayed above.

  1. A change set is built with the Mutate functions.
  2. if there are changes in the set, then a shallow copy of the original record is made
  3. The changeset is applied to the copy
  4. The copy is then validated against its constraints.
  5. The copy is returned.

Concerns

This library intentionally violates the accessibility rules of IRecord<T> types. Private setters and readonly fields are modifiable. Normally, this would be a huge alarm in a code base. The API of this library is meant to limit the scope of this violation so that it is useful but not dangerous.

Building and Testing

The solution should be buildable in VS 2017 and 2019 Community. They're .Net core projects. Visual studio code can even build and test them.

To get coverage I ran the following command:

dotnet test /p:CollectCoverage=true /p:Exclude=[NUnit3.TestAdapter]\* /p:CoverletOutputFormat=\"json,opencover,lcov\" /p:CoverletOutput='./.coverage/'

This should generate a json, lcov, and opencover file for various coverage displays. I prefer Axocover but it does not support .Net Core yet. I ended up using the following VS code extensions.

To display code with Coverage gutters I had to chagne a setting to look for coverage.info as well as the default lcov.info.

    "coverage-gutters.coverageFileNames": [
        "lcov.info",
        "coverage.info",
        "cov.xml",
        "jacoco.xml"
      ]

The test explorer settings have test arugment setting that can be set. I wish it was per repo.

"dotnet-test-explorer.testArguments": "/p:CollectCoverage=true /p:Exclude=[NUnit3.TestAdapter]\\* /p:CoverletOutputFormat=\\\"json,opencover,lcov\\\" /p:CoverletOutput=\\\"./.coverage/\\\""
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 is compatible.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.5

    • No dependencies.
  • .NETFramework 4.6

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.1 647 9/7/2019
0.1.0 507 8/2/2019

Initial release. Expect some bugs and breaking changes to come.