YairHalberstadt / stronginject

compile time dependency injection for .NET
MIT License
845 stars 24 forks source link

Get typeof parent in factory #117

Closed trampster closed 2 years ago

trampster commented 3 years ago

We use serilog for logging and it needs to be initialised with the type of the object it will be logging for (the type it is injected into)

ILogger specificLogger = logger.ForContext(typeof(Parent))

Our types have the ILogger as constructor arguments and out current injection framework creates them for us with the correct context.

I can't see anyway to achieve this with stronginject Factories

YairHalberstadt commented 3 years ago

Currently stronginject have no knowledge of why they're being resolved. I would like to provide a solution to this, but it is not a priority at the moment.

In the meantime, I would suggest using Microsoft.Extensions.Logging.ILogger. Their logger takes a type parameter to mark the type it's used for, which allows easy resolution. I'm sure there is an implementation of this interface for Serilog.

I know changing everything may not be practical. I will try to think of a way to intercept a resolution and inject some extra information.

YairHalberstadt commented 3 years ago

I have proposed a design at https://github.com/YairHalberstadt/stronginject/issues/109#issuecomment-883068923. Please take a look and let me know what you think! Thanks!

YairHalberstadt commented 2 years ago

CLosing as duplicate of #109