Skip to content

Latest commit

 

History

History
155 lines (130 loc) · 3.57 KB

File metadata and controls

155 lines (130 loc) · 3.57 KB

Bind attribute for a generic type

Demonstrates how to use the Bind attribute to configure bindings for generic types, allowing automatic registration without explicit binding declarations.

using Shouldly;
using Pure.DI;

DI.Setup(nameof(Composition))
    .Bind().As(Lifetime.Singleton).To<CommentsFactory>()
    .Bind().To<ArticleService>()

    // Composition root
    .Root<IArticleService>("ArticleService");

var composition = new Composition();
var articleService = composition.ArticleService;
articleService.DisplayComments();

interface IComments<T>
{
    void Load();
}

class Comments<T> : IComments<T>
{
    public void Load()
    {
    }
}

class CommentsFactory
{
    // The 'TT' type marker in the attribute indicates that this method
    // can produce 'IComments<T>' for any generic type 'T'.
    // This allows the factory to handle all requests for IComments<T>.
    [Bind(typeof(IComments<TT>))]
    public IComments<T> Create<T>() => new Comments<T>();
}

interface IArticleService
{
    void DisplayComments();
}

class ArticleService(IComments<Article> comments) : IArticleService
{
    public void DisplayComments() => comments.Load();
}

class Article;
Running this code sample locally
dotnet --list-sdk
  • Create a net10.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

Note

The Bind attribute provides a declarative way to specify bindings directly on types, reducing the need for manual composition setup.

The following partial class will be generated:

partial class Composition
{
#if NET9_0_OR_GREATER
  private readonly Lock _lock = new Lock();
#else
  private readonly Object _lock = new Object();
#endif

  private CommentsFactory? _singletonCommentsFactory62;

  public IArticleService ArticleService
  {
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    get
    {
      IComments<Article> transientIComments231;
      if (_singletonCommentsFactory62 is null)
        lock (_lock)
          if (_singletonCommentsFactory62 is null)
          {
            _singletonCommentsFactory62 = new CommentsFactory();
          }

      CommentsFactory localInstance_1182D1276 = _singletonCommentsFactory62;
      transientIComments231 = localInstance_1182D1276.Create<Article>();
      return new ArticleService(transientIComments231);
    }
  }
}

Class diagram:

---
 config:
  maxTextSize: 2147483647
  maxEdges: 2147483647
  class:
   hideEmptyMembersBox: true
---
classDiagram
	ArticleService --|> IArticleService
	Composition ..> ArticleService : IArticleService ArticleService
	ArticleService *--  ICommentsᐸArticleᐳ : ICommentsᐸArticleᐳ
	ICommentsᐸArticleᐳ o-- "Singleton" CommentsFactory : CommentsFactory
	namespace Pure.DI.UsageTests.Basics.BindAttributeForGenericTypeScenario {
		class ArticleService {
				<<class>>
			+ArticleService(ICommentsᐸArticleᐳ comments)
		}
		class CommentsFactory {
				<<class>>
			+CommentsFactory()
		}
		class Composition {
		<<partial>>
		+IArticleService ArticleService
		}
		class IArticleService {
			<<interface>>
		}
		class ICommentsᐸArticleᐳ {
				<<interface>>
		}
	}
Loading