Hints are used to fine-tune code generation. The Resolve hint determines whether to generate Resolve methods. By default, a set of four Resolve methods are generated. Set this hint to Off to disable the generation of resolve methods. This will reduce class composition generation time, and no anonymous composition roots will be generated in this case. When the Resolve hint is disabled, only the regular root properties are available, so be sure to define them explicitly with the Root<T>(...) method.
In addition, setup hints can be comments before the Setup method in the form hint = value, for example: // Resolve = Off.
using Pure.DI;
using static Pure.DI.Hint;
DI.Setup(nameof(Composition))
.Hint(Resolve, "Off")
.Bind().To<DatabaseService>()
// When the "Resolve" hint is disabled, only the regular root properties
// are available, so be sure to define them explicitly
// with the "Root<T>(...)" method.
.Root<IDatabaseService>("DatabaseService")
.Bind().To<UserService>()
.Root<IUserService>("UserService");
var composition = new Composition();
// The "Resolve" method is not generated,
// so we can only access the roots through properties:
var userService = composition.UserService;
var databaseService = composition.DatabaseService;
// composition.Resolve<IUserService>(); // Compile error
interface IDatabaseService;
class DatabaseService : IDatabaseService;
interface IUserService;
class UserService(IDatabaseService database) : IUserService;Running this code sample locally
- Make sure you have the .NET SDK 10.0 or later installed
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 runFor more hints, see this page.
The following partial class will be generated:
partial class Composition
{
public IDatabaseService DatabaseService
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return new DatabaseService();
}
}
public IUserService UserService
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return new UserService(new DatabaseService());
}
}
}Class diagram:
---
config:
maxTextSize: 2147483647
maxEdges: 2147483647
class:
hideEmptyMembersBox: true
---
classDiagram
DatabaseService --|> IDatabaseService
UserService --|> IUserService
Composition ..> UserService : IUserService UserService
Composition ..> DatabaseService : IDatabaseService DatabaseService
UserService *-- DatabaseService : IDatabaseService
namespace Pure.DI.UsageTests.Hints.ResolveHintScenario {
class Composition {
<<partial>>
+IDatabaseService DatabaseService
+IUserService UserService
}
class DatabaseService {
<<class>>
+DatabaseService()
}
class IDatabaseService {
<<interface>>
}
class IUserService {
<<interface>>
}
class UserService {
<<class>>
+UserService(IDatabaseService database)
}
}