Skip to content

Latest commit

 

History

History
110 lines (85 loc) · 2.82 KB

inject-attribute.md

File metadata and controls

110 lines (85 loc) · 2.82 KB

Inject attribute

CSharp

If you want to use attributes in your libraries but don't want to create your own, you can add this package to your projects:

NuGet

It contains attributes like Inject and Inject<T> that work for constructors and their arguments, methods and their arguments, properties and fields. They allow you to setup all injection parameters.

using Pure.DI.Abstractions;

interface IPerson;

class Person([Inject("NikName")] string name) : IPerson
{
    private object? _state;

    [Inject<int>] internal object Id = "";

    public void Initialize([Inject<Uri>("Person Uri", 1)] object state) =>
        _state = state;

    public override string ToString() => $"{Id} {name} {_state}";
}

DI.Setup(nameof(PersonComposition))
    .Arg<int>("personId")
    .Bind<Uri>("Person Uri").To(_ => new Uri("https://github.com/DevTeam/Pure.DI"))
    .Bind("NikName").To(_ => "Nik")
    .Bind().To<Person>()

    // Composition root
    .Root<IPerson>("Person");

var composition = new PersonComposition(personId: 123);
var person = composition.Person;
person.ToString().ShouldBe("123 Nik https://github.com/DevTeam/Pure.DI");

This package should also be included in a project:

NuGet

The following partial class will be generated:

partial class PersonComposition
{
  private readonly PersonComposition _root;

  private readonly int _argPersonId;

  [OrdinalAttribute(10)]
  public PersonComposition(int personId)
  {
    _argPersonId = personId;
    _root = this;
  }

  internal PersonComposition(PersonComposition parentScope)
  {
    _root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root;
    _argPersonId = _root._argPersonId;
  }

  public IPerson Person
  {
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    get
    {
      Uri transientUri2 = new Uri("https://github.com/DevTeam/Pure.DI");
      string transientString1 = "Nik";
      Person transientPerson0 = new Person(transientString1);
      transientPerson0.Id = _argPersonId;
      transientPerson0.Initialize(transientUri2);
      return transientPerson0;
    }
  }
}

Class diagram:

classDiagram
	class PersonComposition {
		<<partial>>
		+IPerson Person
	}
	class Int32
	class Uri
	class String
	Person --|> IPerson
	class Person {
		+Person(String name)
		~Object Id
		+Initialize(Object state) : Void
	}
	class IPerson {
		<<interface>>
	}
	PersonComposition ..> Person : IPerson Person
	Person *--  String : "NikName"  String
	Person o-- Int32 : Argument "personId"
	Person *--  Uri : "Person Uri"  Uri
Loading