PartialMixin 1.0.55

dotnet add package PartialMixin --version 1.0.55
NuGet\Install-Package PartialMixin -Version 1.0.55
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PartialMixin" Version="1.0.55" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PartialMixin --version 1.0.55
#r "nuget: PartialMixin, 1.0.55"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PartialMixin as a Cake Addin
#addin nuget:?package=PartialMixin&version=1.0.55

// Install PartialMixin as a Cake Tool
#tool nuget:?package=PartialMixin&version=1.0.55

NuGet Build status GitHub license

PartialMixins <img src="https://raw.githubusercontent.com/LokiMidgard/PartialMixins/master/combine.png" width="35px" height="35px" />

Extends C# with Mixins. The Mixins are simulated using partial classes, instead of ilweaving like other librarys. The MixinAttribute copys all members of the targeted Mixin to a partial class implementation of the anotated type. This give you intellisense support on your classes. It uses the roslyn code generation framework.

Usage

First add the NuGet package to your Project.

To create a Mixin just declare a class.

    class IdMixin
    {
        public Guid Id { get; set; } = Guid.NewGuid();
    }

To apply the mixin you need to declare the MixinAttribute on the class that should implement the desired mixin. This class must also have the partial modifyer. Pass the Type of the Mixin in the attribute constructor. After the next time you build your source the mixin is implemented by your class.

    [Mixin(typeof(IdMixin))]
    partial class BusinessObject
    {
        public string Name { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var b1 = new BusinessObject() { Name = "Paul" };
            Console.WriteLine($"{b1.Id}:{b1.Name}");
        }

    }

Your mixins can also implement interfaces.

    interface Id : IEquatable<Id>
    {
        Guid Id { get; }
    }

    class IdMixin : Id
    {
        public Guid Id { get; set; } = Guid.NewGuid();

        public bool Equals(Id other) => Id.Equals(other.Id);

    }

If you use the Mixin Type itself, it will be substitueded with the consuming type.

    abstract class AddMixin
    {
        public abstract AddMixin Add(AddMixin other);
        public static AddMixin operator +(AddMixin a1, AddMixin a2)
        {
            return a1.Add(a2);
        }
    }

A Type that uses this Mixin may look like this:

namespace Sample
{
    [Mixin(typeof(AddMixin))]
    public partial struct MyNumber
    {
        public int Value { get; }
        public MyNumber(int value) => this.Value = value;
        public partial MyNumber Add(MyNumber other) => new MyNumber(this.Value + other.Value);
    }
}

// Generated Code...

namespace Sample
{
    public partial struct MyNumber
    {
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Mixin Task", "1.0.51.0")]
        public partial Sample.MyNumber Add(Sample.MyNumber other);
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Mixin Task", "1.0.51.0")]
        public static Sample.MyNumber operator +(Sample.MyNumber a1, Sample.MyNumber a2)
        {
            return a1.Add(a2);
        }
    }
}

As you can see absract methods are implemented as partial Methods. That way you can reqirer the consumer of your Mixin to provide specific functionallity.

Restrictions

  • The classes that implement the mixins must be partial.
  • Mixins may not have constructors.
  • Mixins may not have methods with the same method signiture then any other mixin that is implemented by the same class, or any method of the implementing class itself. (unless explicitly implemented methods of interfaces)
  • Mixins should not inhire from anything other than Object

Roadmap

  • Better compiletime error reporting
  • Generated source file shuold automaticly added to the Project
  • Automated NuGet build (ci)
  • Allow mixins of mixins. (As long it is no circal dependency)
  • Support for Generic Mixins
  • Better using conflict resolve strategy
  • Add GenerteadCodeAttribute to Methods and Propertys

This Software is licensed under MIT.

Used Assets

Icon Combine created by Paul Philippe Berthelon Bravo published under Public Domain.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .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
1.0.55 2,292 8/26/2022
1.0.54 1,756 1/15/2022
1.0.53 2,039 1/15/2022
1.0.52 1,835 5/13/2021
1.0.46 1,685 5/11/2021
1.0.16 2,131 5/11/2021
1.0.5 1,281 8/18/2019
1.0.4 1,343 5/12/2019
0.8.3.8574 1,630 12/16/2017
0.8.2.35720 1,579 11/14/2016
0.8.2.16796 1,622 7/2/2016
0.8.2.10417 1,518 6/12/2017
0.8.1.31944 1,513 4/6/2016
0.8.1.31524 1,575 4/6/2016
0.8.1.29991 1,565 4/6/2016
0.8.1.29112 1,562 4/6/2016
0.8.1.16530 1,498 7/2/2016
0.8.1.15911 1,552 7/2/2016
0.8.0.34846 1,572 4/3/2016
0.8.0.25240 1,567 4/3/2016
0.8.0.24528 1,508 4/3/2016
0.8.0.4748 1,594 4/3/2016
0.8.0.2685 1,534 4/2/2016
0.8.0.2188 1,511 4/2/2016

Updates package to include the readme.