RichEntity 1.1.3
dotnet add package RichEntity --version 1.1.3
NuGet\Install-Package RichEntity -Version 1.1.3
<PackageReference Include="RichEntity" Version="1.1.3" />
paket add RichEntity --version 1.1.3
#r "nuget: RichEntity, 1.1.3"
// Install RichEntity as a Cake Addin #addin nuget:?package=RichEntity&version=1.1.3 // Install RichEntity as a Cake Tool #tool nuget:?package=RichEntity&version=1.1.3
RichEntity
A library for reducing boilerplate code when defining entities. It uses Roslyn's Source Generators to generate the code needed for entity definition.
NOTE!
If your application failing to compile becuase of dotnet failing to locate Microsoft.CodeAnalysis
package, please update your dotnet sdk to the latest version.
Usage
To generate an entity, you need to add an IEntity<TIdentifier>
to base list of your entity class.
public partial class Sample : IEntity<int> { }
The library will generate this code in the background.
public partial class Sample : IEquatable<Sample>
{
public Int32 Id { get; protected init; }
#pragma warning disable CS8618
protected Sample(Int32 id)
#pragma warning restore CS8618
{
Id = id;
}
#pragma warning disable CS8618
protected Sample()
#pragma warning restore CS8618
{
}
#nullable enable
public bool Equals(Sample? other)
#nullable restore
{
return (other?.Id.Equals(Id) ?? false);
}
#nullable enable
public override bool Equals(object? other)
#nullable restore
{
return Equals(other as Sample);
}
public override int GetHashCode() => (Id).GetHashCode();
}
You can customize generated code with attributes.
ConfigureConstructorsAttribute
To configure generated entity constructors, decorate type with
ConfigureConstructorsAttribute
.
Settings:
- ParametrizedConstructorAccessibility
Accessibility of parameterized constructor (Entity(TIdentity id)
).
[ConfigureConstructors(ParametrizedConstructorAccessibility = Accessibility.Public)]
public partial class Sample : IEntity<int> { }
public partial class Sample : IEquatable<Sample>
{
public Int32 Id { get; init; }
#pragma warning disable CS8618
protected Sample(Int32 id)
#pragma warning restore CS8618
{
Id = id;
}
#pragma warning disable CS8618
protected Sample()
#pragma warning restore CS8618
{
}
#nullable enable
public bool Equals(Sample? other)
#nullable restore
{
return (other?.Id.Equals(Id) ?? false);
}
#nullable enable
public override bool Equals(object? other)
#nullable restore
{
return Equals(other as Sample);
}
public override int GetHashCode() => (Id).GetHashCode();
}
ConfigureIdAttribute
To configure generated identifier, decorate type with ConfigureIdAttribute
.
Settings:
- SetterAccessibility
Accessibility ofId
property setter. - SetterType
Type ofId
property setter (set
/init
).
[ConfigureId(SetterAccessibility = Accessibility.Internal, SetterType = SetterType.Set)]
public partial class Sample : IEntity<int> { }
public partial class Sample : IEquatable<Sample>
{
public Int32 Id { get; internal set; }
#pragma warning disable CS8618
protected Sample(Int32 id)
#pragma warning restore CS8618
{
Id = id;
}
#pragma warning disable CS8618
protected Sample()
#pragma warning restore CS8618
{
}
#nullable enable
public bool Equals(Sample? other)
#nullable restore
{
return (other?.Id.Equals(Id) ?? false);
}
#nullable enable
public override bool Equals(object? other)
#nullable restore
{
return Equals(other as Sample);
}
public override int GetHashCode() => (Id).GetHashCode();
}
Composite key entities
For entities with composite key, you can use IEntity
interface with KeyProperty
attribute.
It will generate a constructor, an Equals
implemetation and a GetHashCode
implementation for that property.
For properties of type IEntity<TIdentifier>
, the property of type TIdentifier
will be generated as well
(and it will be used in all implementations listed above).
public partial class C : IEntity
{
[KeyProperty]
public Sample Sample { get; init; }
[KeyProperty]
public int Composite { get; init; }
}
This will generate:
public partial class C : IEquatable<C>
{
public Int32 SampleId { get; protected init; }
#pragma warning disable CS8618
protected C(Int32 composite, Int32 sampleId)
#pragma warning restore CS8618
{
Composite = composite;
SampleId = sampleId;
}
#pragma warning disable CS8618
protected C()
#pragma warning restore CS8618
{
}
#nullable enable
public bool Equals(C? other)
#nullable restore
{
return (other?.Composite.Equals(Composite) ?? false) && (other?.SampleId.Equals(SampleId) ?? false);
}
#nullable enable
public override bool Equals(object? other)
#nullable restore
{
return Equals(other as C);
}
public override int GetHashCode() => (Composite, SampleId).GetHashCode();
}
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- RichEntity.Annotations (>= 1.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Fixed file txt