xaviergonz / t4immutable Goto Github PK
View Code? Open in Web Editor NEWT4Immutable is a T4 template for C# .NET apps that generates code for immutable classes.
License: Apache License 2.0
T4Immutable is a T4 template for C# .NET apps that generates code for immutable classes.
License: Apache License 2.0
I came across your repo while searching for related topic recently. I created a thread on CoreFx asking about developing your own immutable class APIs, but with also a parent/child relationship. Meaning, I could have an object with children and those children could have children, etc.. However, I need the ability to include/update the respective parent (and ancestors) when updating the properties of the child(ren).
Here is the CoreFx issue: https://github.com/dotnet/corefx/issues/18357
Any help/examples is appreciated.
With the following declaration:
[ImmutableClass(Options = ImmutableClassOptions.IncludeOperatorEquals | ImmutableClassOptions.ExcludeToString | ImmutableClassOptions.ExcludeWith, ConstructorAccessLevel = ConstructorAccessLevel.Internal, BuilderAccessLevel = BuilderAccessLevel.Internal)]
public sealed partial class IndicativeIndexRollover {
We get the following issue. The ToBuilder
method accessibility needs to match the builder class accessibility.
@xaviergonz Hi, I've just taken over a project which uses this Nuget package extensively and have been experimenting/learning what it's for. I notice you haven't updated the repo for a long time. I have some issues, but not sure it's worth creating them here unless you expect to do any work on it to support VS2022, .NET 5,6,7 etc.
Hello,
I'm interested in this class and was trying to use it in a PCL .netStandard 1.3 and it fails to install. Can you support .NETStandard?
I created a netstandard 2.0 library that used T4Immutable, and packaged that as a nuget package.
Then, I created a new solution with projects that referenced my nuget package.
It resulted in the following:
Hi,
Awesome library by the way. It's reminiscent of Google's AutoValue.
How could we go about adding support for nullable properties? Right now, using the Builder
class requires non-null properties, but the regular constructor supports nullables. Ideally we'd add an attribute for [Nullable]
, which then affects the Builder code emitted at https://github.com/xaviergonz/T4Immutable/blob/master/src/content/T4Immutable/T4Immutable.tt#L421. It looks like everything else (Helpers and OptParam) already support nulls.
I can work on the PR, just wanted to make sure you agree with the approach.
Michael
Hi,
I have to say it is great idea that you have created this T4, it is very helpful and saved me lot of code to write. :)
What I found is, that you can improve a performance in Equals method by adding in generated method ImmutableEquals by adding this.GetHashCode().Equals(obj.GetHashCode()) as first code in return statement, before T4Immutable.Helpers.AreEqual for each property.
Because by Microsoft specification,:
https://msdn.microsoft.com/en-us/library/system.object.gethashcode(v=vs.110).aspx
A hash function must have the following properties:
If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two objects do not have to return different values.
You will be sure, that these two object are same and also it is faster to compare two integers first, and than all properties
What do you think?
Thanks
Frankie
Applicable to new-style (.NET Core, .NET Standard, UWP, Portable Class Libraries...) but not to old-style (.NET Framework) projects
Since AFAIK right now NuGet does not support copying files to the project itself you will need to follow the next steps:
"Build - Transform All T4 Templates" won't work, so you will need to right click the file T4Immutable/T4Immutable.tt and click on "Run custom tool" whenever you want to regenerate your templates.
You will need to do 2 and 3 everytime you update this nuget package.
Does anybody know how to overcome this limitation?
In the case of the class, I'd like the generated With
method to create a new object updating Price
but not capable of updating 'Id'.
Can we decorate the 'Id' property to prevent it being included in the With
method parameter list?
[ImmutableClass]
class StockStatus {
[SomeExcludeAttribute]
public string Id { get; } // Wish to exclude this from the With method
public decimal Price { get; }
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.