0

I have the following type which I need to be instantiated by StructureMap:

public class AWebService : IAWebService
{
    private readonly string _applicationId;
    private readonly string _username;
    private readonly string _password;

    public AWebService(string applicationId, string username, string password)
    {
        _applicationId = applicationId;
        _username = username;
        _password = password;
    }
}

The problem is that this constructor takes 3 parameters. I've seen examples how to provide StructureMap with one parameter (e.g. Passing constructor arguments when using StructureMap) but I'm not sure what I need to do to pass 3 in.

Is it simply a case of:

For<IAWebService>().Use<AWebService>()
  .Ctor<string>("applicationId").Is(GetIDFromConfig())
  .Ctor<string>("username").Is(GetUsernameFromConfig())
  .Ctor<string>("password").Is(GetPasswordFromConfig());

or do I have to configure it differently?

Community
  • 1
  • 1
DaveDev
  • 41,155
  • 72
  • 223
  • 385

2 Answers2

6

Have you tried the code in your question, it looks correct.

Register your type with structuremap and set the string constructor parameters

var container = new Container(x => {
            x.For<IAWebService>().Use<AWebService>()
                .Ctor<string>("applicationId").Is("my app id")
                .Ctor<string>("username").Is("my username")
                .Ctor<string>("password").Is("my passowrd");
        });

Then you can get your service with the parameters set.

var service = container.GetInstance<IAWebService>();
Bassetassen
  • 20,852
  • 8
  • 39
  • 40
1

I would say you inject a reference of type IConfigurationHelper or something which holds these 3 properties. This way you can just use different types all derived from IConfigurationHelper

thekip
  • 3,660
  • 2
  • 21
  • 41