Skip to content

Latest commit

 

History

History
152 lines (128 loc) · 3.65 KB

File metadata and controls

152 lines (128 loc) · 3.65 KB

OnCannotResolve wildcard hint

Hints are used to fine-tune code generation. The OnCannotResolve hint determines whether to generate a partial OnCannotResolve<T>(...) method to handle a scenario where an instance which cannot be resolved. In addition, setup hints can be comments before the Setup method in the form hint = value, for example: // OnCannotResolveContractTypeNameWildcard = string.

using Shouldly;
using Pure.DI;
using static Pure.DI.Hint;

// OnCannotResolveContractTypeNameWildcard = string
DI.Setup(nameof(Composition))
    .Hint(OnCannotResolve, "On")
    .Bind().To<DatabaseSettings>()
    .Bind().To<DataService>()
    .Root<IDataService>("DataService");

var composition = new Composition();
var dataService = composition.DataService;
dataService.Settings.ConnectionString.ShouldBe("Server=localhost;");


interface IDatabaseSettings
{
    string ConnectionString { get; }
}

class DatabaseSettings(string connectionString) : IDatabaseSettings
{
    public string ConnectionString { get; } = connectionString;
}

interface IDataService
{
    IDatabaseSettings Settings { get; }
}

class DataService(IDatabaseSettings settings) : IDataService
{
    public IDatabaseSettings Settings { get; } = settings;
}

partial class Composition
{
    private partial T OnCannotResolve<T>(
        object? tag,
        Lifetime lifetime)
    {
        // Emulates obtaining a configuration value
        if (typeof(T) == typeof(string))
        {
            return (T)(object)"Server=localhost;";
        }

        throw new InvalidOperationException("Cannot resolve.");
    }
}
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

The OnCannotResolveContractTypeNameWildcard hint helps define the set of types that require manual dependency resolution. You can use it to specify a wildcard to filter the full type name. For more hints, see this page.

The following partial class will be generated:

partial class Composition
{
  public IDataService DataService
  {
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    get
    {
      return new DataService(new DatabaseSettings(OnCannotResolve<string>(null, Lifetime.Transient)));
    }
  }


  private partial T OnCannotResolve<T>(object? tag, Lifetime lifetime);
}

Class diagram:

---
 config:
  maxTextSize: 2147483647
  maxEdges: 2147483647
  class:
   hideEmptyMembersBox: true
---
classDiagram
	DatabaseSettings --|> IDatabaseSettings
	DataService --|> IDataService
	Composition ..> DataService : IDataService DataService
	DatabaseSettings *--  String : String
	DataService *--  DatabaseSettings : IDatabaseSettings
	namespace Pure.DI.UsageTests.Hints.OnCannotResolveWildcardHintScenario {
		class Composition {
		<<partial>>
		+IDataService DataService
		}
		class DatabaseSettings {
				<<class>>
			+DatabaseSettings(String connectionString)
		}
		class DataService {
				<<class>>
			+DataService(IDatabaseSettings settings)
		}
		class IDatabaseSettings {
			<<interface>>
		}
		class IDataService {
			<<interface>>
		}
	}
	namespace System {
		class String {
				<<class>>
		}
	}
Loading