erri120 / transparentvalueobjects Goto Github PK
View Code? Open in Web Editor NEWSource generator for Value Objects.
License: MIT License
Source generator for Value Objects.
License: MIT License
IValueObject.g.cs(28,50): Warning CS0436 : The type 'IValueObject' in 'TransparentValueObjects/TransparentValueObjects.ValueObjectIncrementalSourceGenerator/IValueObject.g.cs' conflicts with the imported type 'IValueObject' in 'TransparentValueObjects.Sample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'TransparentValueObjects/TransparentValueObjects.ValueObjectIncrementalSourceGenerator/IValueObject.g.cs'
All code generated as part of the post initialization output is written to the assembly that references the source generator. All of these types are public
and inside the TransparentValueObjects
namespace.
This means that if project A and project B use the source generator and project B references project A, the compiler will produce warning CS0436
because types like ValueObjectAttribute
are within project A and B and have the same namespace.
I often want a non-standard printer for my wrapped objects. Mostly so I can tell a difference between a integer and some other form of it. For example I may want Color<uint>
to be displayed as #FF00122
instead of 4278194466
.
Right now I can't override ToString since the code generation does it already.
TransparentValueObjects.Augments
libraryIAugmentWith<T0, T1, ..., TN>
interfaceIAugment
interface and marker classes// new
public readonly partial struct MyValueObject : IAugmentWith<
DefaultValue,
DefaultEqualityComparer,
JsonConverter,
EFCoreSupport> { }
// old
public readonly partial struct SampleValueObjectString :
IHasDefaultValue<SampleValueObjectString, string>,
IHasDefaultEqualityComparer<SampleValueObjectString, string>,
IHasSystemTextJsonConverter,
IHasEfCore<SampleValueObjectString, string> { }
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(113,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator ==(CDNName, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(114,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator !=(CDNName, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(116,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator ==(CDNName, string)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(117,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator !=(CDNName, string)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(119,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator ==(string, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(120,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator !=(string, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(126,34): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.explicit operator CDNName(string)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(127,34): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.explicit operator string(CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(142,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator <(CDNName, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(143,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator >(CDNName, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(144,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator <=(CDNName, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(145,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator >=(CDNName, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(147,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator <(string, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(148,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator >(string, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(149,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator <=(string, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(150,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator >=(string, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(152,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator <(CDNName, string)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(153,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator >(CDNName, string)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(154,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator <=(CDNName, string)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(155,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator >=(CDNName, string)'
0>EfCoreAugment.g.cs(7,39): Warning CS1570 : XML comment has badly formed XML -- 'The character(s) ',' cannot be used at this location.'
Works:
[ValueObject<Guid>]
public readonly partial struct MyValueObject { }
Doesn't work:
public class MyClass
{
[ValueObject<Guid>]
public readonly partial struct MyValueObject { }
}
Depending on the project imports, the JsonAugment
should create a System.Text.Json converter and/or a Newtonsoft.Json converter.
Only when using the EfCore Augment.
In cases when the ValueObject has functionality similar to an enum, but a VO would fit better.
I suggest a new Attribute marker ValueObjectInstanceAttribute
to mark the values as pre-defined and two new Augments - AllValuesAugment
and LimitValuesAugment
AllValuesAugment
exposes an IEnumerable that returns all pre-defined valuesLimitValuesAugment
limits the From
function to only handle the values from pre-definded, throwing if a non pre-defined value is provided.[ValueObject<int>]
public readonly partial struct SampleIntValueObject : IAugmentWith<DefaultValueAugment, AllValuesAugment>
{
[ValueObjectInstance]
public static SampleIntValueObject Default { get; } = From(0);
[ValueObjectInstance]
public static SampleIntValueObject Instance1 { get; } = From(1);
[ValueObjectInstance]
public static SampleIntValueObject Instance2 { get; } = From(2);
public static SampleIntValueObject DefaultValue => Default;
// Auto Generated
public static IEnumerable<SampleIntValueObject> All
{
get
{
yield return Default;
yield return Instance1;
yield return Instance2;
}
}
}
[ValueObject<int>]
public readonly partial struct SampleIntValueObject : IAugmentWith<LimitValuesAugment>
{
[ValueObjectInstance]
public static SampleIntValueObject Default { get; } = From(0);
[ValueObjectInstance]
public static SampleIntValueObject Instance1 { get; } = From(1);
[ValueObjectInstance]
public static SampleIntValueObject Instance2 { get; } = From(2);
// Will throw if values other than 0,1,2 will be suplied to From
}
TVO generates dead code that is unfriendly towards runtimes that are older than .NET 7.
TVO, by default generates types for the following augments, regardless of whether the features are used or not:
DefaultEqualityComparerAugment.g.cs
DefaultValueAugment.g.cs
EfCoreAugment.g.cs
JsonAugment.g.cs
IDefaultEqualityComparer.g.cs
IDefaultValue.g.cs
IValueObject.g.cs
The first 4 can only be used on .NET 5
and above due to ExcludeFromCodeCoverage
with Justification
.
/// <summary>
/// Augment to enable JSON support by generting a JSON converter for the value object.
/// </summary>
/// <remarks>
/// This only supports <c>System.Text.Json</c> at the moment.
/// </remarks>
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage(Justification = "Auto-generated")]
internal sealed class JsonAugment : global::TransparentValueObjects.IAugment { }
The last 3 can only be used on (.NET 6 with LangVersion==preview
) or .NET 7+, due to use of static abstract.
/// <summary>
/// Represents the interface for <see cref="global::TransparentValueObjects.DefaultEqualityComparerAugment"/>
/// with members that the consumer has to implement to support the augment.
/// </summary>
internal interface IDefaultEqualityComparer<out TValueObject, TInnerValue>
where TValueObject : global::TransparentValueObjects.IValueObject<TInnerValue>
where TInnerValue : notnull
{
/// <summary>
/// Gets a equality comparer for the inner value type to be used by default.
/// </summary>
public static abstract global::System.Collections.Generic.IEqualityComparer<TInnerValue> InnerValueDefaultEqualityComparer { get; }
}
Unused code is not generated.
ExcludeFromCodeCoverage
w/ Justification is used on .NET 5+
only. Older runtimes use no justification.
Alternatively source generator warns if TargetFramework
is set too low.
When no augments used.
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.