Why is Func <T> used in this code?

One of my colleagues came across this code, and we cannot understand why we need it _resolverThunk.

The code summary is as follows:

private Func<IDependencyResolver> _resolverThunk;

public SingleServiceResolver()
{
    _resolverThunk = () => DependencyResolver.Current;
}

private TService GetValueFromResolver()
{
   TService result = _resolverThunk().GetService<TService>();
   return result;
}
+4
source share
1 answer

If you look at another internal constructor, it sets the parameter _resolverThunkfrom the parameter. So that makes sense.

 internal SingleServiceResolver(Func<TService> staticAccessor, TService defaultValue, IDependencyResolver resolver, string callerMethodName)
        : this(staticAccessor, defaultValue, callerMethodName)
 {
     if (resolver != null)
     {
            _resolverThunk = () => resolver;
     }
 }

Otherwise, there is no reason for this.

+4
source

Source: https://habr.com/ru/post/1584029/


All Articles