How to inject factory-based dependencies from method aspect into type?

Let's use an example - in Microsoft.Extensions.Logging, I might register an ILoggerFactory so I can inject it into a type's constructor and then build a field from that. For example, typically this might look like this usually:
public class MyType
{
private readonly ILogger<MyType> _logger;

public MyType(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<MyType>();
}
}
public class MyType
{
private readonly ILogger<MyType> _logger;

public MyType(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<MyType>();
}
}
So I'd like to do something similar here. It's not quite what the dependency injection framework does because I'm injecting one thing into the constructor, but then making something else from it. And ideally, it's something that I could do from a MethodAspect because I want to be able to apply the attribute to various methods. Is this possible? As always, thank you!
2 Replies
Petr Onderka
Petr Onderka15mo ago
It's possible and it's documented here: https://doc.metalama.net/conceptual/aspects/dependency-injection#implementing-an-adaptor-for-a-new-dependency-injection-framework. Specifically, it would look like this (using 2023.4.3-preview):
Whit
WhitOP15mo ago
Perfect - thank you
Want results from more Discord servers?
Add your server