Skip to content

Latest commit

History

55 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

AutoReviser

Build status

C# is a great programming language but does not have any handy way to update immutable objects partially. AutoReviser provides helper extension methods to generate partially updated copies of immutable objects using lambda expressions. You need not to write With...() methods anymore. You just need to tell AutoReviser which properties should be updated to what values using lambda expression.

How to use

There are immutable object classes.

publicclassImmutableObject{publicImmutableObject(intalfa,stringbravo)=>(Alfa,Bravo)=(alfa,bravo);publicintAlfa{get;}publicstringBravo{get;}}publicclassComplexImmutableObject{publicComplexImmutableObject(intcharile,ImmutableObjectdelta)=>(Charlie,Delta)=(charlie,delta);publicintCharlie{get;}publicImmutableObjectDelta{get;}}

Traditional way to update an immutable object partially in C#

publicstaticclassImmutableObjectExtensins{// I'm tired of writing these 'With...' methods.publicstaticImmutableObjectWithAlfa(thisImmutableObjectsource,intalfa){returnnewImmutableObject(alfa,source.Bravo);}publicstaticImmutableObjectWithBravo(thisImmutableObjectsource,stringbravo){returnnewImmutableObject(source.Alfa,bravo);}publicstaticComplexImmutableObjectWithCharlie(thisComplexImmutableObjectsource,intcharlie){returnnewComplexImmutableObject(charlie,source.Delta);}publicstaticComplexImmutableObjectWithDelta(thisComplexImmutableObjectsource,ImmutableObjectdelta){returnnewComplexImmutableObject(source.Charlie,delta);}}varsource=newComplexImmutableObject(1,newImmutableObject(2,"foo"));varrevision=source.WithCharlie(10).WithDelta(source.Delta.WithBravo("foo"));

Using AutoReviser

usingAutoReviser;varsource=newComplexImmutableObject(1,newImmutableObject(2,"foo"));varrevision=source.Revise(
x =>x.Charlie==10&&x.Delta.Bravo=="foo");

Immutable Collections

AutoReviser supports System.Collections.Immutable.ImmutableArray<T> struct and System.Collections.Immutable.ImmutableDictionary<TKey, TValue> class.

ImmutableArray

usingAutoReviser;usingSystem.Collections.Immutable;publicclassHasImmutableArray{publicHasImmutableArray(ImmutableArray<string>alfa)=>Alfa=alfa;publicImmutableArray<string>Alfa{get;}}varsource=newHasImmutableArray(alfa:ImmutableArray.Create("foo","bar"));varrevision=source.Revise(
x =>x.Alfa[0]=="baz"&&x.Alfa[1]=="foz");

ImmutableDictionary

usingAutoReviser;usingSystem.Collections.Immutable;publicclassHasImmutableDictionary{publicHasImmutableDictionary(ImmutableDictionary<int,string>alfa)=>Alfa=alfa;publicImmutableDictionary<int,string>Alfa{get;}}varsource=newHasImmutableArray(alfa:ImmutableDictionary<int,string>.Empty);varrevision=source.Revise(
x =>x.Alfa[0]=="baz"&&x.Alfa[1]=="foz");

Install package

PM> Install-Package AutoReviser

License

MIT License
Copyright (c) 2019 Gyuwon Yi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

About

Provides convenient utilities to generate partially updated copies of immutable objects using lambda expressions in C#.

Topics

Resources

Stars

19 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages