Hints are used to fine-tune code generation. The ToString hint determines if the ToString() method should be generated. This method provides a text-based class diagram in the format mermaid. To see this diagram, just call the ToString method and copy the text to this site. An example class diagram can be seen below.
In addition, setup hints can be comments before the Setup method in the form hint = value
, for example: // ToString = On
.
using Pure.DI;
DI.Setup(nameof(Composition))
.Hint(Hint.ToString, "On")
.Bind().To<Dependency>()
.Bind().To<Service>()
.Root<IService>("MyService");
var composition = new Composition();
string classDiagram = composition.ToString();
interface IDependency;
class Dependency : IDependency;
interface IService;
class Service(IDependency dependency) : IService;
Running this code sample locally
- Make sure you have the .NET SDK 9.0 or later is installed
dotnet --list-sdk
- Create a net9.0 (or later) console application
dotnet new console -n Sample
- Add reference to NuGet package
dotnet add package Pure.DI
- Copy the example code into the Program.cs file
You are ready to run the example 🚀
dotnet run
Developers who start using DI technology often complain that they stop seeing the structure of the application because it is difficult to understand how it is built. To make life easier, you can add the ToString hint by telling the generator to create a ToString()
method.
For more hints, see this page.
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 MyService
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return new Service(new Dependency());
}
}
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
Service --|> IService
Dependency --|> IDependency
Composition ..> Service : IService MyService
Service *-- Dependency : IDependency
namespace Pure.DI.UsageTests.Hints.ToStringHintScenario {
class Composition {
<<partial>>
+IService MyService
}
class Dependency {
+Dependency()
}
class IDependency {
<<interface>>
}
class IService {
<<interface>>
}
class Service {
+Service(IDependency dependency)
}
}