EntityFrameworkTraits 0.0.1
dotnet add package EntityFrameworkTraits --version 0.0.1
NuGet\Install-Package EntityFrameworkTraits -Version 0.0.1
<PackageReference Include="EntityFrameworkTraits" Version="0.0.1" />
<PackageVersion Include="EntityFrameworkTraits" Version="0.0.1" />
<PackageReference Include="EntityFrameworkTraits" />
paket add EntityFrameworkTraits --version 0.0.1
#r "nuget: EntityFrameworkTraits, 0.0.1"
#:package EntityFrameworkTraits@0.0.1
#addin nuget:?package=EntityFrameworkTraits&version=0.0.1
#tool nuget:?package=EntityFrameworkTraits&version=0.0.1
Entity Framework Traits
Compose your entity with set of traits / features / behaviours without inheritance
Purpose
The purpose of this library is to provide a way to compose your entity with set of traits / features / behaviours without inheritance. This is done by using interfaces and extension methods. This allows you to create a clean and maintainable codebase, while still being able to add new features to your entities easily.
According to https://en.wikipedia.org/wiki/Trait_(computer_programming): In computer programming, a trait is a language concept that represents a set of methods that can be used to extend the functionality of a class.
While traits are not a new concept, they are partially implemented in C# using interfaces and extension methods. This implementation have limitations of not providing the ability to extend the class with trait`s properties which are essential to entity definition in Entity Framework.
This library aims to provide a way to compose your entity with set of traits / features / behaviours without inheritance. This is done by using interfaces and code generation. This allows you to create a clean and maintainable codebase, while still being able to add new features to your entities easily.
Example
Step 1. Define your trait
public interface ISoftDeletable
{
public bool IsDeleted { get; set; }
}
public static class EntityConfigurationExtensionISoftDeletable
{
public static void ApplyTrait<TEntity, TSoftDeletable>(this EntityTypeBuilder<TEntity> modelBuilder) where TEntity : class, ISoftDeletable
where TSoftDeletable: ISoftDeletable
{
modelBuilder.Property(x => x.IsDeleted)
.HasDefaultValue(false)
.IsRequired();
}
}
Step 2. Make your entity to be a partial class
public partial class MyEntity
{
// your entity definion here
}
Step 3. Add the trait to your entity thru entity configuration
public class MyEntityConfigurationConfiguration
{
public void Configure(EntityTypeBuilder<MyEntity> builder)
{
builder.ToTable("MyEntity");
builder.ApplyTrait<MyEntity, ISoftDeletable>();
// rest of your entity configuration here
}
}
The ApplyTrait method is both - adding the necessary configuration to the entity configuration and informs the code generator to copy the properties from the trait interface to the generated part of the entity class.
Result
In the result you'll get generated partial class with the properties from the trait interface:
// <auto-generated>
public partial class MyEntity : ISoftDeletable
{
public System.bool IsDeleted { get; set; }
}
// </auto-generated>
| Product | Versions 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.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 | 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. |
-
.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.0.1 | 234 | 4/10/2025 |