If I understand correctly, you want to inject an instance of ILogger into a static method. As you probably already figured out, you cannot use the Dependency Injection "normal way" when the dependent method is static.
Here you can find the service locator pattern .
Using the StructureMap IoC container (but you can really use any container), the configuration for connecting it might look like this:
For<ILogger>().Use<SomeLoggerImplementation>();
:
public class ValidateDataInAPI
{
private static ILogger Logger
{
get { return DependencyResolver.Resolve<ILogger>(); }
}
public static bool IsValid(string data)
{
If(error)
{
Logger.Error("Log error as implemented by caller");
}
}
}
, -, , , - .
Injection Dependency , , .
( "" ), . IoC Unit Tests, ? Dependency Injection , unit test .
Injection Dependency, . .
:
public class ValidateDataInAPI : IValidateDataInAPI
{
private readonly ILogger _logger;
public ValidateDataInAPI(ILogger logger)
{
_logger = logger;
}
public bool IsValid(string data)
{
If(error)
{
_logger.Error("Log error as implemented by caller");
}
}
}
, , , API:
public interface IValidateDataInAPI
{
bool IsValid(string data);
}
Validator, unit test API .
, , IsValid , , , .