Minerals.AutoMixins by Szymon Halucha
Nuget / site data
Details
Info
Name: Minerals.AutoMixins
Package for automatic mixin design pattern generation using incremental generator
Author: Szymon Halucha
NuGet: https://www.nuget.org/packages/Minerals.AutoMixins/
You can find more details at https://github.com/SzymonHalucha/Minerals.AutoMixins
Source : https://github.com/SzymonHalucha/Minerals.AutoMixins
Original Readme
Minerals.AutoMixins
This NuGet package provides a capability to automatically generate a mix-in design pattern for C# classes by using only one attribute. This allows you to easily extend the functionality of existing classes.
Funkcje
- Easy mix-in definition: Mix-in object are defined by using the
[GenerateMixin]
attribute. - Easy addition of mix-ins to a class: To add a mix-in object to a class, use the
[AddMixin(typeof(ExampleMixinClass))]
attribute. - Optimized code generation: The package uses an incremental source generator, so it doesn't significantly slow down the compilation process.
- Compatibility with .NET Standard 2.0 and C# 7.3+: Works on a wide range of platforms and development environments.
Installation
Add the Minerals.AutoMixins nuget package to your C# project using the following methods:
1. Project file definition
<PackageReference Include="Minerals.AutoMixins" Version="0.2.1" />
2. dotnet command
dotnet add package Minerals.AutoMixins
Why choose this package instead of the Default Interface Implementation?
Because the C# language option called "Default Interface Implementation", has limited runtime platform support. The Minerals.AutoMixins package is compatible with netstandard2.0
and C# language version 7.3+.
Usage
To define a mix-in object, add the [GenerateMixin]
attribute to the selected class.
Defining mix-in objects
namespace Examples
{
[Minerals.AutoMixins.GenerateMixin]
public class ExampleMixin1
{
public float Property1 { get; set; } = 0.5f;
private int _field1 = 0;
private void Method1()
{
Console.WriteLine("Test1");
}
}
[Minerals.AutoMixins.GenerateMixin]
public class ExampleMixin2
{
public string PropertyText1 { get; set; } = "Test2";
}
}
Using mix-in objects
To use the selected mix-in object, add the [AddMixin(typeof(ExampleMixin1))]
attribute to the selected class. The class implementing the AddMixin attribute must have the partial modifier to work properly.
namespace Examples
{
[Minerals.AutoMixins.AddMixin(typeof(ExampleMixin1))]
public partial class ExampleClass
{
public int MyProperty { get; set; } = 3;
}
}
The code above will generate an ExampleClass.g.cs
file with a partial class ExampleClass
.
namespace Examples
{
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Runtime.CompilerServices.CompilerGenerated]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public partial class ExampleClass
{
// MixinType: ExampleMixin1
public float Property1 { get; set; } = 0.5f;
private int _field1 = 0;
private void Method1()
{
Console.WriteLine("Test1");
}
}
}
Multiple mix-ins
This package allows you to add multiple mix-in objects to a single class through attribute arguments [AddMixin(typeof(ExampleMixin1), typeof(ExampleMixin2))]
.
namespace Examples
{
[Minerals.AutoMixins.AddMixin(typeof(ExampleMixin1), typeof(ExampleMixin2))]
public partial class ExampleClass
{
public int MyProperty { get; set; } = 3;
public void MyMethod()
{
}
}
}
The code above will generate an ExampleClass.g.cs
file with a partial class ExampleClass
.
namespace Examples
{
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Runtime.CompilerServices.CompilerGenerated]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public partial class ExampleClass
{
// MixinType: ExampleMixin1
public float Property1 { get; set; } = 0.5f;
private int _field1 = 0;
private void Method1()
{
Console.WriteLine("Test1");
}
// MixinType: ExampleMixin2
public string PropertyText1 { get; set; } = "Test2";
public string MethodText1()
{
return PropertyText1;
}
}
}
Versioning
We use SemVer for versioning. For the versions available, see the branches on this repository.
Authors
- Szymon Hałucha - Maintainer
See also the list of contributors who participated in this project.
License
This project is licensed under the MIT License - see the LICENSE file for details.
About
Generate Mixin from another classes
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Person.cs
- LogData.cs
This is the CSharp Project that references Minerals.AutoMixins
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Minerals.AutoMixins" Version="0.2.1" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of Minerals.AutoMixins in Program.cs
using DemoMixin;
Person person = new Person { Name = "Andrei Ignat" };
person.LogName();
This is the use of Minerals.AutoMixins in Person.cs
namespace DemoMixin;
[Minerals.AutoMixins.AddMixin(typeof(LogData))]
internal partial class Person
{
public string Name { get; set; }
public void LogName() => Log(Name);
}
This is the use of Minerals.AutoMixins in LogData.cs
namespace DemoMixin;
[Minerals.AutoMixins.GenerateMixin]
internal class LogData
{
public void Log(string data) => Console.WriteLine(data);
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- Person.g.cs
- AddMixinAttribute.g.cs
- GenerateMixinAttribute.g.cs
// <auto-generated>
// This code was generated by a tool.
// Name: Minerals.AutoMixins
// Version: 0.2.1+6c5634e46b130effbe00bd9d3f94459f1dbb2e85
// </auto-generated>
namespace DemoMixin
{
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Runtime.CompilerServices.CompilerGenerated]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal partial class Person
{
// MixinType: LogData
public void Log(string data) => Console.WriteLine(data);
}
}
// <auto-generated>
// This code was generated by a tool.
// Name: Minerals.AutoMixins
// Version: 0.2.1+6c5634e46b130effbe00bd9d3f94459f1dbb2e85
// </auto-generated>
#pragma warning disable CS9113
namespace Minerals.AutoMixins
{
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Runtime.CompilerServices.CompilerGenerated]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.AttributeUsage(global::System.AttributeTargets.Class | global::System.AttributeTargets.Struct, AllowMultiple = false, Inherited = false)]
public sealed class AddMixinAttribute : global::System.Attribute
{
public AddMixinAttribute(params global::System.Type[] mixins)
{
}
}
}
#pragma warning restore CS9113
// <auto-generated>
// This code was generated by a tool.
// Name: Minerals.AutoMixins
// Version: 0.2.1+6c5634e46b130effbe00bd9d3f94459f1dbb2e85
// </auto-generated>
namespace Minerals.AutoMixins
{
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Runtime.CompilerServices.CompilerGenerated]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.AttributeUsage(global::System.AttributeTargets.Class | global::System.AttributeTargets.Struct, AllowMultiple = false, Inherited = false)]
public sealed class GenerateMixinAttribute : global::System.Attribute
{
}
}
Usefull
Download Example (.NET C# )
Share Minerals.AutoMixins
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Minerals.AutoMixins