vb.netservicestackfunq

Why would one need this lambda: Function(x) x


I found this line in an old branch and, as I have a lot of respect for the (unreachable) author, I'm trying to make sense of one specific line, more precisely the lambda at the end:

container.Register(Of ServiceStack.Configuration.IResolver)(Function(x) x)

container is a Funq.Container through ServiceStack. The intellisense tells me that the lambda is filling in for a (factory As Func(Of Funq.Container, ServiceStack.Configuration.IResolver)).

There is two things that I can assume about the author: he's better coder than I am, and he may have left this unfinished. For now I'm guessing that this lambda was deliberate and not some unfinished line with no clear intent, but so far nobody could help me understand why.


Solution

  • The container is a dependency injection container. Code elsewhere will ask the container for instances of interfaces. The code here is registration code, which is telling the container how to provide an IResolver. Furthermore, it's designed to accept a factory function; resolution later will call the function to product the requested IResolver.

    In this case, it appears that the container itself implements IResolver. The lambda is a function that returns its argument, so it's trivial; its argument is a Funq.Container and it returns a ServiceStack.Configuration.IResolver, so the only way this can compile is if the container implements that interface.

    Thus: The container implements IResolver. The code registers a factory function which always returns back the container itself when called.

    It seems rather odd to do. I don't know ServiceStack at all, so I'm not sure why it's done this way.