Basic Use with Func
Ryan yet again was asking for how to do another crazy thing with Structure Map. :)
He wanted to know if there was a way to control the construction of a concrete type. Well.. he didn’t put it that way but it is what he wanted to know.
Besides the regular For().Use
To demo this I have a Foo class that accepts a string in the constructor.
1 2 3 4 5 6 7 8 9 |
|
We want to pass in the message explicitly so we configure our container and tell it how to provide the concrete implementation.
1 2 |
|
And the tests to verify it works :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Using Func<IContext, T>
There are other scenarios where not only do you want more control on a concrete types construction, but also need to resolve other dependencies. SM also allows you to specify a Func<IContext,T> where the IContext allows you to resolve classes from the container.
So if we have a scenario with an IBar and an IFoo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
We can use the Func<IContext,T> similar to how we used the Func
1 2 3 4 5 6 7 8 9 10 11 12 |
|
And naturally our test to verify it works:
1 2 3 4 5 6 7 |
|
So that was a brief introduction into using Funcs to provide the concrete implementation of an interface for Structure Map.
All the Learning Structure Map code can be found in my Learning solution on Git Hub : http://github.com/RookieOne/Learning