Skip to content

Latest commit

 

History

History
147 lines (125 loc) · 3.15 KB

span-and-readonlyspan.md

File metadata and controls

147 lines (125 loc) · 3.15 KB

Span and ReadOnlySpan

Specifying Span<T> and ReadOnlySpan<T> work the same as with the array T[].

using Shouldly;
using Pure.DI;

DI.Setup(nameof(Composition))
    .Bind<Dependency>('a').To<Dependency>()
    .Bind<Dependency>('b').To<Dependency>()
    .Bind<Dependency>('c').To<Dependency>()
    .Bind<IService>().To<Service>()

    // Composition root
    .Root<IService>("Root");

var composition = new Composition();
var service = composition.Root;
service.Count.ShouldBe(3);

struct Dependency;

interface IService
{
    int Count { get; }
}

class Service(ReadOnlySpan<Dependency> dependencies) : IService
{
    public int Count { get; } = dependencies.Length;
}
Running this code sample locally
dotnet --list-sdk
  • Create a net9.0 (or later) console application
dotnet new console -n Sample
dotnet add package Pure.DI
dotnet add package Shouldly
  • Copy the example code into the Program.cs file

You are ready to run the example 🚀

dotnet run

This scenario is even more efficient in the case of Span<T> or ReadOnlySpan<T> when T is a value type. In this case, there is no heap allocation, and the composition root IService looks like this:

public IService Root
{
  get
  {
    ReadOnlySpan<Dependency> dependencies = stackalloc Dependency[3] { new Dependency(), new Dependency(), new Dependency() };
    return new Service(dependencies);
  }
}

The following partial class will be generated:

partial class Composition
{
  private readonly Composition _root;

  [OrdinalAttribute(256)]
  public Composition()
  {
    _root = this;
  }

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

  public IService Root
  {
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    get
    {
      ReadOnlySpan<Dependency> transientReadOnlySpan1 = stackalloc Dependency[3]
      {
        new Dependency(),
        new Dependency(),
        new Dependency()
      };
      return new Service(transientReadOnlySpan1);
    }
  }
}

Class diagram:

---
 config:
  class:
   hideEmptyMembersBox: true
---
classDiagram
	Service --|> IService
	Composition ..> Service : IService Root
	Service *--  ReadOnlySpanᐸDependencyᐳ : ReadOnlySpanᐸDependencyᐳ
	ReadOnlySpanᐸDependencyᐳ *--  Dependency : 'a'  Dependency
	ReadOnlySpanᐸDependencyᐳ *--  Dependency : 'b'  Dependency
	ReadOnlySpanᐸDependencyᐳ *--  Dependency : 'c'  Dependency
	namespace Pure.DI.UsageTests.BCL.SpanScenario {
		class Composition {
		<<partial>>
		+IService Root
		}
		class Dependency {
				<<struct>>
			+Dependency()
		}
		class IService {
			<<interface>>
		}
		class Service {
			+Service(ReadOnlySpanᐸDependencyᐳ dependencies)
		}
	}
	namespace System {
		class ReadOnlySpanᐸDependencyᐳ {
				<<struct>>
		}
	}
Loading