structuremap/structuremap — pull request #39 by tkellogg (2012-05-23 03:37:02+00:00)
Title: Autofactory parameters

in [a google groups conversation][1] a user was requesting that AutoFactories have parameters. I didn't realize what he wanted at the time, until I ran into the same situation. This pull request solves the problem by adding constructor arguments on a per-request basis.

To use, setup an interface that returns an object (which takes complex & primitive constructor arguments):

```csharp
class ValueService
{
public ValueService(string secretKey, IService service)
{
...
}
}

// Again, no implementation needed. One will be generated.
interface IValueFactory
{
ValueService CreateValue(string secretKey);
}
```

The magic here is that secretKey has the same name in factory & constructor. In the factory interface, the method has a parameter that is literally called `secretKey`, which is exactly the same as the constructor argument. In the background we do a `container.With("secretKey").EqualTo(<>).GetInstance()`.

Then in your registry:

```csharp
using StructureMap.AutoFactory;

...

For().CreateFactory();
```

Using it in code is as simple as everything else in StructureMap:

```csharp
public class Component
{
public Component(IValueFactory factory)
{
this.factory = factory;
}

void Execute()
{
var value = factory.CreateValue("this is my secret key for you");
}
```

[1]: https://groups.google.com/forum/?fromgroups#!topic/structuremap-users/7_P4gafPsxg