Skip to content

Latest commit

 

History

History
109 lines (90 loc) · 2.79 KB

File metadata and controls

109 lines (90 loc) · 2.79 KB

ToString hint

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<Database>()
    .Bind().To<UserRepository>()
    .Root<IUserRepository>("GetUserRepository");

var composition = new Composition();
// The ToString() method generates a class diagram in mermaid format
string classDiagram = composition.ToString();

interface IDatabase;

class Database : IDatabase;

interface IUserRepository;

class UserRepository(IDatabase database) : IUserRepository;
Running this code sample locally
dotnet --list-sdk
  • Create a net10.0 (or later) console application
dotnet new console -n Sample
  • Add a reference to the 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
{
  public IUserRepository GetUserRepository
  {
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    get
    {
      return new UserRepository(new Database());
    }
  }
}

Class diagram:

---
 config:
  maxTextSize: 2147483647
  maxEdges: 2147483647
  class:
   hideEmptyMembersBox: true
---
classDiagram
	Database --|> IDatabase
	UserRepository --|> IUserRepository
	Composition ..> UserRepository : IUserRepository GetUserRepository
	UserRepository *--  Database : IDatabase
	namespace Pure.DI.UsageTests.Hints.ToStringHintScenario {
		class Composition {
		<<partial>>
		+IUserRepository GetUserRepository
		}
		class Database {
				<<class>>
			+Database()
		}
		class IDatabase {
			<<interface>>
		}
		class IUserRepository {
			<<interface>>
		}
		class UserRepository {
				<<class>>
			+UserRepository(IDatabase database)
		}
	}
Loading