Skip to content

Injected constructor arguments #14

@znakeeye

Description

@znakeeye

Create a new console app targeting .NET 6. Then paste the code below.

As you can see, we quickly run into a problem when using Mixin classes where the constructor allows for injected dependencies. Is it possible to improve the library for this common use case?

Problem 1
Re-definition of the backing field is encountered in the sample below. I believe PartialMixin should simply choose one. Perhaps the one from the first attribute? I think this is a reasonable trade-off.

Problem 2
The constructor of the Mixin classes are generated as methods - without a return type. Here we should simply create a Foo constructor with all arguments from the constructors of the mixins. To avoid too complex rules, I guess we could limit support to one mixed-in constructor. E.g. in the sample below, we would simply generate this:

public Foo(IDependency dependency)
{
    this.dependency = dependency;
}

Sample

using Mixin;

namespace MyConsoleApp;

public interface IDependency {}
public class Dependency : IDependency {}

[Mixin(typeof(Bar))]
[Mixin(typeof(Baz))]
public partial class Foo
{
}

class Bar
{
    private readonly IDependency dependency;

    public Bar(IDependency dependency)
    {
        this.dependency = dependency;
    }
}

class Baz
{
    private readonly IDependency dependency;

    public Baz(IDependency dependency)
    {
        this.dependency = dependency;
    }
}

class Program
{
    public static void Main()
    {
        var foo = new Foo(new Dependency());
    }
}

image

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions